• SpringMVC学习 通过注解和非注解的方式配置控制器,适配器,映射器


    一:注解式的

     1 @Controller
     2 public class ItemsController2 {
     3     //制定访问(映射)的url
     4     //我对映射器的理解,感觉它就相当于与一个servlet
     5     @RequestMapping("/queryList")
     6     public ModelAndView getList() {
     7         System.out.println("ItemsController2");
     8         List<Items> list = new ArrayList<>();
     9         Items items1 = new Items();
    10         items1.setName("电脑");
    11         items1.setPrice(6000f);
    12         items1.setDetail("联想 Y700");
    13 
    14         Items items2 = new Items();
    15         items2.setName("手机");
    16         items2.setPrice(3000f);
    17         items2.setDetail("HuaWei Mate20");
    18         list.add(items1);
    19         list.add(items2);
    20 
    21         //定义modelAndView
    22         ModelAndView modelAndView = new ModelAndView();
    23         //向modelAndView中添加对象,相当于request中的setAttribute
    24         modelAndView.addObject("ItemsList", list);
    25         //指定视图
    26         modelAndView.setViewName("items/itemsList");
    27         return modelAndView;
    28     }
    29 
    30 
    31 }
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"
     6        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">
     7 
     8     <!--用来扫描controller层的包-->
     9     <context:component-scan base-package="controller"></context:component-scan>
    10 
    11     <!--用来加载MVC提供的映射器和适配器,而且还相较于传统的类加载,这种方式还提供了很多的自定义绑定参数-->
    12     <mvc:annotation-driven></mvc:annotation-driven>
    13 
    14     <!--加载视图处理器-->
    15     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    16         <property name="prefix" value="/WEB-INF/jsp/"></property>
    17         <property name="suffix" value=".jsp"></property>
    18     </bean>
    19 </beans>

    二:非注解式的

     1 public class ItemsController implements Controller {
     2     @Override
     3     public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
     4         List<Items> list = new ArrayList<>();
     5 
     6         Items items1 = new Items();
     7         items1.setName("电脑");
     8         items1.setPrice(6000f);
     9         items1.setDetail("联想 Y700");
    10 
    11         Items items2 = new Items();
    12         items2.setName("手机");
    13         items2.setPrice(3000f);
    14         items2.setDetail("HuaWei Mate20");
    15 
    16         list.add(items1);
    17         list.add(items2);
    18         //定义modelAndView
    19         ModelAndView modelAndView = new ModelAndView();
    20         //向modelAndView中添加对象,相当于request中的setAttribute
    21         modelAndView.addObject("ItemsList", list);
    22         //指定视图
    23         modelAndView.setViewName("items/itemsList");
    24         return modelAndView;
    25     }
    26 }
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        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">
     5 
     6     <!--
     7     配置Handler
     8     通过'name'指定访问的url 通过class 指定被访问的class
     9     -->
    10     <bean name="/itemsController.action" class="controller.ItemsController"></bean>
    11 
    12     <!--
    13         配置处理器的映射器 HandlerMapping
    14         将Bean的name作为Url进行查找,需要在配置Handler时指定beanname
    15         -->
    16     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    17 
    18     <!--
    19         配置处理器适配器
    20         所有适配器都实现controller接口
    21         -->
    22     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    23 
    24     <!--
    25     配置视图解析器
    26     需要配置解析jsp的视图解析器,默认使用jstl标签
    27     -->
    28     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
    29 </beans>

    三:两者通用的web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     5          version="4.0">
     6     <!--context-param可以在web工程发布之前对参数进行初始化,而且他的范围大,整个web工程都能共享他内部的数据-->
     7     <!--contextConfigLocation 和 ContextLoaderListener 由SpringMVC进行识别处理 能够加载配置文件,
     8     当然也可以自定义context-param
     9     -->
    10     <context-param>
    11         <param-name>contextConfigLocation</param-name>
    12         <param-value>/WEB-INF/applicationContext.xml</param-value>
    13     </context-param>
    14 
    15     <listener>
    16         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    17     </listener>
    18 
    19     <!--配置一个servlet 加载DispatcherServlet类,表示声明为一个前置控制器-->
    20     <servlet>
    21         <servlet-name>annotationDispatcher</servlet-name>
    22         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    23         <!--init-param 标签和context-param标签的区别主要在于作用范围,前者只适用于在servlet内部,后者则是全局-->
    24         <init-param>
    25             <param-name>contextConfigLocation</param-name>
    26             <!--通过‘;’分号分隔符,可以实现对多个配置文件的加载-->
    27             <param-value>
    28                 classpath:*.xml;
    29                 /WEB-INF/*-servlet.xml
    30             </param-value>
    31         </init-param>
    32     </servlet>
    33 
    34     <servlet-mapping>
    35         <servlet-name>annotationDispatcher</servlet-name>
    36         <url-pattern>/</url-pattern>
    37     </servlet-mapping>
    38 </web-app>
  • 相关阅读:
    iOS NSString中的搜索方法rangeOfString
    iOS 远程推送通知
    iOS PushMeBaby日志提示SSLwrite():-36 94
    iOS [[NSBundle mainBundle] pathForResource:@"" ofType:@""]无法获取到文件
    iOS 申请测试用的远程推送证书
    数据结构与算法学习笔记(五)
    iOS 上传新版本到AppStore时报错ITMS-90034
    数据结构与算法学习笔记(四)
    数据结构与算法学习笔记(三)
    iOS开发日记49-详解定位CLLocation
  • 原文地址:https://www.cnblogs.com/zhang188660586/p/11678405.html
Copyright © 2020-2023  润新知