从 JSTL 读取属性文件
我正在尝试使用 taglib 读取 JSTL 形式的“属性文件”,但我无法访问它
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
我已在 web.xml 中正确找到了 tld 文件,我确信
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/lib/fmt.tld</taglib-location>
</taglib>
属性文件名是 msg。 属性
<fmt:bundle basename="msg">
<fmt:message key="error.more" />
</fmt:bundle>
我不断收到
???error.more???
而不是属性文件中的消息
我认为问题在于定位属性文件, 或者在基本名称中
<fmt:bundle basename="msg">
我应该在哪里找到属性文件,以及如何在代码中引用它?
谢谢大家
I am trying to read a "properties file" form JSTL using taglib , but i can't access it
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
I've located the tld file correctly in the web.xml , am sure of this
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/lib/fmt.tld</taglib-location>
</taglib>
The properties file name is msg. properties
<fmt:bundle basename="msg">
<fmt:message key="error.more" />
</fmt:bundle>
I keep getting
???error.more???
instead of the message in properties file
I think the problem is either in locating the properties file ,
or in the base name in
<fmt:bundle basename="msg">
where should I locate the properties file , and how can I make a reference to it in the code??
thanks everyone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是错误的 URI。这是针对已经过时很长时间的旧 JSTL 1.0。对于 JSTL 1.1,您应该使用
http://java.sun.com/jsp/jstl/fmt
。当您修复 taglib URL 时这是不必要的。从您的
web.xml
中删除它,并删除所有那些松散的 TLD 文件。您应该在/WEB-INF/lib
中只有jstl.jar
和standard.jar
。或者,当您使用 JSTL 1.2 时,只需jstl-1.2.jar
。无需再做任何事情。另请参阅:
将其放在类路径中。在您的特定情况下,使用基本名称
msg
,您需要将msg.properties
文件放在类路径的根目录中。另请参阅:
This is the wrong URI. This is for the old JSTL 1.0 which is out of life for long. For JSTL 1.1 you should be using
http://java.sun.com/jsp/jstl/fmt
.This is unnecessary when you fix the taglib URL. Remove it from your
web.xml
and remove all those loose TLD files as well. You should just havejstl.jar
andstandard.jar
in/WEB-INF/lib
. Or when you're using JSTL 1.2, just thejstl-1.2.jar
. Nothing more needs to be done.See also:
Put it in the classpath. In your particular case, with the base name
msg
, you need to put themsg.properties
files in the root of the classpath.See also:
尝试
Try
1) 我应该在哪里找到属性文件?
您必须将属性文件保存在 src 目录中。例如,您有两个名为英语和丹麦语的属性文件
位于如下所示的包内
2) 以及如何在代码中引用它?
这将以英文打印相关的按摩
1) where should I locate the properties file?
You have to keep property files inside your src directory. For example you have two property files for English and Danish named
inside a package named like below
2) and how can I make a reference to it in the code?
This will print the relevant massage in English
正如 Isuru 所说,您必须将属性文件放在包中,就像讨论类一样。
我遇到了一个奇怪的问题,我一直在正确引用我的属性文件,但从未得到正确的输出,我发现你必须为属性包遵循相同的包名称格式,所以如果你有包:
你必须创建类似的内容:
你不能这样做
在这里你可以存储你的属性文件
然后像平常一样引用它们:
此外,这里还有一个关于如何国际化你的网络应用程序的很好的答案
如何国际化java Web 应用程序 如何国际化 Java Web 应用程序?
As Isuru says, you have to place the properties file in a package as if you were talking about a class.
I came with a weird problem, I had been referencing my properties file correctly but never got the correct output, what I discovered was that you have to follow the same package name format for your properties bundles, so if you have packages:
You have to create something like:
You CAN'T do
Here you can store your property files
And then referenced them as you would normally do:
Also here is a great answer on how to internationalize your web app
How to internationalize a java web application How to internationalize a Java web application?
这可以通过以下步骤来完成。我将 messages_en.properties 文件放在 src/main/resources 文件夹下,并使用下面的代码引用属性文件。我没有考虑任何本地化,所以没有提到任何与此相关的内容。
在jsp中引用JSTL fmt uri。<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
在jsp中引用bundle。
< fmt:bundle basename="messages_en">
< fmt:message key="error.loginfailure">
This can be done by following the below steps. I placed the messages_en.properties file under src/main/resources folder and referred the properties file using this code below. I didn't considered any localization, so didn't mentioned anything related to this.
Refer the JSTL fmt uri in jsp.<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
Refer the bundle in jsp.
< fmt:bundle basename="messages_en">
< fmt:message key="error.loginfailure">