SpringMVC框架(1)之(1.2 入门程序—处理器映射器和处理器适配器(注解方式))

旧城等待, 2022-04-13 08:21 257阅读 0赞

1.DispatcherServlet加载时会默认加载 DispatcherServlet.properties 文件,目的是为了加载里面的处理器映射器、处理器适配器、视图解析器等各个组件;(所以 springmvc.xml两种处理器适配器、两种处理器适配器、视图解析器都可以省略;)

2.如果在 springmvc.xml中配置了以上组件,则以 springmvc.xml优先(即覆盖掉默认的);

DispatcherServlet.properties
在这里插入图片描述

1. 注解映射器:

3.1 之前使用 DefaultAnnotationHandlerMapping 注解映射器
3.1 之后使用 RequestMappingInfoHandlerMapping 注解映射器:
**1. 需要在 Handler使用 @Controller标识这是一个控制器;

  1. 2. 使用 @RequestMapping注解指定 Handler方法对应的 URLURL可以与方法名不一致,但建议一致,方便开发维护);**

2. 注解适配器:

3.1 之前使用 AnnotationMethodHandlerAdapter 注解适配器
3.1 之后使用 RequestMappingHandlerAdapter 注解适配器(不要求Handler(即 Controller)实现接口了,但要求注解映射器和注解适配器配对使用;)

要求注解映射器和注解适配器要一起配对使用;

3. 注解开发的 Handler:

springmvc.xml 中配置注解映射器、注解适配器后,开发注解 Handler:
1. 创建 3. ItemListController3.java 类,使用 @Controller、@RequestMapping注解;
2. 再在 springmvc.xml 文件中配置注解开发的 Handler(即添加对应Handler(即 Controller)类的 bean);
3. 如果有多个 Controller类,则在 springmvc.xml 文件中直接开启注解扫描方式:

  1. <context:component-scan base-package="com.asd"></context:component-scan>

4. 视图解析器 ViewResolver:

根据逻辑视图名解析成真正的视图:
1. ItemListController3.java 类中
modelAndView.setViewName(“/WEB-INF/jsp/itemsList.jsp”)不写全名,改为 modelAndView.setViewName(“itemsList”)
2. springmvc.xml 文件中视图解析器中的 bean添加< prperty>属性标签配置前后缀 (前缀+逻辑视图名+后缀)

2. springmvc.xml(使用注解后)

  1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/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. <!-- 3.注解开发的Handler -->
  3. //单个类时:<bean class="com.asd.ItemListController3"></bean>
  4. <context:component-scan base-package="com.asd"></context:component-scan>
  5. <!-- 1.注解映射器 -->
  6. <bean name="" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingInfoHandlerMapping">
  7. </bean>
  8. <!-- 2.注解适配器 -->
  9. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
  10. <!-- 4.视图解析器 -->
  11. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  12. <property name="prefix" value="/WEB-INF/jsp"></property>
  13. <property name="suffix" value=".jsp"></property>
  14. </bean>
  15. </beans>

2. springmvc.xml(使用注解前)

  1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/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. <!-- 3.配置Handler(因为使用BeanNameUrlHandlerMapping处理器映射器,name配置是url) -->
  3. <bean id="itemsListController1" name="/itemsList.action" class="com.asd.ItemListController"></bean>
  4. <bean id="itemsListController2" class="com.asd.ItemListController2"></bean>
  5. <!-- 1.处理器映射器 --> //可以省略
  6. <!-- 法一:根据bean的name(自定义)查找handler,将action的url配置在bean的name中 -->
  7. <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
  8. </bean>
  9. <!-- 法二:根据bean的name(自定义)查找handler,将action的url配置在bean的name中 -->
  10. <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  11. <property name="mapping">
  12. <props> <!-- < prop>标签中 key:url;值:handler的id -->
  13. <prop key="/items1.action">itemsListController1</prop>
  14. //<prop key="/items2.action">itemsListController1</prop>
  15. <prop key="/items2.action">itemsListController2</prop>
  16. </props>
  17. </property>
  18. </bean>
  19. <!-- 2.处理器适配器 --> //可以省略
  20. <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
  21. <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
  22. <!-- 4.视图解析器 --> //可以省略
  23. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
  24. </beans>

3. ItemListController3.java
(不需要实现接口了,需要在 Handler即 Controller使用 @Controller 标识这是一个控制器;使用 @RequestMapping指定 Handler方法对应的 URL(URL可以与方法名不一致,但建议一致,方便开发维护);

  1. @Controller
  2. public class ItemListController3 {
  3. @RequestMapping("/queryItems")
  4. public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{
  5. //静态数据(商品列表)
  6. List<Items> itemsList=new ArrayList<Items>();
  7. Items item1=new Items();
  8. item1.setName("笔记本");
  9. item1.setPrice(5000);
  10. item1.setDetail("笔记本电脑");
  11. Items item2=new Items();
  12. item2.setName("手机");
  13. item2.setPrice(5000);
  14. item2.setDetail("华为手机");
  15. itemsList.add(item1);
  16. itemsList.add(item2);
  17. ModelAndView modelAndView=new ModelAndView();
  18. //填充数据到 request域中
  19. modelAndView.addObject("items",itemsList);
  20. //视图
  21. //指定转发的jsp页面
  22. //真正视图名:modelAndView.setViewName("/WEB-INF/jsp/itemsList.jsp");
  23. modelAndView.setViewName("itemsList"); //逻辑视图名
  24. return modelAndView;
  25. }
  26. }

运行结果:
(URL:http: // localhost8080/项目名/queryItems.action)

(queryItems即为 Controller中 @ResultMapping注解指定的 url;)

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读