zoukankan      html  css  js  c++  java
  • CSS样式表、JS脚本加载顺序与SpringMVC在URL路径中传参数与SpringMVC 拦截器

    CSS样式表和JS脚本加载顺序

       Css样式表文件要在<head>中先加载,这样网页显示时可以第一次就渲染出正确的布局和样式,网页就不会闪烁,或跳变

       JS脚本尽可能放在<body>结束时再加载

          Js脚本作为程序,要求按顺序执行,而且是由主线程(单个线程)去执行的,

          如果很JS脚本放在头部,就会导致浏览器无法以多线程的方式加载和渲染页面

          浏览会等待所有JS一个接一个执行完毕后才继续往下加载

          其结果是网页打开速度变慢!

    从现在开始,对于自己的作品,自己代码要达到产品级的要求

    点击后执行一个JS函数(删除为例)

          1.<a>标签: href=”javascript:函数名(参数)

          2.其他标签:onclick=函数名(参数);函数名(参数)

    SpringMVCURL路径中传参数

    (删除为例)

          id时:

               /user/remove?id=123      QueryString      查询字符串

               /user/remove/123         Path            路径传参数

         1.使用路径传参,更加美观,符合RESTful风格,SEO权重也比较高,有利网页排名

         2.使用路径传参,要求映射必须支持占位符,所以Servlet不支持

                  SpringMVC通过下面的方式支持URL占位符

                  @RequestMapping(“/user/remove/{id}”)

                  占位符可以有多个

                  取值时,在请求处理方法中使用@PathVariable注解标记参数

    (分页为例)

         /user/list/3

         /user/list/1   第一页又不希望有/1

         

      如果把占位符写成 /user/list/{pageNo}

      当访问/user/list时就会出现404,因为现在映射不匹配

    解决的方法是

       添加一个新请求处理方法,将映射设置为

       @ReqMapping(“/user/list”)

    Public String list(){

       list(1)

    }

       

    SpringMVC 服务端数据验证

         服务端验证是必须的!即使已经提供JS在浏览器实现了美观的数据验证

              服务端也不能相信JS是靠谱的

         服务端验证可以有效的抵御网络攻击,可以保证服务器安全,

          同时也能有效地检查出不合法的数据,予以拒绝,避免将非法数据写入数据库或文件

        

    服务端验证是有关系统安全的大事

    java中,专门有一个标准提案JSR303负责制定数据验证的规范

    JSR303的常用实现是Hibernate-Validator

    JSR303规定使用注解实现验证,定义验证规则,比如是否可空、是否符合正则表达式、长度、日期格式等是否正确。

    SpringMVC通过@Valid调用数据验证

      通过BindingResult或者Errors请求处理参数拿到验证结果

          在请求处理方法中,应该先调用BindingResult.hasErrors()检查是否有错误,

    再进行其他操作,否则会导致抛出异常

    SpringMVC通过<form:errors path=”属性名”>在页面上显示错误信息

    常用验证注解

    面是主要的验证注解及说明:

    注解

    适用的数据类型

    说明

    @AssertFalse

    Boolean, boolean

    验证注解的元素值是false

    @AssertTrue

    Boolean, boolean

    验证注解的元素值是true

    @DecimalMax(value=x)

    BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

    验证注解的元素值小于等于@ DecimalMax指定的value值

    @DecimalMin(value=x)

    BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

    验证注解的元素值小于等于@ DecimalMin指定的value值

    @Digits(integer=整数位数, fraction=小数位数)

    BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

    验证注解的元素值的整数位数和小数位数上限

    @Future

    java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

    验证注解的元素值(日期类型)比当前时间晚

    @Max(value=x)

    BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number.

    验证注解的元素值小于等于@Max指定的value值

    @Min(value=x)

    BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number.

    验证注解的元素值大于等于@Min指定的value值

    @NotNull

    Any type

    验证注解的元素值不是null

    @Null

    Any type

    验证注解的元素值是null

    @Past

    java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

    验证注解的元素值(日期类型)比当前时间早

    @Pattern(regex=正则表达式, flag=)

    String. Additionally supported by HV: any sub-type of CharSequence.

    验证注解的元素值与指定的正则表达式匹配

    @Size(min=最小值, max=最大值)

    String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence.

    验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小

    @Valid

    Any non-primitive type(引用类型)

    验证关联的对象,如账户对象里有一个订单对象,指定验证订单对象

    @NotEmpty

    CharSequence,Collection, Map and Arrays

    验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)

    @Range(min=最小值, max=最大值)

    CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types

    验证注解的元素值在最小值和最大值之间

    @NotBlank

    CharSequence

    验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格

    @Length(min=下限, max=上限)

    CharSequence

    验证注解的元素值长度在min和max区间内

    @Email

    CharSequence

    验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式

     

    SpringMVC中获取Session

         在请求方法中添加HttpSession类型的参数即可获取到Session

    Maven项目下载JavaDoc和源代码的方法

       方法一:项目上点右键-->Maven-->Download JavaDoc/Sources

       方法二:(全局)Window --Preferences-->Maven-->Download JavaDoc/Sources

     

    下载源代码需要时间比较长! JavaDoc次之    Jar需要时间最短

     

     

    SpringMVC 拦截器

    Servlet的过滤器在SpringMVC依然有效

    拦截器需要实现HandlerInterceptor接口

    preHandle  前处理

    postHandle  后处理,在控制器方法执行后执行

    afterCompletion  在视图生成之后清理和释放资源

    preHandle返回boolean值,true表示继续后续处理,false放弃后续处理

    拦截器的装配

    springmvc-servlet.xml中添加

    <mvc:interceptors>

        <mvc:interceptor> 可以定义多个,按先后顺序执行拦截

                <mvc:mapping> 拦截URL,可以多个

                <mvc:excluding-mapping>   排除拦截的URL, 可以是多个

                <bean class=”指定拦截器类”>   也可以写成<ref id=””>

    示例:

     

    静态资源拦截的处理

    1.将静态资源交给Tomcat等服务器自带的默认处理器处理

    2.在拦截器中排除静态资源URL,最好将静态资源收拢到一个文件夹中(static

    3.动静分离


  • 相关阅读:
    InnoDB On-Disk Structures(三)--Tablespaces (转载)
    InnoDB On-Disk Structures(二)--Indexes (转载)
    InnoDB On-Disk Structures(一)-- Tables (转载)
    UML之状态图
    UML之活动图
    UML交互图
    UML类图
    UML用况图
    UML OOA
    UML问题定义
  • 原文地址:https://www.cnblogs.com/yangxiansen/p/7860021.html
Copyright © 2011-2022 走看看