首页 > 代码库 > Spring MVC——适配器与映射器(非注解方式)

Spring MVC——适配器与映射器(非注解方式)

<!--
配置非注解处理器适配器:根据指定规则执行处理器
-->
<!-- 1. SimpleControllerHandlerAdapter:适配所有实现org.springframework.web.servlet.mvc.Controller接口的处理器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

(处理器写法:

  

public class HelloController implements Controller {

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

ModelAndView mav = new ModelAndView();

//存数据,相当于request.setAttribute();
mav.addObject("message", "你好,控制器处理器");

//指定返回视图
mav.setViewName("main.jsp");

return mav;
}

}



<!-- 2. HttpRequestHandlerAdapter:适配所有实现org.springframework.web.HttpRequestHandler接口的处理器,二者可共存 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

(处理器写法:

  

public class HelloController2 implements HttpRequestHandler {

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setAttribute("message", "你好,控制器处理器_2....HttpRequestHandler");

request.getRequestDispatcher("/main.jsp").forward(request, response);

}

}



<!--
配置 非注解处理器映射器:配置url地址与处理器的映射关系
-->
<!-- 1. BeanNameUrlHandlerMapping :根据url地址匹配bean的name属性值 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<!-- 2. SimpleUrlHandlerMapping: 根据url地址匹配bean的id属性值,是方式一的增强版本,二者可共存-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello11.action">helloController</prop><!-- key:url地址 value:bean的id属性值 -->
<prop key="/hello22.action">helloController2</prop>
</props>
</property>
</bean>

Spring MVC——适配器与映射器(非注解方式)