6

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

hind
  • 131
  • 2
  • 3
  • 7

5 Answers5

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

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
3

Try

   < fmt:bundle basename="msg"/>
   < fmt:message key="error.more" />
一二三
  • 21,059
  • 11
  • 65
  • 74
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

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

1

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?

Community
  • 1
  • 1
ulisescastillo
  • 73
  • 2
  • 11
-1

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">
srinivas
  • 474
  • 7
  • 14