JSP cannot find default bundle for i18n - java

After browsing through the whole internet I ended up asking this question, although I find it a bit difficult to describe the situation.
I have a little application here which runs on embedded Tomcat server (v7), and uses servlets and JSPs; I try to internationalize them with JSTL tags. The final project is deployed as JAR, and when I run it from the console with java -jar, the embedded server starts nicely, everything works just fine.
The problem is when I try to run it in the IDE (I use IntelliJ Idea v13.1.2): again, it starts, but instead of the values from the bundle, the pages show values such as ???default.username???.
Here is how my JSPs mostly 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>
And so on. The <fmt:message bundle="${bund}" key="default.title" /> and similar parts work perfectly fine when I use the JAR, and result in ???default.title??? when from IDE. In one case I use the bundle file from the servlet, and when ran from JAR, it works fine, and when from IDE, it causes java.util.MissingResourceException.
What have I tried so far? I added my messages.properties and messages_en_US.properties files in various locations (in resources folder, on the same level with the java and webapp folders; in separate package in the com.my.example package; as simple properties files in the com.my.example package), tried to refer to it only with the basename (resourceBundle = ResourceBundle.getBundle("messages", locale);), or with the fully qualified path; also, I set the fallbackLocale and localizationContext parameters in the web.xml file.
What am I missing?

Code looks well.
I was checking in some of my projects and I have this 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 "scope" attribute. My label.properties file is in "org.juanitodread.msg" package.

