spring MVC 入门程序
由上一篇文章的分析spring MVC主要有以下几个组成部分:dispatcherServlet、HandlerMapping、HandlerAdapter、Handler、ViewResolver、View。其中
DispatcherServlet:前端控制器,由springmvc提供
HandlerMappting:处理器映射器,由springmvc提供
HandlerAdapter:处理器适配器,由springmvc提供
Handler:处理器,需要程序员开发
ViewResolver:视图解析器,由springmvc提供
View:真正视图页面需要由程序编写
1.新建dynamic web工程,HelloSpringMVC
2.在lib目录下放入需要的jar包
3.前端控制器配置
前端控制器在web.xml中进行配置
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <web-appxmlns:xsiweb-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5">
- <display-name>HelloSpringMVC</display-name>
- <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-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>SpringMVC</servlet-name>
- <url-pattern>*.action</url-pattern>
- </servlet-mapping>
- <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>
- </web-app>
4.处理器映射器、处理器适配器、视图解析器配置
这些在springMVC-servlet中配置,在工程目录下新建config的源码文件夹,新建springMVC-servlet.xml文件
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- 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
- ">
- <!-- 自动装配bean -->
- <!-- 自动检测bean -->
- <context:component-scan
- base-package="com.hellospringmvc"
- ></context:component-scan>
- <!-- 配置处理器映射器 -->
- <!-- 根据bean的name进行查找Handler 将action的url配置在bean的name中 -->
- <bean
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <propertynamepropertyname="mappings">
- <props>
- <propkeypropkey="/helloController.action">helloController</prop>
- </props>
- </property>
- </bean>
- <!-- 配置处理器适配器 -->
- <bean
- class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
- <beannamebeanname="/helloController" id = "helloController" class ="com.hellospringmvc.HelloController"></bean>
- <!-- 配置视图解析器
- 要求将jstl的包加到classpath
- -->
- <beanclassbeanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix"value="/WEB-INF/" />
- <property name="suffix"value=".jsp" />
- </bean>
- </beans>
也可以用SimpleurlhandlerMapping作为处理器映射器,配置文件如下
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- 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
- ">
- <!-- 自动装配bean -->
- <!-- 自动检测bean -->
- <context:component-scan
- base-package="com.hellospringmvc"
- ></context:component-scan>
- <bean id ="helloController" class ="com.hellospringmvc.HelloController"></bean>
- <!-- 配置处理器映射器 -->
- <!-- 根据bean的name进行查找Handler 将action的url配置在bean的name中 -->
- <bean
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <propertynamepropertyname="mappings">
- <props>
- <propkeypropkey="/helloController.action">helloController</prop>
- </props>
- </property>
- </bean>
- <!-- 配置处理器适配器 -->
- <bean
- class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
- <!-- 配置视图解析器
- 要求将jstl的包加到classpath
- -->
- <beanclassbeanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix"value="/WEB-INF/" />
- <property name="suffix"value=".jsp" />
- </bean>
- </beans>
5.POJO编写
在src目录下新建com.hellospringmvc包,新建Item类
- packagecom.hellospringmvc;
- importorg.springframework.stereotype.Component;
- public classItem {
- private int id;
- private String name;
- private String detail;
- public String getDetail() {
- return detail;
- }
- public void setDetail(String detail) {
- this.detail = detail;
- }
- private double price;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- }
6.Handler编写
com.hellospringmvc包下新建HelloController类
- packagecom.hellospringmvc;
- importjava.util.ArrayList;
- importjava.util.List;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- importorg.springframework.web.bind.annotation.RequestMethod;
- importorg.springframework.web.servlet.ModelAndView;
- importorg.springframework.web.servlet.mvc.Controller;
- public classHelloController implements Controller{
- @Override
- public ModelAndViewhandleRequest(HttpServletRequest request,
- HttpServletResponseresponse) throws Exception {
- //商品列表
- List<Item> itemsList =new ArrayList<Item>();
- Item items_1 = new Item();
- items_1.setName("联想笔记本");
- items_1.setPrice(6000f);
- items_1.setDetail("ThinkPadT430 联想笔记本电脑!");
- Item items_2 = new Item();
- items_2.setName("苹果手机");
- items_2.setPrice(5000f);
- items_2.setDetail("iphone6苹果手机!");
- itemsList.add(items_1);
- itemsList.add(items_2);
- //创建modelAndView准备填充数据、设置视图
- ModelAndView modelAndView =new ModelAndView();
- //填充数据
- modelAndView.addObject("itemsList",itemsList);
- //视图
- modelAndView.setViewName("helloController");
- return modelAndView;
- }
- }
7.发布网站。访问http://localhost/HelloSpringMVC/helloController.action