springMVC国际化

踩坑: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">&copy; 2017-2022</p>

</form:form>

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC国际化是建立在Java国际化的基础之上的。它通过提供不同国家/语言环境的消息资源,然后通过Resource Bundle加载指定Locale对应的资源文件,再取得该资源文件中指定key对应的消息。整个过程与Java程序的国际化完全相同,只是Spring MVC框架对Java程序国际化进行了进一步的封装,从而简化了应用程序的国际化。 在Spring MVC中,实现国际化有以下几个步骤: 1. 给系统加载国际化资源文件。 2. 输出国际化消息。可以在视图页面上输出国际化消息,需要使用Spring MVC的标签库;也可以在Controller的处理方法中输出国际化消息,需要使用org.springframework.web.servlet.support.RequestContext的getMessage()方法来完成。 除了默认的实现方式之外,Spring MVC还提供了其他两种国际化方式:SessionLocaleResolver和CookieLocaleResolver。这两种方式的配置稍有不同,包括配置国际化资源文件、配置国际化操作拦截器和配置LocaleResolver。 总结起来,Spring MVC国际化是通过加载不同国家/语言环境的消息资源文件来实现的,可以在视图页面或Controller的处理方法中输出国际化消息。同时,Spring MVC还提供了不同的国际化实现方式供选择。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring MVC国际化](https://blog.csdn.net/A_Runner/article/details/83684593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值