JSP cannot find default package for i18n
After browsing all over the internet, I ended up asking this question, although I find it difficult to describe the situation.
I have a small application here that runs on an embedded Tomcat server (v7) and uses servlets and JSPs; I am trying to internationalize them using JSTL tags. The final project is deployed as a JAR and when I run it from the console using the java -jar
embedded server starts up well, everything works fine.
The problem is when I try to run it in the IDE (I am using IntelliJ Idea v13.1.2): it starts again, but instead of the values ββfrom the package, the pages show values ββfor example default_username ???.
This is how my JSPs look like:
<!DOCTYPE html>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<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="messages" scope="session" var="bund"/>
<html>
<head>
<title><fmt:message bundle="${bund}" key="default.title" /></title>
<link rel="stylesheet" type="text/css" href="../css/tdb.css" media="all">
</head>
Etc. <fmt:message bundle="${bund}" key="default.title" />
and similar parts work fine when i use the JAR and the result is default.title ??? when from IDE. In one case I am using a package file from a servlet and when run from JAR it works fine and when from IDE it comes up java.util.MissingResourceException
.
What have I tried so far? I added messages.properties and messages_en_US.properties files in different locations (in the resources folder on the same level with the java and webapp folders, in a separate package in the com.my.example package, just like simple properties files in the com.my.example package), tried to access it only with the base name ( resourceBundle = ResourceBundle.getBundle("messages", locale);
) or with the full path; Also, I am setting the fallbackLocale and localizationContext parameters in the web.xml file.
What am I missing?
source to share
The code looks good.
I have been looking at some of my projects and I have the following attributes:
<fmt:setBundle basename="org.juanitodread.msg.label" var="label"/>
<fmt:message key="common.title" bundle="${label}" />
I have my projects in Eclipse, but you can try without the "scope" attribute. My label.properties file is in the org.juanitodread.msg package.
source to share
I am also using Intellij Idea and have similar problems, here are my findings. You have to put your messages file in
yourproject/src/main/java/resources
(Typically, Idea will highlight the resource folder icon with the specified icon). I have no locale setting and localization options in web.xml. My package file is called messages_en.properties and I am using it as a way
<fmt:setBundle basename="messages" var="labels" />
and don't add "resources.messages" to the basename attribute. I am running IntellijIdea version 2016.2 and my application works fine:
- embedded Tomcat v 7.0.73 using bmuschko gradle -tomcat plugin https://github.com/bmuschko/gradle-tomcat-plugin
- remote deployment of Tomcat v 7.0.73 with Tomcat server configuration
To check if your resources are actually loading (if your properties are not loaded on the page, which means you are clearly missing a file), use the following code as mentioned at fooobar.com/questions/876089 / ...
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
URL propsURL = ctxLoader.getResource("opto-mapping.properties");
URLConnection propsConn = propsURL.openConnection();
If you have loaded your resources openConnecton will throw an exception, this connection to Message.properties is already open (at least I did that and tried to make my package work as well for a long time).
source to share