Reputation: 1038
I would like to be able to use the JSP servlet on my JavaScript files for i18n purposes. Take the following JavaScript for example:
function you_did_it_wrong() {
alert("<fmt:message key="you.did.it.wrong" />");
}
I have tried to set up the JspServlet in my web.xml like this:
<servlet>
<servlet-name>preprocessor</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>preprocessor</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
But when I call the js file, it comes back without being processed by the servlet.
Upvotes: 1
Views: 2108
Reputation: 1108712
Bozho gave the right hint. However, I'd like to answer the concrete problem.
The given code snippet will fail when the fmt
taglib is not declared in top of file:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:setLocale value="${language}" />
<fmt:setBundle basename="com.example.i18n.text" />
So just ensure that it is there above in your JS file.
The JSP servlet entry looks fine, although I think I would rather have used just this:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
(jsp
is the servlet-name
of the Tomcat's builtin JspServlet
which you can locate in its /conf/web.xml
)
Upvotes: 2
Reputation: 597076
There are better ways to do that than serving .js files through the jsp servlet.
Check this question. I ended up having all variables declared in the .js
file, and having them passed through an init method:
init({somgMsg: '<fmt:.../>', anotherMsg: '<fmt:... />'});
Upvotes: 1