【发布时间】:2018-07-19 13:52:26
【问题描述】:
我现在正在学习 Spring Boot 框架,我正在尝试将 i18n 概念应用于我的简单应用程序。但是每当我运行应用程序时,都会返回以下错误:"No message found under code 'label.welcomeMessage' for locale 'en_US'."。在我来这里询问但注意到与我合作之前,我已经阅读了有关此问题的信息并尝试了很多。
这是我的 AppClass 配置:
package com.abed.main.configuration;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class AppConfig implements WebMvcConfigurer{
@Bean
public LocaleResolver localeResolver()
{
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr ;
}
@Bean
public LocaleChangeInterceptor LocaleChangeInterceptor()
{
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci ;
}
{
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(LocaleChangeInterceptor());
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource msgSrc = new ReloadableResourceBundleMessageSource();
msgSrc.setBasename("classpath:messages/ticket");
msgSrc.setDefaultEncoding("UTF-8");
return msgSrc;
}
}
这里是欢迎 JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><spring:message code="label.welcomePageTitle"></spring:message></title>
</head>
<body>
<h1><spring:message code="label.welcomeMessage"></spring:message></h1>
<form action="ticket" method="GET">
<spring:message code="label.ticketId"></spring:message><input type="text" name="Student_Id">
<input type="submit" value="<spring:message code="label.search"></spring:message>">
</form>
<a href="createTicket"><spring:message code="label.createTicketSubmit"></spring:message></a>
</body>
</html>
这是我的应用程序的层次结构:
任何帮助,请提前致谢
【问题讨论】:
-
你应该有一个
ticket.properties作为默认值。此外,Spring Boot 已经配置了一个MessageSource那么为什么不简单地使用那个呢?最后,我建议不要使用 JSP,而是使用 Thymeleaf 之类的东西作为您的视图(比 JSP 更灵活,限制更少,尤其是嵌入式容器)。 -
我只是按照你的评论做了,但它仍然是同样的问题。非常感谢您的评论,我的下一步是使用 Thymeleaf 进行此操作
-
我多次测试你的代码。每件事都是正确的!并且欢迎页面显示没有错误。也许您的消息文件语法有误!
标签: java spring jsp spring-boot internationalization