Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading properties file from JSTL

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

like image 397
hind Avatar asked Nov 25 '10 07:11

hind


4 Answers

<%@ 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:

  • Our JSTL wiki page

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:

  • How to internationalize a Java web application?
like image 65
BalusC Avatar answered Dec 28 '22 09:12

BalusC


Try

   < fmt:bundle basename="msg"/>
   < fmt:message key="error.more" />
like image 27
Gopi Krishna Dhulipalla Avatar answered Dec 28 '22 09:12

Gopi Krishna Dhulipalla


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

like image 38
Isuru Samarasinghe Avatar answered Dec 28 '22 07:12

Isuru Samarasinghe


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?

like image 37
ulisescastillo Avatar answered Dec 28 '22 08:12

ulisescastillo