spring mvc的配置
添加spring的支持,导入spring需要的jar包。
配置web.xml.
1 <!--IOC容器配置 --> 2 <context-param> 3 <param-name>contextConfigLocation</param-name> 4 <param-value>/WEB-INF/spring/spring.xml 5 /WEB-INF/spring/spring-*.xml 6 </param-value> 7 </context-param> 8 <!-- 监听器 --> 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 13 <!-- spring mvc配置 --> 14 <servlet> 15 <servlet-name>dispatcherServlet</servlet-name> 16 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 17 <init-param> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>/WEB-INF/spring/mvc/spring-mvc-servlet.xml</param-value> 20 </init-param> 21 <load-on-startup>1</load-on-startup><!-- 系统启动加载 --> 22 </servlet> 23 <servlet-mapping> 24 <servlet-name>dispatcherServlet</servlet-name> 25 <url-pattern>/</url-pattern><!-- 所有请求都经过处理。。注意静态资源的访问 --> 26 </servlet-mapping>
spring-mvc-servlect.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/mvc 7 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 10 <!-- 自动扫描包 --> 11 <context:component-scan base-package="com.phome.**"></context:component-scan> 12 13 <!-- 已经注册了 基于Annotation HandlerMapping,HandlerAdapter 添加了常见的类型转换 --> 14 <!-- 验证和转换 --> 15 <!-- 验证 --> 16 <mvc:annotation-driven validator="validator" 17 conversion-service="convertionService"></mvc:annotation-driven> 18 <!-- 验证是由jsr303验证标准 --> 19 <bean id="validator" 20 class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 21 <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property> 22 </bean> 23 <!-- 配置一个转换器工厂 --> 24 <bean id="convertionService" 25 class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 26 <property name="converters"> 27 <list> 28 <!-- 将来更多的自定义转换器注册到这里如果出错,则显示:typeMismatch.Target类型=内容 --> 29 <bean class="com.phome.convert.StringArray2String"></bean> 30 </list> 31 </property> 32 </bean> 33 34 <!-- 默认的注解映射的支持 自动注册DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter --> 35 <mvc:annotation-driven></mvc:annotation-driven> 36 <!-- 资源的处理 默认处理 --> 37 <mvc:default-servlet-handler /> 38 <mvc:resources mapping="/images/**" location="/images/" 39 cache-period="31556926" /> 40 <mvc:resources mapping="/js/**" location="/js/" 41 cache-period="31556926" /> 42 <mvc:resources mapping="/css/**" location="/css/" 43 cache-period="31556926" /> 44 <!-- 关于返回页面的 --> 45 <bean 46 class="org.springframework.web.servlet.view.InternalResourceViewResolver" 47 p:order="2"> 48 <property name="viewClass" 49 value="org.springframework.web.servlet.view.JstlView" /> 50 <property name="contentType" value="text/html" /> 51 <property name="prefix" value="/" /> 52 <property name="suffix" value=".jsp" /> 53 </bean> 54 </beans>
spring.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 7 <context:component-scan base-package="com.phome.service.**,com.phome.dao.**"></context:component-scan> 8 <!-- Spring 对 I18n的支持 --> 9 <bean id="messageSource" 10 class="org.springframework.context.support.ResourceBundleMessageSource"> 11 <property name="basename" value="com.phome.action.hchxApplication"></property> 12 </bean> 13 </beans>
添加国际化资源文件
hchxApplication.properties
添加资源文件
ValidationMessages.properties
注:1:此处用3.2的jar包。
2:IOC容器配置是父容器,子容器可以访问父容器的内容,相反不可以。
3:<listener>是加载IOC配置文件的。
4: spring mvc配置
配置完成。
转换内置不用我们干预,只提供资源文件,转换类型失败后返回的东西,hchxApplication.properties。 基本类型转换失败后的返回结果。自定义转换实现convert接口。
1 @Controller 2 public class RegisterController { 3 4 @RequestMapping("/register") 5 public String register( @Valid@ModelAttribute("emp") Empvo empvo,BindingResult result) 6 { 7 if (result.hasErrors()) 8 { 9 return "register"; 10 } 11 return "index"; 12 } 13 }
register.jsp
1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 2 3 <form:form action="${pageContext.request.contextPath }/register" commandName="emp" method="post"> 4 name:<input name="empName"/><form:errors path="empName"></form:errors><br/><br/> 5 favs:<input type="checkbox" name="favs" />南<input type="checkbox" name="favs" />动<input type="checkbox" name="favs" />嘛<br> 6 age:<input type="text" name="age"/><form:errors path="age"></form:errors><br> 7 <input type="submit" value="Register"/> 8 </form:form>