zoukankan      html  css  js  c++  java
  • Spring总结之SpringMvc上

    一、简介

        Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架。

    二、流程架构

    1.用户发送请求至 前端控制器DispatcherServlet。

    2.前端控制器DispatcherServlet收到请求后调用处理器映射器HandlerMapping。

    3.处理器映射器HandlerMapping根据请求的Url找到具体的处理器,生成处理器对象Handler及处理器拦截器HandlerIntercepter(如果有则生成)一并返回给前端控制器DispatcherServlet。

    4.前端控制器DispatcherServlet通过处理器适配器HandlerAdapter调用处理器Controller。

    5.执行处理器(Controller,也叫后端控制器)

    6.处理器Controller执行完后返回ModelAnView。

    7.处理器映射器HandlerAdapter将处理器Controller执行返回的结果ModelAndView返回给前端控制器DispatcherServlet。

    8.前端控制器DispatcherServlet将ModelAnView传给视图解析器ViewResolver。

    9.视图解析器ViewResolver解析后返回具体的视图View。

    10.前端控制器DispatcherServlet对视图View进行渲染视图(即:将模型数据填充至视图中)

    11.前端控制器DispatcherServlet响应用户。

    三、springmvc xml配置总结

    1. springmvc.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:mvc="http://www.springframework.org/schema/mvc" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:p="http://www.springframework.org/schema/p" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="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 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
           
       
      <!--处理器映射器-->
      <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />-->
      <!--简单url映射器-->
      <!--<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />-->
       
       
      <!--=========非注解配置==========-->      
      <!--处理器适配器1 实现controller接口 -->
      <!--<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> -->
       
      <!--处理器适配器2实现HttpRequestHandler接口 -->
      <!--<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/> -->
       
      <!-- controller配置 -->
      <!-- <bean name="/userController.do"  class="sy.controller.UserController"/>-->
      <!--===================-->   
       
       
      <!--=========注解配置==========-->   
      <!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->
      <context:component-scan base-package="sy.controller"/>
      <!--注解映射器 -->
      <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
       
      <!--注解适配器 -->
      <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
       
      <!-- springmvc使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,
      可用在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。-->  
      <!--加载了很多参数 -->
      <mvc:annotation-driven></mvc:annotation-driven>
       
      <!--视图解析器-->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
          <property name="prefix" value="/jsp/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
       
      </beans>

      2、web.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
      http://www.springmodules.org/schema/cache/springmodules-cache.xsd  
      http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd" 
      xmlns="http://java.sun.com/xml/ns/javaee" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
        <display-name>MyBatisPro05</display-name>
        <welcome-file-list>
          <welcome-file>login.jsp</welcome-file>
        </welcome-file-list>
         
          <!-- Spring配置文件 -->
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:applicationContext.xml</param-value>
          </context-param>
          <!-- 编码过滤器 -->
          <filter>
              <filter-name>encodingFilter</filter-name>
              <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
              <async-supported>true</async-supported>
              <init-param>
                  <param-name>encoding</param-name>
                  <param-value>UTF-8</param-value>
              </init-param>
          </filter>
          <filter-mapping>
              <filter-name>encodingFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>
          <!-- Spring监听器 -->
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
           
          <!-- 添加对springmvc的支持 -->
          <servlet>
              <servlet-name>springMVC</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:spring-mvc.xml</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
              <async-supported>true</async-supported>
          </servlet>
          <servlet-mapping>
              <servlet-name>springMVC</servlet-name>
              <url-pattern>*.do</url-pattern>
          </servlet-mapping>
      </web-app>

      四、Springmvc常用注解总结

          (1)@RequestMapping:(@RequestMapping(value="/book"))

      RequestMapping是一个用来处理请求地址映射的注解(将请求映射到对应的控制器方法中),可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

      @RequestMapping有三个属性:value:指定实际的url、method:指定请求method类型(get,put,post,delete等)、params:指定request中必须包含某些参数值,才让该方法处理、headers:指定request中必须包含某些指定的header值,才处理请求、consumes:指定处理请求的的提交内容类型(Content-type)、produces:指定返回的内容类型

      例子@RequestMapping(value="/get/{bookid}",method = RequestMethod.GET, params="myParam=myValue",headers = "Accept=application/json")       

          (2)@RequestParam:

      @RequestParam用于将请求参数区数据映射到功能处理方法的参数上。

      @RequestParam有以下三个参数:
          value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
          required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将抛出异常;
          defaultValue:默认值,表示如果请求中没有同名参数时的默认值,设置该参数时,自动将required设为false。

          例子: public String requestparam4(@RequestParam(value="username",required=false) String username)   

          (3)@PathVariable

      @PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。

          例子:@RequestMapping(value="/users/{userId}/topics/{topicId}")
                   public String test(@PathVariable(value="userId") int userId, @PathVariable(value="topicId") int topicId)

          (4)@Responsebody

      @ResponseBody 将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMessageConverter的Adapter转换对象,写入输出流。
      作用: 
       该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
      使用时机:
       返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

      (5)@RequestBody

      @RequestBody 将HTTP请求正文转换为适合的HttpMessageConverter对象。

      作用: 
       i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
       ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
      使用时机:
      A) GET、POST方式提时, 根据request header Content-Type的值来判断:
      application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
      multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
      其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
      B) PUT方式提交时, 根据request header Content-Type的值来判断:
      application/x-www-form-urlencoded, 必须;
      multipart/form-data, 不能处理;
      其他格式, 必须;
      说明:request的body部分的数据编码格式由header部分的Content-Type指定;

  • 相关阅读:
    solr jvm参数内存自动计算
    记一次使用Chrome修改useragent
    如何查看本机中已安装的.Net Framework版本
    经典实例 PetShop 4.0 For .NET 2.0 下载
    Web.Config配置节加密工具
    some command for Nokia IP 740
    JNCIP进阶OSPF MultiArea Configuration
    寒武纪
    Symantec AntiVirus Corporate Edition 10.0 Error: "Can't communicate with the Server Group..."
    心房客
  • 原文地址:https://www.cnblogs.com/jnba/p/10832754.html
Copyright © 2011-2022 走看看