从 JSTL 读取属性文件

发布于 2024-10-04 07:29:05 字数 729 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戴着白色围巾的女孩 2024-10-11 07:29:05
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 

这是错误的 URI。这是针对已经过时很长时间的旧 JSTL 1.0。对于 JSTL 1.1,您应该使用 http://java.sun.com/jsp/jstl/fmt


我已经在 web.xml 中正确找到了 tld 文件,我确信这一点

;
http://java.sun.com/jstl/fmt
/WEB-INF/lib/fmt.tld

当您修复 taglib URL 时这是不必要的。从您的 web.xml 中删除它,并删除所有那些松散的 TLD 文件。您应该在 /WEB-INF/lib 中只有 jstl.jarstandard.jar。或者,当您使用 JSTL 1.2 时,只需 jstl-1.2.jar。无需再做任何事情。

另请参阅:


属性文件名为 msg.属性




我不断得到

???error.more???

我认为问题不是在属性文件中的消息,而是在定位属性文件中,或者在 中的基本名称中,我应该在哪里找到属性文件,如何在代码中引用它?

将其放在类路径中。在您的特定情况下,使用基本名称 msg,您需要将 msg.properties 文件放在类路径的根目录中。

另请参阅:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 

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.


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>

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 have jstl.jar and standard.jar in /WEB-INF/lib. Or when you're using JSTL 1.2, just the jstl-1.2.jar. Nothing more needs to be done.

See also:


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?

Put it in the classpath. In your particular case, with the base name msg, you need to put the msg.properties files in the root of the classpath.

See also:

梦亿 2024-10-11 07:29:05

尝试

   < fmt:bundle basename="msg"/>
   < fmt:message key="error.more" />

Try

   < fmt:bundle basename="msg"/>
   < fmt:message key="error.more" />
久而酒知 2024-10-11 07:29:05

1) 我应该在哪里找到属性文件?
您必须将属性文件保存在 src 目录中。例如,您有两个名为英语和丹麦语的属性文件

Messages_en.properties

Messages_da.properties

位于如下所示的包内

com.isuru.test.i18N.resources

2) 以及如何在代码中引用它?

<fmt:setLocale value="en" scope="session"/>
<fmt:bundle basename="com.isuru.test.i18N.resources.Messages" scope="session">
<fmt:message key="error.more" />

这将以英文打印相关的按摩

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

Messages_en.properties

Messages_da.properties

inside a package named like below

com.isuru.test.i18N.resources

2) and how can I make a reference to it in the code?

<fmt:setLocale value="en" scope="session"/>
<fmt:bundle basename="com.isuru.test.i18N.resources.Messages" scope="session">
<fmt:message key="error.more" />

This will print the relevant massage in English

树深时见影 2024-10-11 07:29:05

正如 Isuru 所说,您必须将属性文件放在包中,就像讨论类一样。

我遇到了一个奇怪的问题,我一直在正确引用我的属性文件,但从未得到正确的输出,我发现你必须为属性包遵循相同的包名称格式,所以如果你有包:

com.test.clients   
com.test.stores 

你必须创建类似的内容:

com.test.i18n 

不能这样做

other.test.i18n

在这里你可以存储你的属性文件

error.more.properties
error.more_es_MX.properties

然后像平常一样引用它们:

<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="com.example.i18n.text" />
<fmt:message key="error.more" />

此外,这里还有一个关于如何国际化你的网络应用程序的很好的答案

如何国际化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:

com.test.clients   
com.test.stores 

You have to create something like:

com.test.i18n 

You CAN'T do

other.test.i18n

Here you can store your property files

error.more.properties
error.more_es_MX.properties

And then referenced them as you would normally do:

<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="com.example.i18n.text" />
<fmt:message key="error.more" />

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?

丑疤怪 2024-10-11 07:29:05

这可以通过以下步骤来完成。我将 messages_en.properties 文件放在 src/main/resources 文件夹下,并使用下面的代码引用属性文件。我没有考虑任何本地化,所以没有提到任何与此相关的内容。

  1. 在jsp中引用JSTL fmt uri。<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

  2. 在jsp中引用bundle。
    < fmt:bundle basename="messages_en">

  3. 使用属性文件中定义的键。
    < 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.

  1. Refer the JSTL fmt uri in jsp.<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

  2. Refer the bundle in jsp.
    < fmt:bundle basename="messages_en">

  3. Use the keys defined in properties file.
    < fmt:message key="error.loginfailure">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文