1 在web.xml文件中配置咱们的 springmvc容器前端
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置咱们的前端控制器 DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation配置spingmv加载配置文件(配置处理器,映射器等等) 若是不配置contextConfigLocation 他就默认加载WEB-INF/Servlet名称-servlet.xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 第一种:*.action 访问以*.action为结尾的DispatcherServlet进行解析 第二种:/ 全部访问地址都有DispatcherServlet进行解析, 对于静态资源的解析须要配置不让DispatcherServlet进行解析,此种方法能够实现resuful的url 第三种:/* 这样不对,这种配置,在转发jsp到一个jsp页面时, 任然是有DispatcherServlet解析jsp地址,不能根据jsp查找到handler会报错 --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
2 配置咱们 springmvc.xml 配置文件java
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 3 配置handler "/queryUser.action": 就是请求的url --> <bean id="/queryUser.action" class="com.shi.controller.ItemsController1"></bean> <!-- 2 配置处理器映射器 将bean的name做为url进行查找,须要配置Handler时,指定beanname就是url--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 1 配置处理器适配器 全部的适配器都要实现 HandlerAdapter 接口 SimpleControllerHandlerAdapter适配器 要求咱们的handler实现Controller接口--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 4 配置试图解析器 解析jsp的 视图解析器:默认使用jstl标签 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
3 编写 handler 注意 上面配置文件的要求web
package com.shi.controller; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.shi.pojo.User; /** * SimpleControllerHandlerAdapter该适配器要求实现Controller接口 * @author SHF * */ public class ItemsController1 implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { //模拟静态数据 List<User> user_list=new ArrayList<User>(); User user1=new User(); user1.setUsername("张三"); User user2=new User(); user2.setUsername("李四"); user_list.add(user1); user_list.add(user2); //建立模型和视图对象 ModelAndView mv=new ModelAndView(); mv.addObject("user_list", user_list); mv.setViewName("WEB-INF/jsp/user.jsp"); return mv; } }
4 注意点spring
1 处理器映射器,(多个映射器能够并存,前端控制器判断能让哪一个映射器处理 就让哪一个映射器处理)json
<!-- 3 配置handler "/queryUser.action": 就是请求的url --> <bean id="itemsController" name="/queryUser.action" class="com.shi.controller.ItemsController1"></bean> <!-- 全部的映射器都实现HanderMapping接口 --> <!-- 2 .1 配置处理器映射器 将bean的name做为url进行查找,须要配置Handler时,指定beanname就是url--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 2.3 配置简单的 url 映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props > <!-- 对 itemsController进行url映射,url是:/query1.action --> <prop key="/query1.action">itemsController</prop> <prop key="/query2.action">itemsController</prop> </props> </property> </bean>
2 处理器适配器 (也能够并存)spring-mvc
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 3 配置handler --> <bean id="HttpController" class="com.shi.controller.ItemsController2"></bean> <!-- 全部的映射器都实现HanderMapping接口 --> <!-- 2.3 配置简单的 url 映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props > <prop key="/HttpController.action">HttpController</prop> </props> </property> </bean> <!-- 1.1 配置处理器适配器 SimpleControllerHandlerAdapter 全部的适配器都要实现 HandlerAdapter 接口 SimpleControllerHandlerAdapter适配器 要求咱们的handler 实现Controller接口 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 1.2 配置处理器适配器HttpRequestHandlerAdapter 要求controller实现 HttpRequestHandler 接口 --> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean> <!-- 4 配置试图解析器 解析jsp的 视图解析器:默认使用jstl标签 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
案例 :mvc
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注解的开发 --> <!-- 2 配置 扫描包下的 handler --> <context:component-scan base-package="com.shi.controller"></context:component-scan> <!-- 1 配置注解扫描驱动 单独配置 注解映射器和注解适配器 mvc:annotation-driven 默认加载不少参数绑定的方法(推荐使用) 好比json默认转换器就默认加载了, --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 3 配置试图解析器 解析jsp的 视图解析器:默认使用jstl标签 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
Handler:app
package com.shi.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; // @Controller 标识 他是一个控制器 @Controller public class UserHandler { //@RequestMapping 实现对queryUsers的url映射 ,一个方法对应一个url @RequestMapping("/queryUsers") public ModelAndView queryUsers()throws Exception{ ModelAndView mv =new ModelAndView(); mv.setViewName("WEB-INF/jsp/user.jsp"); return mv; } }
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 默认加载jstl标签 因此不须要配置jstl了--> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>