JSP找不到i18n的默认包

时间:2014-08-07 12:29:46

标签: java jsp servlets intellij-idea internationalization

在浏览整个互联网后,我最后问了这个问题,虽然我觉得描述这种情况有点困难。

我在这里有一个小应用程序,它运行在嵌入式Tomcat服务器(v7)上,并使用servlet和JSP;我尝试用JSTL标签将它们国际化。最终项目部署为JAR,当我从控制台运行java -jar时,嵌入式服务器启动很好,一切正常。

问题当我尝试在IDE中运行它时(我使用IntelliJ Idea v13.1.2):再次,它启动,但不是捆绑中的值,页面显示值例如??? default.username ???

以下是我的JSP主要是这样的:

<!DOCTYPE html>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="language"
   value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}"
   scope="session"/>
<fmt:setLocale value="${language}"/>
<fmt:setBundle basename="messages" scope="session" var="bund"/>

<html>
<head>
    <title><fmt:message bundle="${bund}" key="default.title" /></title>
    <link rel="stylesheet" type="text/css" href="../css/tdb.css" media="all">
</head>

等等。当我使用JAR时,<fmt:message bundle="${bund}" key="default.title" />和类似的部分工作得很好,并导致??? default.title ???来自IDE。在一种情况下,我使用来自servlet的bundle文件,当从JAR运行时,它工作正常,当从IDE运行时,它会导致java.util.MissingResourceException

到目前为止我尝试了什么?我在各个位置添加了我的messages.properties和messages_en_US.properties文件(在资源文件夹中,与java和webapp文件夹位于同一级别;在单独的包中在com.my.example包中;作为com.my.example包中的简单属性文件),尝试仅使用basename(resourceBundle = ResourceBundle.getBundle("messages", locale);)或完全限定的路径引用它;另外,我在web.xml文件中设置了fallbackLocale和localizationContext参数。

我错过了什么?

2 个答案:

答案 0 :(得分:0)

代码看起来很好。

我正在检查我的一些项目,我有这个属性:

<fmt:setBundle basename="org.juanitodread.msg.label" var="label"/>


<fmt:message key="common.title" bundle="${label}" />

我在Eclipse中有我的项目,但你可以尝试没有“scope”属性。我的label.properties文件位于“org.juanitodread.msg”包中。

答案 1 :(得分:0)

我也使用Intellij Idea并遇到类似的问题,这是我的结论。您应该将“消息文件”放到

yourproject/src/main/java/resources 

文件夹(通常,Idea会突出显示带有指定符号的资源文件夹图标)。我在web.xml中没有后备区域设置配置和本地化上下文参数。我的捆绑文件名为 messages_en.properties ,我按照

的方式使用它
<fmt:setBundle basename="messages" var="labels" />

并且不将“resources.messages”添加到basename属性。 我正在运行IntellijIdea版本2016.2,我的应用程序正常运行:

要检查您的资源是否真正加载(如果您的属性未加载到页面,这意味着您显然错过了该文件),请使用以下代码,如https://stackoverflow.com/a/4137991/2759640中所述

ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
URL propsURL = ctxLoader.getResource("opto-mapping.properties");
URLConnection propsConn = propsURL.openConnection();

如果你加载了你的资源, openConnecton 会抛出一个异常,那个与messages.properties的连接已经打开了(至少我已经这样做了,并试图让我的捆绑也工作了很长时间)。

相关问题