mvc非注解的处理器映射器和适配器
在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。
在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
要求编写的Handler实现 Controller接口。
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
要求编写的Handler实现 HttpRequestHandler接口
方式二:
1.springmvc.xml配置
多个映射器可以并存,前端控制器判断url能让哪些映射器映射,就让正确的映射器处理。
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置处理器映射器,springMvc默认的处理器映射器 BeanNameUrlHandlerMapping: 根据bean(自定义Controller)的name属性的url去寻找handler(Action:Controller) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 配置处理器适配器执行Controller ,springmvc默认的 SimpleControllerHandlerAdapter:执行Controller SimpleControllerHandlerAdapter只能适配实现了Controller接口的Controller--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/b.do">indexController</prop> <prop key="/c.do">index2Controller</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"/> <bean id="index2Controller" class="controller.Index2Controller"/> <!-- 配置自定义Controller --> <bean id="myController" name="/hello.do" class="controller.HelloController"></bean> <!-- 配置sprigmvc视图解析器:解析逻辑视图 后台返回逻辑视图:index 视图解析器解析出真正物理视图: 前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsps/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
2.Controller层代码:
package controller; import org.springframework.web.HttpRequestHandler; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class IndexController implements HttpRequestHandler { @Override public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute("hello","this is a HttpRequestHandler"); req.getRequestDispatcher("/WEB-INF/jsps/index.jsp") .forward(req,resp); } }
或者:
package controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class IndexController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
ModelAndView mv=new ModelAndView();
mv.addObject("hello","Welcome To MVC !");
mv.setViewName("first");
return mv;
}
}
3.测试