踩坑:1.如果已经按照步骤进行springxml配置,访问出现405 No message found under code'txt.Login' for locale 'zh_CN'。可以考虑开发工具编码问题。其操作如下:设置==》
编辑器==》文件编码==》勾选自动转换成Ascii但显示原生内容。如图:
2.本文段jsp的样式采用bootstrap模板,版本为4.6.2
springxml:
<context:component-scan base-package="dt"></context:component-scan>
<!-- 配置视图解析,配上前后缀简化名称-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="viewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources mapping="/static/**" location="/WEB-INF/static/"></mvc:resources>
<!-- 国际化-->
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basename" value="message"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!-- 配置SessionLocaleResolver-->
<bean class="org.springframework.web.servlet.i18n.SessionLocaleResolver" id="localeResolver">
</bean>
创建3个资源包:
message.properties:
email=邮箱
errorMessage=用户名或密码错误
Login=登录
password=密码
rember=记住我
NotEmpty.user.email=邮箱不能为空
Email.user.email=邮箱格式错误
NotEmpty.user.password=密码不能为空
#typeMismatch.user.email=定制格式 typeMismatch.对象.属性=····
message_en_US.properties:
email=email
errorMessage=The user name or password is incorrect
Login=Login in
password=password
rember=rember me
NotEmpty.user.email=email is not empty
Email.user.email=email format error
NotEmpty.user.password=password is not empty
#typeMismatch.user.email=定制格式 typeMismatch.对象.属性=····
message_zh_CN.properties:
email=邮箱
errorMessage=用户名或密码错误
Login=登录
password=密码
rember=记住我
NotEmpty.user.email=邮箱不能为空
Email.user.email=邮箱格式错误
NotEmpty.user.password=密码不能为空
#typeMismatch.user.email=定制格式 typeMismatch.对象.属性=····
I18NController:
@Controller
public class I18NController {
@GetMapping("/i18n")
public String i18N(User user){
return "login";
}
@RequestMapping("/i18n/{language}_{country}")
public String changgeLocale(@PathVariable String language,
@PathVariable String country,
HttpServletRequest request,
HttpServletResponse response,
@Autowired SessionLocaleResolver localeResolver) {
Locale locale = new Locale(language,country);
localeResolver.setLocale(request,response,locale);
return "login";
}
}
UserController:
@Controller
public class UserController {
@Autowired
MessageSource messageSource;
@PostMapping("login")
/*BindingResult bindingResult用于返回验证结果*/
public String login(@Valid User user, BindingResult bindingResult, Model model ,Locale locale) {
if (bindingResult.hasErrors()) {
return "login";
}
if (!user.getEmail().equals("123@qq.com")&&!user.getPassword().equals("123456")){
model.addAttribute("errorMessage",
messageSource.getMessage("errorMessage",null,locale));
return "login";
}
return "success";
}
}
user:
public class User {
private Integer id;
@NotEmpty
@Email
private String email;
@NotEmpty
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
login.jsp:
<%--
Created by IntelliJ IDEA.
User: D.T
Date: 2023/3/1
Time: 11:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% request.setAttribute("basePath", request.getContextPath()); %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="utf-8">
<title>登录页面——国际化</title>
<link rel="canonical" href="" target="_blank">https://getbootstrap.com/docs/4.6/examples/floating-labels/">
<!-- Bootstrap core CSS -->
<link href="${basePath}/static/assets/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="${basePath}/static/css/floating-labels.css" rel="stylesheet">
</head>
<body>
<%--modelAttribute="user" 关联user对象--%>
<form:form class="form-signin" action="${basePath}/login" method="post" modelAttribute="user">
<div class="text-center mb-4">
<img class="mb-4" src="${basePath}/static/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal"> <spring:message code="Login"/></h1>
<div class="row">
<a class="col-2" href="${basePath}/i18n/zh_CN" >中文</a>
<a class="col-2" href="${basePath}/i18n/en_US" >english</a>
</div>
</div>
<div class="form-label-group">
<input type="email" id="inputEmail" name="email" class="form-control" placeholder="邮箱" autofocus>
<label for="inputEmail"> <spring:message code="email"/></label>
<form:errors path="email"></form:errors>
</div>
<div class="form-label-group">
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="密码">
<label for="inputPassword"> <spring:message code="password"/></label>
<form:errors path="password"></form:errors>
</div>
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="记住我"><spring:message code="rember"/>
</label>
</div>
${errorMessage}
<button class="btn btn-lg btn-primary btn-block" type="submit">
<spring:message code="Login"/>
</button>
<p class="mt-5 mb-3 text-muted text-center">© 2017-2022</p>
</form:form>
</body>
</html>