• ssm(3-1)Springmvc基础


    1.概念

      spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,目前最主流的 MVC 框架之一,Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架 ,Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口,支持 REST 风格的 URL 请求,采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性

    2.半配置半注解的springmvc

    web.xml

    <!DOCTYPE web-app PUBLIC
            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
      <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
      </welcome-file-list>
    
      <!--characterEncodingFilter拦截器-->
      <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
        </init-param>
        <init-param>
          <param-name>forceRequestEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
        <init-param>
          <param-name>forceResponseEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
    
      <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application.xml</param-value>
      </context-param>
    
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>//web加载spring配置文件
      </listener>
    
      <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>//web加载springmvc的配置文件
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
        </init-param>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>
    

    springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           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">
    
        <context:component-scan base-package="cn.springmvc.controller"  use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
            <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"></context:include-filter>
        </context:component-scan>
    
        <!--一般不用
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
            <!--<property name="prefix"  value="/" ></property>-->
            <!--<property name="suffix" value=".jsp"></property>-->
        <!--</bean>-->
    
        <!--静态资源的处理,与下面一条配置是一起使用的,否则requestmapping失效-->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>
        <!--在配置了静态资源处理以后,先寻找静态资源,在没有的情形下,annotation-driven处理-->
        <mvc:annotation-driven></mvc:annotation-driven>
    
    </beans>
    

    备注:

    InternalResourceViewResolver的作用是prefix+returnval+suffix,在没有使用redirect(表示重定向)和forword(表示转发)表示转发。

    <context:annotation-config/>与<context:component-scan base-package="com.xx.xx" />的关系

    <context:annotation-config/>它的作用是隐式的向Spring容器注册

    AutowiredAnnotationBeanPostProcessor(注入容器后@Autowired注解才能使用

    CommonAnnotationBeanPostProcessor (注入容器后@Resource、@ PostConstruct、@ PreDestroy注解才能使用) 

    PersistenceAnnotationBeanPostProcessor注入容器后@PersistenceContext注解才能使用) 

    RequiredAnnotationBeanPostProcessor注入容器后@Required注解才能使用) 
    <context:component-scan base-package="com.xx.xx" />
    隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和  CommonAnnotationBeanPostProcessor,所以可以使用@Autowired注解与@Resource、@ PostConstruct、@ PreDestroy注解
     

     application.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    		">
    
        <!--扫描的包-->
        <context:component-scan base-package="cn.springmvc">
            <!--排除的注解-->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
            <context:exclude-filter type="annotation"
                                    expression="org.springframework.web.bind.annotation.ControllerAdvice"></context:exclude-filter>
        </context:component-scan>
    
    </beans>
    

     test

    package cn.springmvc.bean;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Person {
        @Value("张三")
        private   String  name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    package cn.springmvc.controller;
    
    import cn.springmvc.bean.Person;
    import com.sun.deploy.net.HttpResponse;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpRequest;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpServletResponse;
    
    @Controller//spring  mvc必须注解
    public class SpringTestContorller {
        @Autowired
        Person  person;
        @ResponseBody//需要导入Jackson包,返回json格式的数据
        @RequestMapping("/login")
        public  Person  loginVF( @RequestParam(required = false) String user, HttpServletResponse response){
            response.setHeader("Access-Control-Allow-Origin", "*");//跨域
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            return  person;
        }
    }
    

     ajax

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<style></style>
    	<body>
    		<button>查询</button>
    	</body>
    	<script type="text/javascript" src="js/jquery.min.js" ></script>
    	<script>
    		$("button").click(function(){
    			$.ajax({
    				type:"post",
    				url:"http://localhost:8080/login",
    				async:true,
    				data:{},
    				success:function(data){
    					console.log(data.name)
    				}
    			});
    		})
    	</script>
    </html>
    

    3.完全基于注解的springmvc,类似spring的IOC,这里不再说明

    4.基础注解释义

    4.1@RequestMapping

    @RequestMapping修饰方法表示注解请求的url,返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器会做如下解析,用prefix+returnval+suffix(后缀)这样的方式的到实际的物理视图,假如修饰类,那么等于类+方法的RequestMapping的拦截地址,注解必须使用@controller,路径/表示根目录(转发表示项目的根目录,转发表示站点的根目录), @RequestMapping 修饰类, 类似于请求命名空间

    @RequestMapping("/helloworld")
    
    public  String execute(){
           System.out.println("spring mvc ...");
           return "success";//拦截 /helloword,转发到 prefix+returnval+suffix(后缀)页面
    }
    

    属性

    Value:指定请求的实际地址

    Method:指定请求的method类型可以是requestMethod.get/post/put/delete等,默认为get请求

    consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html; 

    produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

    params:指定request中必须包含某些参数值是,才让该方法处理。 

    headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。

    params 和 headers支持简单的表达式:

    param1: 表示请求必须包含名为 param1 的请求参数

    !param1: 表示请求不能包含名为 param1 的请求参数

    param1 != value1: 表示请求包含名为 param1 的请求参数,但其值不能为 value1

    {“param1=value1”, “param2”}: 请求必须包含名为 param1 和param2的两个请求参数,且 param1 参数的值必须为 value1

    备注:

    @requestmapping还支持ant风格url

    Ant风格资源地址支持的匹配符

    ?:匹配文件名中的一个字符

    *:匹配文件名中的任意字符

    **:** 匹配多层路径

    例如:

    @RequestMapping(value="helloworld",params={"username","age!=10"},headers={"Content-Type=text/html"})
    @RequestMapping(value="/helloworld/{id}",method=RequestMethod.POST)
    /user/*/createUser
    

    4.3@pathvariable

    @pathvariable映射url绑定的占位符

    Spring3.0 新增的功能,@PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过, @PathVariable("xxx") 绑定到操作方法的入参中。

    @RequestMapping("/helloworld/{id}")
    public String execute2(@PathVariable("id") Integer id){
           System.out.println("execute1..."+id);
           return "success";
    }
    
    @RequestMapping("/login/{str1}/{str2}")
        public  String  test(@PathVariable("str1") String str1,@PathVariable("str2") String str2) {
            System.out.println(str1+":"+str2);
            return  "success";
     }
    

    4.4@RequestParam

    @RequestParam绑定请求参数值

    @RequestMapping(value="/testrequestparam",method=RequestMethod.POST)
    //注入请求参数
    public String execute6(@RequestParam(value="username",required=false) String name,@RequestParam(value="age") Integer age){
           System.out.println("execute5"+name+"..."+age);
           return "success";
    } 

    属性

    value:参数名

    required:是否必须。默认为 true, 表示请求参数中必须包含对应

    的参数,若不存在,将抛出异常

    defaultValue:假如没有找到相应的值那么输入相应的默认值作为传参

    5.5@RequestHeader

    @RequestHeader 绑定请求报头的属性值

    @RequestMapping("/testRequestHeader")
    //注入请求头相应的信息
    public String execute6(@RequestHeader("Accept-language") String info){
           System.out.println("execute6..."+info);
           return "success";
    
    }
    

    5.6 @CookieValue

    @CookieValue 可让处理方法入参绑定某个 Cookie 值

    @RequestMapping("/testcookie")
    public String execute7(@CookieValue(value="sessionId的键") String info){
           System.out.println("execute6..."+info);
           return "success";
    }
    

    6.rest:即 Representational State Transfer。(资源)表现层状态转化

    HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE,它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源

    配置在web.xml中添加如下拦截器,例如@RequestMapping(value="helloworld/{id}",method=RequestMethod.GET/POST/PUT/DELETE)

    <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>
    

    在提较表单中书写相应的隐藏域name为_method value为put或者delete如下

    <form action="helloworld/1" method="post"><br>
    <!—HiddenHttpMethodFilter首先判断事都是post,然后根据value判断是delete还是put -->
      <input type="hidden" name="_method"  value="delete">
      <input type="submit"  value="按钮1">
    </form>
    
    <form action="helloworld/1" method="post"><br>
      <input type="hidden" name="_method"  value="put">
      <input type="submit"  value="按钮2">
    </form>
    

    详细原理请参见HiddenHttpMethodFilter源码

    public static final String DEFAULT_METHOD_PARAM = "_method";

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)

    7.pojo

    带有自动注入的功能 类似struts2,也可以使用级联的属性,纯净的spring来说在注入级联属性前应该先初始化前一个类

    @RequestMapping("/testbean")
    public String execute(User user){
           System.out.println(user);
           return "success";
    }
    

    在form表单需要与其一致,包括级联属性对象名.属性,例如user.name

    8.Springmvc对servlet原生api的支持(9种)

    HttpServletRequest,HttpServletResponse,HttpSession,java.security.Principal,Locale,InputStream,OutputStream,Reader,Writer

    例如

    @RequestMapping("/testapi")
    
    public String testapi(HttpServletRequest request,HttpServletResponse response){
    System.out.println(request+"----"+response);
           return "success";
    }
    

    9.Modleandview类

    @RequestMapping("/testmodel")
    public ModelAndView  testModelAndView(){
           String str ="success";
           ModelAndView view = new ModelAndView(str);
           ModelAndView view2 = view.addObject("name", new Date());
           return view2;
    }
    

    备注:

    ModelAndView作用域为request ,也就是说向resquest中放入数据

    控制器处理方法的返回值如果为 ModelAndView, 则其既包含视图信息("success"),也包含模型数据信息(view.addObject)

    10.利用map和model或者modlemap也可以做为传参的媒介

    @RequestMapping("/testmap")
    public String execute1(Map<String, Object> map){
            map.put("name", Arrays.asList("aaa","bbb"));
           return "success";
    }
    
    @RequestMapping("/testmodel1")
    public String execute2(Model map){
            map.addAttribute("name", "aa");
           return "success";
    }
    

    Spring MVC 在调用方法前会创建一个隐含的模型对象作为模型数据的存储容器。如果方法的入参为 Map 或 Model  类型,Spring MVC 会将隐含模型的引用传递给这些入参。在方法体内,开发者可以通过这个入参对象访问到模型中的所有数据,也可以向模型中添加新的属性数据,Spring MVC 在内部使用了一个org.springframework.ui.Model 接口存储模型数据

    默认是向request中放入数据

    上述运行结果如下:

    page:aa,request:aa,session://session作用域是没有值的

    11.@SessionAttributes, Spring MVC 将在模型中对应的属性暂存到 Session 中。

    可以从隐含对象中获取隐含的模型数据对象(隐形map,model),优先尝试从session中获取该属性,在找不到的前提下,然后用默认的request作用域的参数填充。如果找不到对应的属性,则抛出 HttpSessionRequiredException 异常,只能放在类上面,不能放在方法上面,这个可以产看注解的target

    @SessionAttributes(value={"name"},types={String.class}) // types={String.class}可不写,假如写那么表示String.class类型的值也会放入(包括types类型的值和String execute2(Model map)中map<>中对应的值的类型可以放入session中,也就是说除了是map中指定的类型还可以传入string类型),添加到HttpSession中,value是一个数组类型可以放入多个值

    12. @ModelAttribute

    @ModelAttribute作用有点类似modeldriven(Strus2中),在拦截之前运行。

    @ModelAttribute
        public void  get(Map<String, Object> map){
            person.setName("李四");
            map.put("abc", person);
    }
    

    在使用@modelattribute(“key”)注解修饰时,则会寻找在modelattribute的map中是否存在key且值满足相应的参数的类,那么会使用modelattribute的map中的相应的key的值作为原始的参数值,没有匹配的key,会去寻找@SessionAttributes 注解的键值对中是否含有满足的key,假如存在,那么使用满足的key的值作为原始的参数值,假如都没有就会报错,执行的过程先执行@ModelAttribute,然后执行requstmapping。

    13.spring MVC如何解析视图及自定义视图的创建

    13.1解析视图

    返回结果为string modelandview view modelmap等的情况下都会相应的转化为modelandview,然后通过viewresolve转化为视图的对象

    备注:视图对象由视图解析器负责实例化。由于视图是无状态的,所以他们不会有线程安全的问题

    13.2自定义视图的创建

    需要实现view接口,然后重写getContentType 与render方法,标注@Component注解

    @Component
    public class helloview implements View
    
          @Override
           public String getContentType() {//需要初始化, 类名第一个字母小写
                 return "text/html";
          }
    
           @Override
           public void render(Map<String, ?> model, HttpServletRequest request,
                         HttpServletResponse response) throws Exception {
                  response.getWriter().print("hello view, time: " + new Date());
    }
    

    解析视图view需在spring中书写如下内容

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean>
    

    设置优先级,值越小搜寻的优先级越高

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
                  <property name="order" value="1"></property>
    </bean>
    

    访问

    @RequestMapping("/testbeannameViewResolver")
    public String execute1(){
           return "helloview";  //helloview  类名第一个字母小写,在配置了context-scan的前提下,且添加了@Component标签
    }
    

    14.重定向

    如果返回的字符串中含有redirect:表示重定向

    @RequestMapping("/testredirect")
    public String execute2(){
           return "redirect:/login.jsp";
    }
    

    15.Springmvc的转换器

    15.1Springmvc已经实现的内置转换器

    java.lang.Boolean -> java.lang.String : –

    org.springframework.core.convert.support.ObjectToStringConverter

    java.lang.Character -> java.lang.Number : CharacterToNumberFactory

    java.lang.Character -> java.lang.String : ObjectToStringConverter

    java.lang.Enum -> java.lang.String : EnumToStringConverter

    java.lang.Number -> java.lang.Character : NumberToCharacterConverter

    java.lang.Number -> java.lang.Number : NumberToNumberConverterFactory

    java.lang.Number -> java.lang.String : ObjectToStringConverter

    java.lang.String -> java.lang.Boolean : StringToBooleanConverter

    java.lang.String -> java.lang.Character : StringToCharacterConverter

    java.lang.String -> java.lang.Enum : StringToEnumConverterFactory

    java.lang.String -> java.lang.Number : StringToNumberConverterFactory

    java.lang.String -> java.util.Locale : StringToLocaleConverter

    java.lang.String -> java.util.Properties : StringToPropertiesConverter

    java.lang.String -> java.util.UUID : StringToUUIDConverter

    java.util.Locale -> java.lang.String : ObjectToStringConverter

    java.util.Properties -> java.lang.String : PropertiesToStringConverter

    java.util.UUID -> java.lang.String : ObjectToStringConverter

    15.2 自定义类型转换器

    需要实现的接口converter,重写convert方法(实际是将string转换为自己相应的类型,然后将该类添加到FormattingConversionServiceFactoryBean容器中)

    在spring中添加的配置信息

    在mvc:annotation-driven中装配conversion-service="conversionService"

    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    <!-- 配置 ConversionService -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
         <!—在其添加转换器类 -->             
           <property name="converters">
                         <set>
                                <ref bean="employeeCovert"/>
                         </set>
            </property> 
    </bean>
    

    备注:

    FormattingConversionServiceFactoryBean是ConversionService 接口的实现类,该实现类拓展了GenericConversionService,因此它既具有类型转换的功能,又具有格式化的功能,所以我们一般都是采用实现FormattingConversionServiceFactoryBean,装配了 FormattingConversionServiceFactroyBean 后,就可以在 Spring MVC 入参绑定及模型数据输出时使用注解驱动了。<mvc:annotation-driven/> 默认创建的ConversionService(类型转换) 实例即为FormattingConversionServiceFactroyBean,添加到converters后,会自动的转换,不需要手动的去做其他的设置。假如重复,那么以前的转换器依然可以用,自动用

    16.数据的转换

    转换错误的显示,当转换发生错误的时候,会将相应的错误的信息放置于Bindingresult的内部,通过getdefalutmessage可以获取相应的字段的类型转换错误,BindingResult前面不能放map,只能放在后面

    @RequestMapping(value="/test1")
    public String test(student stu,BindingResult result){
           if(result.getFieldErrorCount()>0){
                  System.out.println("出错了");
                  for(FieldError error:result.getFieldErrors()){
                         System.out.println(error.getField()+":"+error.getDefaultMessage());
                  }
           }
           System.out.println(stu);
           return "success";
    }
    

    FormattingConversionServiceFactoryBean内部已经注册如下两个类型的注解

    时间的转换:@DateTimeFormat(pattern="yyyy-MM-dd")

    可对java.util.Date、java.util.Calendar、java.long.Long 时间

    pattern 属性:类型为字符串。指定解析/格式化字段数据的模式,如:”yyyy-MM-dd hh:mm:ss”

    iso 属性:类型为 DateTimeFormat.ISO。指定解析/格式化字段数据的ISO模式,包括四种:ISO.NONE(不使用) -- 默认、ISO.DATE(yyyy-MM-dd) 、ISO.TIME(hh:mm:ss.SSSZ)、ISO.DATE_TIME(yyyy-MM-dd hh:mm:ss.SSSZ)

    style 属性:字符串类型。通过样式指定日期时间的格式,由两位字符组成,第一位表示日期的格式,第二位表示时间的格式:S:短日期/时间格式、M:中日期/时间格式、L:长日期/时间格式、F:完整日期/时间格式、-:忽略日期或时间格式

    数据的转换:@NumberFormat(pattern="#,###,###.#")

    style:类型为 NumberFormat.Style。用于指定样式类型,包括三种:Style.NUMBER(正常数字类型)、Style.CURRENCY(货币类型)、 Style.PERCENT(百分数类型)pattern:类型为 String,自定义样式如patter="#,###";,但是这两个属性是互斥的,这些类似web的国际化的操作

    17.数据绑定的流程

    Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFactory 实例,以创建 DataBinder 实例对象,DataBinder 调用装配在 Spring MVC 上下文中的 ConversionService 组件进行数据类型转换、数据格式化工作。将 Servlet 中的请求信息填充到入参对象中,调用 Validator 组件对已经绑定了请求消息的入参对象进行数据合法性校验,并最终生成数据绑定结果BindingData 对象,Spring MVC 抽取 BindingResult 中的入参对象和校验错误对象,将它们赋给处理方法的响应入参

    18.静态资源的处理

    Springmvc一般使用/来进行拦截,那么会拦截的结果(包含图片等,但是不包括*.jsp,/*拦截包括*.jsp在内),当我们进行直接访问页面时也会拦截(会寻找requestmapping),所有需要配置<mvc:default-servlet-handler/>,配置这个以后造成拦截后的转发页面找不到的问题,(requestmapping失效),解决的方法是配置<mvc:annotation-driven></mvc:annotation-driven>(在静态找不到时,requestmapping生效),<mvc:default-servlet-handler/> 将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler,它会对进入 DispatcherServlet 的请求进行筛查,如果发现是没有经过映射的请求,就将该请求交由 WEB 应用服务器默认的 Servlet 处理,如果不是静态资源的请求,才由DispatcherServlet 继续处理,一般 WEB 应用服务器默认的 Servlet 的名称都是 default。若所使用的 WEB 服务器的默认 Servlet 名称不是 default,则需要通过 default-servlet-name 属性显式指定<mvc:default-servlet-handler default-servlet-name=""/>

    19.@InitBinder注解

    @InitBinder 标识可以对 WebDataBinder 对象进行初始化。WebDataBinder 是 DataBinder 的子类,用于完成由表单字段到 JavaBean 属性的绑定,@InitBinder方法不能有返回值,它必须声明为void。@InitBinder方法的参数通常是 WebDataBinder,例如

    @InitBinder

    public  void  initBinder(WebDataBinder  wb){

      wb.setDisallowedFields("name");//由表单字段到 JavaBean 属性的绑定哪个值不进行赋值

    }

     20.jsr303(数据校验)

    1)jsr303注解:JSR 303 是 Java 为 Bean 数据合法性校验提供的标准框架,它已经包含在 JavaEE 6.0 中(数据的校验),需要public String test(@Valid Teacher teacher,或者使用@validate注解

    @Null 被注释的元素必须为 null
    @NotNull 被注释的元素必须不为 null
    @AssertTrue 被注释的元素必须为 true
    @AssertFalse 被注释的元素必须为 false
    @Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @Size(max, min) 被注释的元素的大小必须在指定的范围内
    @Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
    @Past 被注释的元素必须是一个过去的日期
    @Future 被注释的元素必须是一个将来的日期
    @Pattern(value) 被注释的元素必须符合指定的正则表达式

    2)Hibernate Validator 扩展注解:Hibernate Validator 是 JSR 303 的一个参考实现

    @Email 被注释的元素必须是电子邮箱地址
    @Length 被注释的字符串的大小必须在指定的范围内
    @NotEmpty 被注释的字符串的必须非空
    @Range 被注释的元素必须在合适的范围内

    3)需要导入的包 

    validation-api-1.0.0.GA.jar jboss-logging-3.1.0.CR2.jar   hibernate-validator-4.3.2.Final.jar

    需要在相应的需要验证的参数上面添加相应的@valid(requestmapping)  例如:public String test(@Valid Teacher teacher(在bindingresult中的前面不能添加map否则会出现错误,如public String test(@Valid Teacher teacher,Map<String, Object> map,BindingResult bindingResult,但是可以这么去放:public String test(@Valid Teacher teacher,BindingResult bindingResult,Map<String, Object> map))

    备注:可能报错是因为jar的冲突

    4)验证错误消息

    <form:errors path="name" ></form:errors>显示相应字段的错误的消息,显示所有字段的错误的消息 <form:errors path="*"></form:errors>(自动的添加进入到了errors中,根据要求可以显示相应的名称)

    自定义错误消息,在相应的国际化文件中添加相应验证的注解.相应的类名的第一个字母小写.属性名,例如(NotEmpty.emp.name),自动的读取自定义的设置,但是需要设置国际化文件配置

    备注:Spring 在进行数据绑定时,可同时调用校验框架完成数据校验工作。在 Spring MVC 中,可直接通过注解的方式进行数据校验,Spring 的 LocalValidatorFactroyBean  既实现了 Spring 的Validator 接口,也实现了 JSR 303 的 Validator 接口。只要在 Spring 容器中定义了一个LocalValidatorFactoryBean,即可将其注入到需要数据校验的 Bean 中。Spring 本身并没有提供 JSR303 的实现,所以必须将JSR303 的实现者的 jar 包放到类路径下。

    在国际化的i18n中还可以添加入下的注解类

    required:必要的参数不存在。如 @RequiredParam(“param1”)标注了一个入参,但是该参数不存在 ,例如(required.emp.name)表示当该属性不存在时

    typeMismatch:在数据绑定时,发生数据类型不匹配的问题 methodInvocation:Spring MVC 在调用处理方法时发生了错误,类似如上

    21.@ResponseBody注解(返回一个json)与@ResponseBody注解(注入)

    见配置的测试。

    @RequestMapping("/testHttpMessageConverter")
    //将请求的内容信息注入到body中
    public String test6(@RequestBody String body){
           System.out.println(body);
           return "www:"+new Date();
    }
    

    原理:HttpMessageConverter<T>负责将请求信息转换为一个对象(类型为 T),将对象(类型为 T)输出为响应信息,也就是说请求信息通过httpinputmessage转换为相应的输入流,然后通过HttpMessageConverter<T>转换为java对象,java对象通过HttpMessageConverter<T>通过httpoutputmessage转换为输出流,然后转换为响应

    21.Springmvc文件的上传与下载

    在spring中配置文件

    <!-- 配置 MultipartResolver -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">//可设置的参数见CommonsMultipartResolver类
           <!-- 设置字符集 -->
           <property name="defaultEncoding" value="UTF-8"></property>
           <!-- 设置上传文件的大小 -->
           <property name="maxUploadSize" value="1024000"></property>  
    </bean>
    

    在jsp中

    <form action="testMultipartResolver" method="POST" enctype="multipart/form-data">
                  File: <input type="file" name="file"/>
                  Des: <input type="text" name="des"/>
                  <input type="submit" value="Submit"/>
    </form>
    

    在handler中

    // MultipartFile才能实现文件的上传
    public String test17(@RequestParam("des") String des,@RequestParam("file") MultipartFile file) throws IOException{
           //获取描述文件
           System.out.println(des);
            //获取文件名
           System.out.println(file.getOriginalFilename());
            //获取文件大小
           System.out.println(file.getSize());
            //获取name
           System.out.println(file.getName());
            //获取ContentType()类型
           System.out.println(file.getContentType());
            //获取byte
           System.out.println(file.getBytes());
            //获取输入流
           System.out.println(file.getInputStream());
           return "success";
    }     
    

    多文件上传

    @RequestMapping("testMultipartResolver")
    public String test17(@RequestParam("des") String des,@RequestParam("file") MultipartFile file[]) throws IOException{
           System.out.println(des);
           System.out.println(file[0].getOriginalFilename());
           System.out.println(file[0].getSize());
           System.out.println(file[0].getName());
           System.out.println(file[0].getContentType());
           System.out.println(file[0].getBytes());
           System.out.println(file[0].getInputStream());
           return "success";
    }配置与单文件上传一样
    

    下载

    ResponseEntity 实现文件的下载
    @RequestMapping("/testResponseEntity")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{
           byte [] body = null;
           ServletContext servletContext = session.getServletContext();
           InputStream in = servletContext.getResourceAsStream("abc.txt");
           body = new byte[in.available()];//available非阻塞的前提下,在阻塞的情况下不能使用,这里在互联网下载时会出问题
           in.read(body);
           HttpHeaders headers = new HttpHeaders();
           headers.add("Content-Disposition", "attachment;filename=abc.txt");
           HttpStatus statusCode = HttpStatus.OK;
           ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
           return response;
    }
    

    22.Springmvc国际化

    需要导入jstl包,利用fmt标签,然后在spring的配置中需要加

    <bean id="messageSource"  class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basename" value="i18n"></property>
    </bean>
    

    id="messageSource" (必须)否则不能完成相应的映射,配置的加载

    在java中提取相应的信息

    @RequestMapping("/i18n")
    public String test12(Locale locale){
           //第一个参数str  第二个参数填充str  第三个参数语言
           String message = source.getMessage("name", null, locale);//ResourceBundleMessageSource  source;
           System.out.println(message);
           return  "i18n1";
    }
    

    使用链接的方式来实现语言切换

    <a href="i18n?locale=en_US">英文</a><br>
    <a href="i18n?locale=zh_CN">中文</a><br>
    

    在spring中配置文件

    <bean id="localeResolver"
                class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    </bean>
    <!-- 实行拦截 -->
    <mvc:interceptors>
    <!-- 配置 LocaleChanceInterceptor -->
                  <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>
    

    原理:通过LocaleChangeInterceptor拦截器获取 LocaleResolver对象,然后通过SessionLocaleResolver设置为session的属性,然后从session中获取locale属性,设置locale属性

    23.自定义拦截器

    自定义拦截器

    需要实现handlerintercepter接口

    重写如下方法afterCompletion,postHandle,preHandle

        //在调用目标方法之后执行
           @Override
           public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
                         throws Exception {
                  System.out.println("one after");
           }
    
        //在调用目标方法之后, 但渲染视图之前,可以对请求域中的属性或视图做出修改.
           @Override
           public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
                         throws Exception {
                  System.out.println("one post");
           }
    
        //在调用目标方法之前执行
           @Override
           public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
                  System.out.println("one pre");
                  //倘若返回为false,那么后续拦截器将不再执行
                  return true;
           }
    }
    

    在spring中添加配置

    <mvc:interceptors>
                  <!-- 配置自定义的拦截器,作用于所有路径,相当于全局 -->
                  <bean class="com.springmvc.interceptors.FirstInterceptor"></bean>     
                  <!-- 配置拦截器(不)作用的路径 ,配置在内表示仅作用域<mvc:mapping path="/emps"/>的路径,相当于局部,当然也可以配置不执行的情况的局部拦截器-->
                  <mvc:interceptor>
                         <mvc:mapping path="/emps"/>
                         <bean class="com.springmvc.interceptors.SecondInterceptor"></bean>
                  </mvc:interceptor>
    </mvc:interceptors>
    

    备注:多个拦截器的执行顺序,postHandle按配置的顺序执行,postHandle与afterCompletion按配置的反序执行

    24.springmvc的异常处理

    异常处理

    需要在spring配置中书写<mvc:annotation-driven/>

    <a href="testexception?i=2">testexception</a>
    
    @RequestMapping("/testexception")
    public String testexception(@RequestParam("i") int i){
           int c=10/i;
           System.out.println(c);
           return "success";
    }
    
    @ExceptionHandler({Exception.class})
    //在(Exception ex)中 不能采用map参数,那么需要回显需要采用返回modelandview,自动注入ex中
    public String test14(Exception ex){
           System.out.println(ex);
           return "error";
    }
    

    假如需要采用map传值,那么可以采用如下方式

    <a href="testexception?i=2">testexception</a>
    
    @RequestMapping("/testexception")
    public String testexception(@RequestParam("i") int i){
           int c=10/i;
           System.out.println(c);
           return "success";
    }
    
    @ExceptionHandler({Exception.class})
    public ModelAndView test14(Exception ex){
           ModelAndView modelAndView = new ModelAndView("error");
           modelAndView.addObject("exception", ex);
           return modelAndView;
    }
    

    异常显示优先级的问题(一个异常属于多个异常,是多个异常的子类):根据异常继承关系找到继承深度最浅的为优先级最高的,ExceptionHandlerMethodResolver 内部若找不到@ExceptionHandler 注解的话,会找@ControllerAdvice 中的@ExceptionHandler 标记的方法

    采用@ControllerAdvice可以定制全局的异常处理

    <a href="testexception?i=2">testexception</a>
    
    @RequestMapping("/testexception")
    public String testexception(@RequestParam("i") int i){
           int c=10/i;
           System.out.println(c);
          return "success";
    }
    
    @ControllerAdvice//@ControllerAdvice不需要添加@Component等扫描组件,自动添加,因为自己已经实现了@Component
    public class TestExceptionHandler { @ExceptionHandler({Exception.class}) public ModelAndView test14(Exception ex){ ModelAndView modelAndView = new ModelAndView("error"); modelAndView.addObject("exception", ex); return modelAndView; } }

    ResponseStatusExceptionResolver

    采用@ResponseStatus 注解 可以放在异常的类名上,也可以放在方法上

    @ResponseStatus(value=HttpStatus.FORBIDDEN, reason="不一致")
    public class UserNameMismacthExcepition extends RuntimeException{
    
    }
    

    参数说明 value:状态码 reason原因

    @RequestMapping("/userNameMismacthExcepition")
    public String test18(@RequestParam("i") int i){
          if(i==15){
                  throw new UserNameMismacthExcepition();
           }
           System.out.println("UserNameMismacthExcepition....");
           return "success";
    }
    
    <a href="userNameMismacthExcepition?i=2">userNameMismacthExcepition</a><br>
    

    以上为标注在类上,当标注在方法上时如下(在方法上的ResponseStatus会优先使用)

    @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="不一致")
    @RequestMapping("/userNameMismacthExcepition")
    public String test18(@RequestParam("i") int i){
           if(i==15){
                  throw new UserNameMismacthExcepition();
           }
           System.out.println("UserNameMismacthExcepition....");
           return "success";
    }
    

    当输入10的时候也会相应的出现404页面,但是System.out.println("UserNameMismacthExcepition....");依旧会运行

    DefaultHandlerExceptionResolver:对一些特定的异常进行处理,如NoSuchRequestHandlingMethodException、HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException、HttpMediaTypeNotAcceptableException等。

    SimpleMappingExceptionResolver当发生异常是发生页面的跳转

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
      <props>
      <!—异常的全类名 -->
        <prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>//转向error页面
      </props>
    </property>
    

    这样匹配的异常会自动的放入相应的request域的exception中( ${exception})(默认值是exception,当然也可以配置<property name="exceptionAtrribute"  value="ex">,那么值就变成了ex( ${ex})

    附录:依赖

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>cn.springmvc.test</groupId>
      <artifactId>springmvctest</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    
      <dependencies>
        <!--spring-webmvc -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.1.0.RELEASE</version>
        </dependency>
    
        <!--spring-aspects -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
          <version>5.1.0.RELEASE</version>
        </dependency>
    
    
        <!--jstl不能使用,jstl会产生冲突,报add异常 -->
        <!--<dependency>-->
          <!--<groupId>javax.servlet</groupId>-->
          <!--<artifactId>jstl</artifactId>-->
          <!--<version>1.2</version>-->
        <!--</dependency>-->
    
        <!--servlet-api测试 -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>4.0.1</version>
          <scope>provided</scope>
        </dependency>
    
        <!-- junit依赖,scope为test所以是灰色的 -->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
    
        <!--spring-test-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>5.1.0.RELEASE</version>
          <scope>test</scope>
        </dependency>
    
        <!--pagehelper-->
        <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>5.1.6</version>
        </dependency>
    
        <!--log4j-->
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
        </dependency>
    
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.7</version>
        </dependency>
    
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.9.7</version>
        </dependency>
    
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.9.7</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
  • 相关阅读:
    MS SQL入门基础:SQL数据库中的事务
    MS SQL入门基础:删除数据
    MS SQL入门基础:触发器的高级应用
    MS SQL入门基础:SQL Server 安全性管理的途径
    MS SQL入门基础:权限管理
    MS SQL入门基础:打开游标
    MS SQL入门基础:数据库更新添加数据
    MS SQL入门基础:游标、游标的优点和种类
    MS SQL入门基础:创建和管理视图
    MS SQL入门基础:数据库中的连接查询
  • 原文地址:https://www.cnblogs.com/gg128/p/9707150.html
Copyright © 2020-2023  润新知