java JSTL fmt:message 和资源包导致 ???hello?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11458547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:11:20  来源:igfitidea点击:

JSTL fmt:message and resource bundle result in ???hello?

javajspinternationalization

提问by Guy

i am trying to use JSLT. i followed some instructions.

我正在尝试使用 JSLT。我遵循了一些指示。

  1. i've installed a tomcat on xamppp activated it with the control panel and it's working fine on port 8080.
  2. in the WEB-INF\lib directory i've added a "jstl-1.2.jar" file and a Messages_en_US.properties
  1. 我在 xamppp 上安装了一个 tomcat,用控制面板激活它,它在端口 8080 上工作正常。
  2. 在 WEB-INF\lib 目录中,我添加了一个“jstl-1.2.jar”文件和一个 Messages_en_US.properties

this is my test.jsp:

这是我的 test.jsp:

<fmt:setLocale value="en_US" scope="application"/>
<fmt:setBundle basename="Messages"/>
<fmt:message key="welcome" />

<h1>test</h1>

<p>Counting to three:</p>
<% for (int i=1; i<4; i++) { %>
<p>This number is <%= i %>.</p>
<% } %>
<p>Done counting.</p>

the counting worked so my jsp is valid. but the welcome just shows ???welcome???.

计数有效,所以我的 jsp 有效。但欢迎只是显示???欢迎???。

what am i doing wrong? am i putting the bundles in the right directory? i tried putting the messages bundle in classes and in WEB-INF - no help. i also tried restarting the server - it didn't help.

我究竟做错了什么?我是否将捆绑包放在正确的目录中?我尝试将消息包放在类和 WEB-INF 中 - 没有帮助。我也尝试重新启动服务器 - 它没有帮助。

how can i use string bundles? how can i use several bundles?

我如何使用字符串包?如何使用多个捆绑包?

cheers,

干杯,

回答by nobeh

A number of things could happen.

可能会发生很多事情。

When you say

当你说

<fmt:setLocale value="en_US" ... />

it means that you should have Messages_en.propertiesor Messages_en-US.properties. Using

这意味着你应该有Messages_en.propertiesMessages_en-US.properties。使用

<fmt:setBundle basename="Messages" var="msg" />

you should assign the loaded messages into a variable inside the current scope. When you need to print a message, then, you should use

您应该将加载的消息分配到当前范围内的变量中。当您需要打印消息时,您应该使用

<fmt:message key="welcome" bundle="${msg}" />

to display the message.

以显示消息。