SpringMVC之入门程序——使用浏览器展示商品数据
springMVC执行流程(图片来源:https://www.jianshu.com/p/8a20c547e245)
1.创建pojo(商品实体类)...
2.导包
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>springmvc-01</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> <!-- 前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 默认找/WEB-INF/[servlet的名称(name)]-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 1. /* 拦截所有 jsp js png .css.... 真的全部拦截 建议不使用 2. *.action *.do 拦截以 do action 结尾的请求 肯定能使用 3. / 拦截所有(不包含jsp)(包含.js,.png,.css...) 强烈建议使用 /对静态资源放行 --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
4.配置springmvc.xml核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 扫描@Controller、@Service --> <context:component-scan base-package="deep"></context:component-scan> </beans>
5.创建Controller层
package deep.springmvc.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 deep.springmvc.pojo.Items; /** * 商品管理 * @author DeepSleeping * */ @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.addObject("itemList", list); mav.setViewName("/WEB-INF/jsp/itemList.jsp"); return mav; } }
6.导入jsp页面,获取数据