zoukankan      html  css  js  c++  java
  • springmvc-注解总结

    一、springmvc 概述

    Spring MVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,通过把Model,View,Controller分离,将web层进行职责解耦

    二、springmvc的实现

     1 、在 web.xml 中配置 DispatcherServlet 2、 加入 Spring MVC 的配置文件3、编写处理请求的处理器,并标识为处理器4、编写视图

    加入以下jar包

    jar 包:• commons-logging-1.1.3.jar– spring-aop-4.0.0.RELEASE.jar– spring-beans-4.0.0.RELEASE.jar– spring-context-4.0.0.RELEASE.jar– spring-core-4.0.0.RELEASE.jar– spring-expression-4.0.0.RELEASE.jar– spring-web-4.0.0.RELEASE.jar– spring-webmvc-4.0.0.RELEASE.jar

    web.xml文件的配置

    配置 DispatcherServlet :DispatcherServlet 默认加载 /WEB-• INF/<servletName-servlet>.xml 的 Spring 配置文件, 启动 WEB 层 的 Spring 容器。可以通过 contextConfigLocation 初始化参数自定 义配置文件的位置和名称

    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        
      <display-name>Archetype Created Web Application</display-name>
        <!-- 
        配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POST 请求 
        -->
        <filter>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
      <servlet>
          <servlet-name>dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                 <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springContext.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>dispatcher</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
     
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springContext.xml</param-value>
      </context-param>
      
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
       
     
    </web-app>

    创建 Spring MVC 配置文件: 配置自动扫描的包

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <!--自动扫描包 搜索spring控件 -->
        <context:component-scan base-package="com.mvc.example"></context:component-scan>
        <!-- 视图页面配置 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/views/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
            
          <!--自定义视图解析  -->  
         <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
            <property name="order" value="100"></property>
         </bean>
            
            <!-- 配置直接转发的页面 -->
        <!-- 可以直接相应转发的页面, 而无需再经过 Handler 的方法.  -->
        <mvc:view-controller path="/success" view-name="success"/>
            
        <!-- 在实际开发中通常都需配置 mvc:annotation-driven 标签 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        </bean>
    </beans>

    创建请求处理器类

    @Controller
    public class Hello {
        
        @RequestMapping(value="hello",method=RequestMethod.GET)
        public ModelAndView  heelo() {
            System.out.println("hello");
             ModelAndView mv = new ModelAndView("sucess");//指定视图
              //向视图中添加所要展示或使用的内容,将在页面中使用
                mv.addObject("message", "aaa");
                mv.addObject("name","name");
                return mv;
        }
    }

    @RequestMapping

    Spring MVC 使用 @RequestMapping 注解为控制器指定可• 以处理哪些 URL 请求 在控制器的类定义及方法定义处都可标注

    • @RequestMapping 类定义处:提供初步的请求映射信息。相对于  WEB 应用的根目录– 方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若– 类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于 WEB 应用的根目录 DispatcherServlet 截获请求后,就通过控制器上

    • @RequestMapping 提供的映射信息确定请求所对应的处理 方法。

    映射请求参数、请求方法或请求头

     

     @RequestMapping 映射请求
    Ant 风格资源地址支持 3 种匹配符:• ?:匹配文件名中的一个字符– *:匹配文件名中的任意字符– **:** 匹配多层路径– @RequestMapping 还支持 Ant 风格的 URL:• /user/*/createUser: 匹配 – /user/aaa/createUser、/user/bbb/createUser 等 URL /user/**/createUser: 匹配 – /user/createUser、/user/aaa/bbb/createUser 等 URL /user/createUser??: 匹配 – /user/createUseraa、/user/createUserbb 等 URL

    @PathVariable  映射 URL 绑定的占位符

    带占位符的 URL 是 Spring3.0 新增的功能,该功能在 • SpringMVC 向 REST 目标挺进发展过程中具有里程碑的 意义 通过 @PathVariable 可以将 URL 中占位符参数绑定到控• 制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中.

     REST风格

    HiddenHttpMethodFilter:浏览器 form 表单只支持 GET • 与 POST 请求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一个过滤器,可以将这些请求转换 为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     <mvc:annotation-driven /> 会自RequestMappingHandlerMappingRequestMappingHandlerAdapterExceptionHandlerExceptionResolver三个bean提供以下支持:支持使用ConversionService参数转换支持使用@NumberFormat annotation@DateTimeFormat注解完成数据型的格式化支持使用@Valid注解JavaBean JSR 303 验证支持使用@RequestBody@ResponseBody注解

    HttpMessageConverter<T>HttpMessageConverter<T>Spring3.0 新添加的一个接口,负责求信息转换为一个象(T),象(T为响应信息HttpMessageConverter<T>接口定的方法:Boolean canRead(Class<?> clazz,MediaTypemediaType): 指定转换可以取的型,即转换器是否可将求信息转换为clazz型的象,同指定支持MIME (text/html,applaiction/json)Boolean canWrite(Class<?> clazz,MediaTypemediaType):指定转换是否可将clazz型的象写到响应流中,响应流支持的媒体MediaType中定LIst<MediaType> getSupportMediaTypes()该转换器支持的媒体型。T read(Class<? extends T> clazz,HttpInputMessageinputMessage)求信息流转换为T 型的象。void write(T t,MediaTypecontnetType,HttpOutputMessgaeoutputMessage):T型的象写到响应流中,同指定相的媒体contentType

     

     

     

     

     

     

     

     

  • 相关阅读:
    JSON对象和字符串之间的相互转换
    php小数取整的方法
    Vim之Nerd Tree杂草帮助
    PHP获取汉字拼音首字母
    chmod 变更文件或目录的权限
    grep
    tar
    Linux下socket编程 多线程 进程超时阻塞、卡死问题定位
    一些Lambda表达式的学习
    读高性能MySQL的笔记
  • 原文地址:https://www.cnblogs.com/hellohero55/p/12653701.html
Copyright © 2011-2022 走看看