springmvc.xml
视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
springmvc处理静态资源
<mvc:default-servlet-handler/>
数据类型转换
前提要加<MVC:annotation-driven>
@DataTimeFormat
@NumberFormat
返回json
@ResponseBody注解
jar包:fastjson.jar
日期转换
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <!-- Date的日期转换器 --> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
springmvc拦截器
<!-- 配置interceptors --> <mvc:interceptors> <mvc:interceptor> <!--mvc作用的路径--> <mvc:mapping path="/sys/**"/> <bean class="cn.smbms.interceptor.SysInterceptor"/> </mvc:interceptor> </mvc:interceptors>
springMVC文件上传
配置multipartResovler
1 <!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver --> 2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <property name="maxUploadSize" value="5000000"/> 4 <property name="defaultEncoding" value="UTF-8"/> 5 </bean>
web.xml
web.xml头文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="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" version="2.5">
DispatcherServlet配置
1 <servlet> 2 <servlet-name>springmvc</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <init-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>classpath:springmvc-servlet.xml</param-value> 7 </init-param> 8 <load-on-startup>1</load-on-startup> 9 </servlet> 10 11 <listener> 12 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 13 </listener> 14 15 <context-param> 16 <param-name>contextConfigLocation</param-name> 17 <param-value>classpath:applicationContext-*.xml</param-value> 18 </context-param> 19 20 21 <filter> 22 <filter-name>encodingFilter</filter-name> 23 <filter-class> 24 org.springframework.web.filter.CharacterEncodingFilter 25 </filter-class> 26 <init-param> 27 <param-name>encoding</param-name> 28 <param-value>UTF-8</param-value> 29 </init-param> 30 <init-param> 31 <param-name>forceEncoding</param-name> 32 <param-value>true</param-value> 33 </init-param> 34 </filter> 35 <filter-mapping> 36 <filter-name>encodingFilter</filter-name> 37 <url-pattern>/*</url-pattern> 38 </filter-mapping>