<script type="text/javascript">
function checkSubmit(){
document.forms[0].submit();
}
</script>
<tags:message code="userName"></tags:message>:<input type="text" name="userName" /><font color="red"><form:errors path="user.userName"></form:errors></font></br>
<tags:message code="age"></tags:message>:<input type="text" name="age" /><font color="red"><form:errors path="user.age"></form:errors></font></br>
<tags:message code="password"></tags:message>:<input type="password" name="password" /><font color="red"><form:errors path="user.password"></form:errors></font></br>
<tags:message code="repassword"></tags:message>:<input type="password" name="repassword"><font color="red"><form:errors path="user.repassword"></form:errors></font></br>
<tags:message code="phone"></tags:message>:<input type="text" name="phone" /><font color="red"><form:errors path="user.phone"></form:errors></font></br>
<tags:message code="email"></tags:message>:<input type="text" name="email" /><font color="red"><form:errors path="user.email"></form:errors></font></br>
<tags:message code="website"></tags:message>:<input type="text" name="website"><font color="red"><form:errors path="user.website"></form:errors></font></br>
<tags:message code="date"></tags:message>: <input type="date" name="date" /> <br>
<input type="button" οnclick="checkSubmit()" value="<tags:message code="button"></tags:message>">
</form>
1.需要配置properties文件,文件名xxx_语言_国家
2.导入springmvc标签:
<%@taglib uri="http://www.springframework.org/tags" prefix="tags" %>
3.用户自由选择语言:
4 .xx.servlet.xml配置:
<!--国际化: 该拦截器拦截界面传来的url上的语言参数 只会拦截JSP经过action后,才会将语言设置存放到sesion中,同时从session中取值-->
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<!-- 默认value值locale,可以省略配置 <property name="paramName" value=""></property>
当value值修改后,传过来的参数key要修改为value值
-->
<property name="paramName" value="language"></property>
</bean>
</mvc:interceptors>
<!--界面传来的url上的语言参数需要存放的一个地方,当用户再次访问时,使用之前设置的语言 bean名固定:localeResolver
-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
spring.xml配置:
<!-- bean的名称一定要是 messageSource
ReloadableResourceBundleMessageSource:自动加载properties资源文件,修改properties文件后不需要重启容器
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="properties文件的全路径"></property>
</bean>
5.controller:
@RequestMapping(value = "/m", method = RequestMethod.POST)
public String mid(@ModelAttribute("user") @Valid UserInfo user,BindingResult error){
if(error.hasErrors()){
return "xx.jsp";
}
return null;
}
6.表单验证国际化:
public class UserInfo {
@NotEmpty(message = "{userNameError}")
private String userName;
@NotEmpty(message = "{ageError}")
@Range(min = 1, max = 120, message = "输入的年龄不合法")
private String age;
@NotEmpty(message = "{passwordError}")
@Size(max = 11, message = "输入的密码不能超过11位")
private String password;
@NotEmpty(message = "{repasswordError}")
private String repassword;
@Size(min = 11, max = 11, message = "{phoneError}")
private String phone;
@Email(message = "{emailError}")
private String email;
@Pattern(message = "{websiteError}", regexp = "^((https|http)?:\\/\\/)[^\\s]+")
private String website;
@NotEmpty(message = "{dateError}")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String date;
//get/set省略..
}
xxx.servlet.xml中添加:
<!-- 表单验证国际化 -->
<bean id="localValidatorFactoryBean" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"></property>
</bean>
<mvc:annotation-driven validator="localValidatorFactoryBean"></mvc:annotation-driven>
function checkSubmit(){
document.forms[0].submit();
}
</script>
<a href="${pageContext.request.contextPath}/mid?language=zh_CN">中文</a>
<a href="${pageContext.request.contextPath}/mid?language=en_US">英文</a>
<form action="<%=path%>/m" method="post"><tags:message code="userName"></tags:message>:<input type="text" name="userName" /><font color="red"><form:errors path="user.userName"></form:errors></font></br>
<tags:message code="age"></tags:message>:<input type="text" name="age" /><font color="red"><form:errors path="user.age"></form:errors></font></br>
<tags:message code="password"></tags:message>:<input type="password" name="password" /><font color="red"><form:errors path="user.password"></form:errors></font></br>
<tags:message code="repassword"></tags:message>:<input type="password" name="repassword"><font color="red"><form:errors path="user.repassword"></form:errors></font></br>
<tags:message code="phone"></tags:message>:<input type="text" name="phone" /><font color="red"><form:errors path="user.phone"></form:errors></font></br>
<tags:message code="email"></tags:message>:<input type="text" name="email" /><font color="red"><form:errors path="user.email"></form:errors></font></br>
<tags:message code="website"></tags:message>:<input type="text" name="website"><font color="red"><form:errors path="user.website"></form:errors></font></br>
<tags:message code="date"></tags:message>: <input type="date" name="date" /> <br>
<input type="button" οnclick="checkSubmit()" value="<tags:message code="button"></tags:message>">
</form>
1.需要配置properties文件,文件名xxx_语言_国家
2.导入springmvc标签:
<%@taglib uri="http://www.springframework.org/tags" prefix="tags" %>
3.用户自由选择语言:
<a href="${pageContext.request.contextPath}/mid?language=zh_CN">中文</a>
<a href="${pageContext.request.contextPath}/mid?language=en_US">英文</a>
4 .xx.servlet.xml配置:
<!--国际化: 该拦截器拦截界面传来的url上的语言参数 只会拦截JSP经过action后,才会将语言设置存放到sesion中,同时从session中取值-->
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<!-- 默认value值locale,可以省略配置 <property name="paramName" value=""></property>
当value值修改后,传过来的参数key要修改为value值
-->
<property name="paramName" value="language"></property>
</bean>
</mvc:interceptors>
<!--界面传来的url上的语言参数需要存放的一个地方,当用户再次访问时,使用之前设置的语言 bean名固定:localeResolver
-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
spring.xml配置:
<!-- bean的名称一定要是 messageSource
ReloadableResourceBundleMessageSource:自动加载properties资源文件,修改properties文件后不需要重启容器
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="properties文件的全路径"></property>
</bean>
5.controller:
@RequestMapping(value = "/m", method = RequestMethod.POST)
public String mid(@ModelAttribute("user") @Valid UserInfo user,BindingResult error){
if(error.hasErrors()){
return "xx.jsp";
}
return null;
}
6.表单验证国际化:
public class UserInfo {
@NotEmpty(message = "{userNameError}")
private String userName;
@NotEmpty(message = "{ageError}")
@Range(min = 1, max = 120, message = "输入的年龄不合法")
private String age;
@NotEmpty(message = "{passwordError}")
@Size(max = 11, message = "输入的密码不能超过11位")
private String password;
@NotEmpty(message = "{repasswordError}")
private String repassword;
@Size(min = 11, max = 11, message = "{phoneError}")
private String phone;
@Email(message = "{emailError}")
private String email;
@Pattern(message = "{websiteError}", regexp = "^((https|http)?:\\/\\/)[^\\s]+")
private String website;
@NotEmpty(message = "{dateError}")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String date;
//get/set省略..
}
xxx.servlet.xml中添加:
<!-- 表单验证国际化 -->
<bean id="localValidatorFactoryBean" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"></property>
</bean>
<mvc:annotation-driven validator="localValidatorFactoryBean"></mvc:annotation-driven>