springmvc的基本配置

1.前端控制器的配置

在web.xml中配置前段控制器
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- contextConfigLocation加载springmvc的配置文件(处理器映射器、适配器等等) 如果不配置contextConfigLocation, 
默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->
<param-name>contextConfigLocation</param-name>
 <!-- classpath也就是项目部署后的WEB-INF/classes目录  此配置在src目录下新建个spring目录把springmvc.xml放在spring里 src目录下的东西部署后自动放到                                classes目录下  也可新建个源文件夹存放配置文件                                   -- >
<param-value>classpath:spring/springmvc.xml</param-value> 
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 第一种:*.
spring ,访问以.spring 结尾 由DispatcherServlet进行解析 第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置                   不让 DispatcherServlet进行解析 
使用此种方式可以实现 RESTful风格的url 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能
                                 根据jsp页面找到handler,会报错。 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>

2.springmvc.xml的基本配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


<!-- 可以扫描controller、service包等
这里让扫描controller,指定controller的包
-->
<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>
<!--注解映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!--注解适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->

<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,
如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
-->
<mvc:annotation-driven></mvc:annotation-driven>
       <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀
<property name="prefix" value="/WEB-INF/jsp/"/>
配置jsp路径的后缀
<property name="suffix" value=".jsp"/> -->
</bean>
</beans>

3.自定义参数绑定的配置

<!--  基本数据类型(int,double)可以自动绑定   -->
在springmvc.xml中添加如下配置
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 自定义参数绑定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">  <!-- 名字是固定的 -->
<list>
<!-- 日期类型转换 -->
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>

4.文件上传的配置

在springmvc.xml中配置下面的bean记得导入 commons-fileupload-1.2.2.jar和commons-io-2.4.jar两个jar包
      <!-- 文件上传的配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的大小 5MB 1024*1024*5B -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>

5.关于乱码的配置

post请求的处理
<!-- 基于拦截器-->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
get请求
方案一修改tomcat编码 将tomcat的默认编码和整个工程的编码都设为utf-8
在server.xml中找到8080端口的connector标签添加 URIEncoding="utf-8"
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
iso8859-1是tomcat的默认编码
方案二
都参数重新编码
例如:
对 username重新编码
String newName =  new String( request.getParameter("username").getBytes("iso8859-1"),"utf-8));

6.关于校验器的配置

在springmvc.xml中引用    本配置引用的 是hibernate的校验框架引用hibernate的校验jar包 可以去官网找
   <mvc:annotation-driven conversion-service="conversion" validator="validator"></mvc:annotation-driven>
<!-- 校验器 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- hibernate校验器 -->
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<!-- 指定校验使用的资源文件,在文件中配置错误信息,如不指定默认使用classpath 下的ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource"/>
</bean>
<!-- 校验错误信息配置文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 资源文件名字 -->
<property name="basenames"> <!-- basenames是固定的 -->
<list>
<value>classpath:UserValidatorMessage</value>  <!-- UserValidatorMessage classpath下的 UserValidatorMessage.properties文件-->
</list>
</property>
<!-- 资源文件编码格式 -->
<property name="fileEncodings" value="utf-8"/>
<!-- 对资源文件内容缓存时间,秒 -->
<property name="cacheSeconds" value="120"/>
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值