I also use the Intellij Idea and had the similar problems, here are my conclusions. You should put your 'messages file' to
yourproject/src/main/java/resources
folder (as a rule, Idea will highlight the resources folder icon with the specified sign). I have no fallback locale configuration and localization context parameters in web.xml. My bundle file is named messages_en.properties and I use it the way
<fmt:setBundle basename="messages" var="labels" />
and not add "resources.messages" to basename attribute.
I'm running IntellijIdea version 2016.2, and my application works fine on:
embedded Tomcat v 7.0.73 , using bmuschko gradle-tomcat plugin https://github.com/bmuschko/gradle-tomcat-plugin
remote Tomcat v 7.0.73 deployment with Tomcat Server Idea configuration
To check your resources are really loading (if your properties are not loaded to the page that means you're obviously missing the file), use the following code, as mentioned in https://stackoverflow.com/a/4137991/2759640
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
URL propsURL = ctxLoader.getResource("opto-mapping.properties");
URLConnection propsConn = propsURL.openConnection();
In case you have your resources loaded, openConnecton will throw an exception, that connection to the messages.properties has been already opened (at least I've done that way and tried to make my bundle work also for a long time).

Related

JSP Tomcat7 static css and js files not loading

Learning JSP and have an issue with the css/js content not loading. I have a jsp page, where I have bootstrap css and js referenced using standard html link and script tags:
<link rel="stylesheet" type="text/css" link="/bootstrap/css/bootstrap.css" />
and
<script src="/WebIntro/bootstrap/js/bootstrap.js"></script>
neither of them work and Chrome is giving me the following on the console:
Resource interpreted as Stylesheet but transferred with MIME type text/plain:
If I use an include directive, it works for the css but pulls all the content into the file:
<%#include file="/bootstrap/css/bootstrap.css"%>
The jsp has the following #page and meta tags:
<%#page contentType="text/html" %>
and
<meta http-equiv = "Content-Language" content = "en"/>
<meta http-equiv = "Content-Type" content="text/html; charset=utf-8">
I tried googling and found the mime-mapping element for the web.xml but the following seems to have no affect:
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>application/javascript</mime-type>
</mime-mapping>
If there is some standard Tomcat config that needs to happen I am unaware as I am new to Tomcat; using Tomcat 7, and eclipse and this is in a maven project.
The solution for me was very strange. The changes to the web.xml file that was present since I built the project in eclipse/maven, weren't reflected in the web.xml within tomcat webapp application directories. Everything else in the package updates fine (as far as I can tell), except for the web.xml?
So I found the web.xml file within eclipse inside target >> project-SNAPSHOT >> WEB-INF >> web.xml (which is supposed to be derived). Forced changes there and it worked, changes were picked up and my original issue gone.
I am guessing I had inadvertently broken some type of dependency link or something along the way somehow, but once I added the servlet, mappings, etc back in, everything worked fine.
Weird.
Removing this line in jsp resolved the error for us.
We were facing the issue when using Tomcat 8.5.59

Error when including jsp files on my jsp page

I am trying to include 2 JSP files in my JSP page. My main page is called temp.jsp - this is in a subfolder in my web project called tempFolder.
I am trying to include a file in the main project folder (called invalidcqs.jsp) and a file (called env_status_report.jsp) in a sub folder (envmon) of the main project folder.
the code in my temp.jsp file is:
<html>
<head>
<title>Screen1 using includes</title>
<meta http-equiv="refresh" content="10"/>
</head>
<body style="background-color:#E6E6FA">
<%# include file="../envmon/env_status_report.jsp" %>
<br><hr><br>
<%# include file="../invalidcqs.jsp" %>
</body>
</html
The second include <%# include file="../invalidcqs.jsp" %> works fine but the first one <%# include file="/../envmon/env_status_report.jsp" %> shows an error in Eclipse.
The text of the error is:
Multiple annotations found at this line:
- Syntax error on token "else", delete this token
- Syntax error, insert "Finally" to complete
TryStatement
- Syntax error on token "else", delete this token
Does anyone know why Eclipse doesn't like this?
Usually I don't care much about Eclipse reporting errors on jsp pages, specially when using the <%# include> directive. For instance, if you declare a scriptlet variable in your main page and use it in the included page, Eclipse will complain about it not being declared while in the included page, but it will work all right at runtime.
This error is possibly coming out of the included jsp, so I'd start looking for this error inside it.
You could also try to include pages the EL way:
<jsp:include page="/WEB-INF/pages/received.shtml" />
Maybe that will help

Eclipse - Can't find facelet tag library for uri http://java.sun.com/jsf/html

In my index.xhtml , I have a namespace defined like this xmlns:h="http://java.sun.com/jsf/html". The server at this url indicates that the page cannot be found.
Do you know where the page has moved ?
Eclipse Info
Version: Indigo Release
Build id: 20110615-0604
For solving this problem this is what I did :
1. Close the eclipse project
2. Open the eclipse project
3. Right click on the project
4. Click on Validate
=> The (false) warnings are gone.
The XML taglib namespace URI does not point to a real web resource or something. It just points to the same value as the <namespace> entry of the .taglib.xml file of the XML taglib in question in the runtime classpath, which in case of JSF taglibs (and lot others) just happens to be a HTTP URL. In case of Mojarra, you can find the declaration in the /com/sun/faces/metadata/taglib/html_basic.taglib.xml file of the jsf-impl.jar file.
If you're encountering problems with referencing JSF HTML tags, then the cause lies somewhere else.
Previous answers were useful to me. Here I provide an alternative way for solving this issue. I fixed this problem by adding the jar primefaces-[version].jar to the WEB-INF/lib directory.
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
jsf-api.jar and jsf-impl.jar jar in your lib.
Compare the Uri respective to the jsf version you are using.
. The real JSTL 1.0 taglib uses the URI *http://java.sun.com/jstl/core.*
. The real JSTL 1.1/1.2 taglib uses the URI *http://java.sun.com/jsp/jstl/core*.
. Facelets 1.x uses the URI *http://java.sun.com/jstl/core.*
. Facelets 2.x uses the URI *http://java.sun.com/jsp/jstl/core.*
. Facelets 2.2+ uses the URI *http://xmlns.jcp.org/jsp/jstl/core.*

Add Jar to Runtime Path for JSP

I'm using IBM's Rational Software Architect (essentially Eclipse I suppose). I have a JAR file that contains Proxy classes to access a Web Service (JAX-RPC). I've created a Dynamic Web Project with a simple JSP page in which I'm trying to consume the Web Service using a Proxy class from this library. Code from the JSP page:
<jsp:useBean id="queryProxy" scope="session" class="location.DataSearchProxy" />
<% queryProxy.setEndpoint("http://localhost:9080/CIDataService/services/DataSearch"); %>
<%=queryProxy.query("SELECT street, city, prov, postcode FROM v_location WHERE c1 = '48704'") %>
At this point I have added the External JAR file to the Libraries section of the Build Path, however when running the JSP page on WebSphere in-browser; I get the "DataSearchProxy cannot be resolved to a type" error.
I have also tried using:
<%# page import="location.DataSearchProxy" %>
<% DataSearchProxy queryProxy = new DataSearchProxy(); %>
<% queryProxy.setEndpoint("http://localhost:9080/CIDataService/services/DataSearch"); %>
<%=queryProxy.query("SELECT street, city, prov, postcode FROM v_location WHERE c1 = '48704'") %>
But I get the same error. I have a feeling for this type of Web-Project I may need to reference it in some other way so it can be resolved from JSP pages or other Beans in the project. I may be going about this in the wrong way and I hope someone can point me in the right direction for consuming a Web Service from JSP.
You probably want to package the jar into your application.
Remove it from your build path (we'll add it back later)
Put the JAR in the root of the EAR, drag it to your Application project, not the WEB App
Open your Web project properties and go to Java EE dependencies. You should see the jar offered there. Select it and it will add it to both the build time classpath and also the Manifest so that it's picked up at runtime.

pre-compile jsp files happend exceptions

I pre-compile some jsp files through ant task of jspc,but it built failed.
errers:
info.jsp(35,2) The attribute prefix fn does not correspond to any imported tag library
info.jsp line 35 :
<c:if test="${fn:length(requestScope.checkDetailInfoList) gt 1}">
ant task xml:
<jasper validateXml="false" uriroot="${basedir}/WebRoot"
webXmlFragment="${dir.WEB-INF}/generated_web.xml"
outputDir="${dir.WEB-INF}/src" />
How should I correct?
You need to make sure the jsp file imports the fn namespace of the JSTL. You'll need a line that looks something like this in your jsp file:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Also, you'll need to make sure the JSTL jars (jstl.jar and standard.jar) are in your classpath when jasper tries to compile.
Asaph's comments are spot on.
There's one other bit to check: The <fn> tag set was a later addition to the JSTL libraries. Maybe you have an older version of jstl.jar and standard.jar that needs to be updated.

Categories