SpringMVC点滴(1)

在使用springMVC很久,却一直没有总结其中的一些便捷配置和功能,恰好有空,加以总结

Servlet 3之后,在web.xml中加入async的支持,从而实现异步请求,需要在servlet和filter中同时加入。

<async-supported>true</async-supported>

 在无任何业务处理,只是进行页面转向时,在springmvc.xml中使用view-controller

<mvc:view-controller path="/async" view-name="/async"/>

 静态资源映射,在springmvc.xml中添加映射,从而实现对静态资源(js, css, 图片)的访问。倘若无此设置,html或jsp无法加载资源,渲染出美观页面。

<mvc:resources mapping="/static/**" location="classpath:/static/"/>
在访问资源时,使用static/*/*进行资源加载,而不必使用../等相对位置加载资源的方式,这种方式在maven目录下也无法访问。目录结构如下:

springMVC拦截器,实现HandlerInterceptor或者继承HandlerInterceptorAdaptor,重写preHandle和postHandle方法。在springmvc.xml中配置拦截器栈,配置拦截url和排除url

 <mvc:interceptors>
        <mvc:interceptor>
          <mvc:mapping path="/"/>
            <mvc:exclude-mapping path="/say"/>
            <bean class="com.soft.interceptor.DemoInterceptor"/>
        </mvc:interceptor>
  </mvc:interceptors>

定时任务。在springmvc.xml中配置定时扫描bean,并在定时执行方法上注解执行时间

<task:annotation-driven/>
@Scheduled(fixedDelay = 5000)
public void refresh(){
if(deferredResult!=null){
deferredResult.setResult(new Long(System.currentTimeMillis()).toString());
}
}

 在方法中加入@RequestBody返回字符串,而不加入时返回跳转URL

 上传文件时,配置CommonpartResolver解析器和最大上传文件大小

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <property name="maxUploadSize" value="1000000000"/>
   </bean>
原文地址:https://www.cnblogs.com/CaesarLinsa/p/7990525.html