三步轻松搭建springMVC框架

 更新时间:2017年08月10日 16:20:36   作者:~驰~  
这篇文章主要教大家三步轻松搭建springMVC框架,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、搭建步骤

1、导入jar包、创建项目包结构

2、在web.xml中配置前端控制器

3、编写springMvc核心配置文件

4、编写pojo类和Controller类测试

二、实现

1、导入jar包、创建项目包结构

 2、在web.xml中配置前端控制器

<!-- springMvc前端控制器 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springMvc核心配置文件位置
如果没有指定那么默认就会去"/WEB-INF/+ <servlet-name>标签中内容 + -servlet.xml"中找
例如:"/WEB-INF/springMvc-servlet.xml"
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

3、编写springMvc核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="cn.it.controller"></context:component-scan>

</beans>

4、编写pojo类和Controller类测试

pojo类代码:

package cn.it.pojo;

import java.util.Date;

public class Items {

private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name == null ? null : name.trim();
}

public Float getPrice() {
return price;
}

public void setPrice(Float price) {
this.price = price;
}

public String getPic() {
return pic;
}

public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}

public Date getCreatetime() {
return createtime;
}

public void setCreatetime(Date createtime) {
this.createtime = createtime;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
}

Controller类代码:

package cn.it.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.it.pojo.Items;

@Controller
public class ItemsController {

//@RequestMapping指定URL到请求方法的映射,例如:
@RequestMapping("/itemsList")
public ModelAndView itemsList(){
List<Items>itemList = new ArrayList<Items>();

//商品列表
Items items_1 = new Items();
items_1.setName("联想笔记本_3");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");

itemList.add(items_1);
itemList.add(items_2);

/*
* 模型和视图:
* model模型:模型对象中存放了返回给页面的数据
* view视图:视图对象中指定了返回的页面的位置
*/
//创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • rabbitmq使用springboot实现direct模式(最新推荐)

    rabbitmq使用springboot实现direct模式(最新推荐)

    这篇文章主要介绍了rabbitmq使用springboot实现direct模式,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • SpringBoot中的统一异常处理详细解析

    SpringBoot中的统一异常处理详细解析

    这篇文章主要介绍了SpringBoot中的统一异常处理详细解析,该注解可以把异常处理器应用到所有控制器,而不是单个控制器,借助该注解,我们可以实现:在独立的某个地方,比如单独一个类,定义一套对各种异常的处理机制,需要的朋友可以参考下
    2024-01-01
  • 用SpringBoot框架来接收multipart/form-data文件方式

    用SpringBoot框架来接收multipart/form-data文件方式

    这篇文章主要介绍了用SpringBoot框架来接收multipart/form-data文件方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • SpringBoot自动装配原理详解

    SpringBoot自动装配原理详解

    这篇文章主要详细介绍了SpringBoot的自动装配原理,文中通过代码示例介绍的非常详细,需要的朋友可以参考一下
    2023-04-04
  • Spring @Profile注解实现多环境配置

    Spring @Profile注解实现多环境配置

    这篇文章主要介绍了Spring @Profile注解实现多环境配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • java提取字符串中数字string以及获取字符串中的整数或小数

    java提取字符串中数字string以及获取字符串中的整数或小数

    这篇文章主要给大家介绍了关于java提取字符串中数字string以及获取字符串中的整数或小数的相关资料,需要的朋友可以参考下
    2023-08-08
  • SpringBoot集成quartz实现定时任务

    SpringBoot集成quartz实现定时任务

    这篇文章主要介绍了如何使用SpringBoot整合Quartz,并将定时任务写入库中(持久化存储),还可以任意对定时任务进行如删除、暂停、恢复等操作,需要的可以了解下
    2023-09-09
  • Python文件高级操作函数之文件信息获取与目录操作

    Python文件高级操作函数之文件信息获取与目录操作

    这篇文章主要介绍了Python文件高级操作函数之文件信息获取与目录操作,在Python中,内置了文件(File)对象。在使用文件对象时,首先需要通过内置的open()方法创建一个文件对象,然后通过该对象提供的方法进行一些基本文件操作,需要的朋友可以参考下
    2023-05-05
  • Spring实现IoC的多种方式小结

    Spring实现IoC的多种方式小结

    本篇文章主要介绍了Spring实现IoC的多种方式小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • springboot+vue实现页面下载文件

    springboot+vue实现页面下载文件

    这篇文章主要为大家详细介绍了springboot+vue实现页面下载文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12

最新评论