Search code examples
eclipsemavenjstlbundlemessage

Using JSTL fmt:message in a Eclipse IDE Maven Spring 4 Java Web Project


I googled the following questions, but answers to them I was unable to find. They are interrelated so I am putting them together in one post. I am a Java backend developer using Eclipse IDE, using JSTL fmt:message tags in a Maven Spring 4 Java project.

Question #1:

If you have a jsp file, that includes another jsp file in it, where does the fmt taglib go?

Question #2:

Can the fmt:setBundle tag have a nested fmt:message tag?

Question #3:

Where does the message.properties file have to be located for use with the fmt:setBundle tag?


Solution

  • Answer #1:

    If you are going to use fmt:message with fmt:setBundle and fmt:setLocale, you will need the fmt taglib. This is obvious I am sure. However, in addition, if you have a jsp file, that includes another jsp file, using the jsp:includes tag, and the included file is the one that contains the fmt tags, then the fmt taglib goes into the included file, and NOT in the wrapper file.

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

    Answer #2:

    I was unable to get the following to work. Eclipse said that the fmt:setBundle tag must be empty. However, I did see examples on the web that nested the fmt:message tag within open and close fmt:setBundle tags.

    <fmt:setBundle basename="message">
       <fmt:message key="name"/>
    </fmt:setBundle>
    

    See: https://www.baeldung.com/jstl

    I WAS able to get the following to work.

    <fmt:setLocale value="en_US" />
    <fmt:setBundle basename="com.studypartner.properties.message" var="lang"/> // approach #1
    <h1><fmt:message key="name" bundle="${lang}" /></h1>
    <fmt:setBundle basename="message" var="lang"/> // approach #2
    <h1><fmt:message key="name" bundle="${lang}" /></h1>
    

    To make this code i18n, you can set the locale to whatever you want from the list of language_COUNTRY codes, and if you have a matching property file, the code above will print the value of key "name" to the page. For instance, I created a properties file called "message_en_US.properties". Since I set the locale to "en_US", and the end of the basename is "message", the properties file I have will be found and the key "name" that is in this file, that matches the fmt:message tag, will end up in the tag above. That part about "lang" in the tags above just seems to be necessary to link the bundle to the message.

    See: https://www.tutorialspoint.com/jsp/jstl_format_message_tag.htm

    Here's a list of language and country codes:

    https://docs.oracle.com/cd/E13214_01/wli/docs92/xref/xqisocodes.html

    Answer #3:

    Ok, look at the code for "approach #1" and "approach #2" again, in Answer #2. Why did I repeat it? I wanted to prove that either approach will work. It seems that it is important to place the properties file in the correct location. However, in a Maven Spring 3 Java web project, you can locate it in a few surprising locations, and it will work for any of them. Take a look at the project setup in the link below:

    project setup

    First, if you don't have a message_en_US.properties file, but you do have a message.properties file, then the fmt:setBundle tag will still work. Also, you can place your properties file in 3 different places, and it will still work.

    a) src/main/java > com.studypartner.properties

    b) src/main/resources > com > studypartner > properties

    c) src/main/resources

    "a" and "b" work for

    "c" works for

    Where of course you would replace "com.studypartner.properties" with your own package name (a, b) or name of nested folders (c).

    Basically, this is what they mean when they say that the properties file has to be on your "classpath".