springmvc-学习总结-非注解式处理器和映射器

时间:2022-10-06 05:46:22

学习参考这里:http://jinnianshilongnian.iteye.com/blog/1752171

非注解式映射器适配器1

<!-- 映射器1 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptorOne"/>
<ref bean="myInterceptorTwo"/>
</list>
</property>
</bean>

<!-- 适配器1 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/><!-- 要求编写的处理器action实现controller接口 -->

<!-- 试图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

<!-- 处理器 -->
<bean id="hello1" class="springMVC.HelloWorldController"></bean>

<!-- 拦截器 -->
<bean id="myInterceptorOne" class="lee.springmvc.interceptor.MyInterceptorOne"></bean>
<bean id="myInterceptorTwo" class="lee.springmvc.interceptor.MyInterceptorTwo"></bean>

非注解式映射器适配器2

<!-- 映射器1 
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptorOne"/>
<ref bean="myInterceptorTwo"/>
</list>
</property>
</bean>
-->
<!-- 映射器2 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptorOne"/>
<ref bean="myInterceptorTwo"/>
</list>
</property>
<property name="mappings"><!-- mappings可以替换为urlMap。注:配置需要改变 -->
<props>
<prop key="/helloABC">hello1</prop><!-- 对url为/helloABC进行访问,跳转到ID为hello1的处理器action -->
<prop key="/helloDEF">hello2</prop>
</props>
</property>
</bean>

<!-- 适配器1 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/><!-- 要求编写的处理器action实现controller接口 -->

<!-- 适配器2 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean><!-- 要求编写的处理器action实现HttpRequestHandler接口 -->

<!-- 试图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

<!-- 处理器 -->
<bean id="hello1" name="/hello" class="springMVC.HelloWorldController"></bean>

<bean id="hello2" class="springMVC.Controller2"></bean>

<!-- 拦截器 -->
<bean id="myInterceptorOne" class="lee.springmvc.interceptor.MyInterceptorOne"></bean>
<bean id="myInterceptorTwo" class="lee.springmvc.interceptor.MyInterceptorTwo"></bean>

非注解的映射器,适配器用的分别是:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping和org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter。此时适配器要求我们定义的处理器action实现HttpRequestHandler接口。映射器我们可以配置mappings或者urlMap属性,将处理器action和自定义的请求url进行绑定。注意:此处我们绑定的是处理器action的id而不是name。

处理器action的代码示例如下:

public class Controller2 implements HttpRequestHandler {

public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("message", "Hello World Controller2");
request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);


}
}