项目结构:这里只需要红框框住的
1、导入jar包
2、在web.xml中配置DispatcherServlet(前端控制器)
(1)DispatcherServlet位置:
(2)web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WW_15SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- SpringMVC配置文件位置:默认:WEB-INF/{servlet-name}-servlet.xml -->
<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>
<!--
/*拦截所有,jsp png css
*.action 仅仅拦截以.action结尾的请求
/拦截所有,不包含jsp,包含js png css
-->
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="club.ityuchao"></context:component-scan>
</beans>
4、controller书写
package club.ityuchao.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import club.ityuchao.pojo.Items;
@Controller
public class ItemController {
@RequestMapping(value = "/item/itemlist.action")
public ModelAndView itemlist() {
//准备数据(模拟从数据库中查到)
List<Items> list = new ArrayList<Items>();
list.add(new Items(1, "1华为 荣耀8", 2399f, new Date(), "质量好!1"));
list.add(new Items(2, "2华为 荣耀8", 2399f, new Date(), "质量好!2"));
list.add(new Items(3, "3华为 荣耀8", 2399f, new Date(), "质量好!3"));
list.add(new Items(4, "4华为 荣耀8", 2399f, new Date(), "质量好!4"));
list.add(new Items(5, "5华为 荣耀8", 2399f, new Date(), "质量好!5"));
list.add(new Items(6, "6华为 荣耀8", 2399f, new Date(), "质量好!6"));
//该对象用来存放数据和视图
ModelAndView mav = new ModelAndView();
//设置跳转的视图
mav.setViewName("/WEB-INF/jsp/itemList.jsp");
//设置保存的数据
mav.addObject("itemList", list);
return mav;
}
}