开发流程:
1.创建web工程
2.导入jar包
3.配置springmvc.xml,配置注解扫描
4.在web.xml配置SpringMVC前端控制器、SpringMVC核心配置文件
5.编写pojo类,Controller类,jsp页面
1.创建web工程
2.导入jar包
3.配置springmvc.xml,配置注解扫描
<?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="com.sise.controller"></context:component-scan>
</beans>
4.在web.xml配置SpringMVC前端控制器、SpringMVC核心配置文件
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvc.xml</param-value>
</init-param>
<!-- 在tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!--
*.action 代表拦截后缀名为.action结尾的
/ 拦截所有但是不包括.jsp
/* 拦截所有包括.jsp
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
5.编写pojo类,Controller类,jsp页面
pojo类
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
........
Controller类
@Controller
public class ItemsController {
@RequestMapping("/list")
public ModelAndView ItemsList() throws Exception {
List<Items> itemList = new ArrayList<Items>();
// 商品列表
Items items_1 = new Items();
items_1.setName("小米手机");
items_1.setPrice(6000f);
items_1.setDetail("小米MIX2!");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphoneX苹果手机!");
itemList.add(items_1);
itemList.add(items_2);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
modelAndView.setViewName("/itemList.jsp");
return modelAndView;
}
}