如何将javascript文件编译为jsp? [英] How to compile a javascript file as a jsp?

查看:77
本文介绍了如何将javascript文件编译为jsp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将JavaScript文件上的JSP servlet用于i18n.以以下JavaScript为例:

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" />");
}

我试图像这样在我的web.xml中设置JspServlet:

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>

但是当我调用js文件时,它会返回,而不会被servlet处理.

But when I call the js file, it comes back without being processed by the servlet.

推荐答案

Bozho给出了正确的提示.但是,我想回答具体问题.

Bozho gave the right hint. However, I'd like to answer the concrete problem.

当未在文件顶部声明fmt taglib时,给定的代码片段将失败:

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" />

因此,只需确保它在JS文件中位于上方即可.

So just ensure that it is there above in your JS file.

JSP servlet条目看起来不错,尽管我想我会只使用以下内容:

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是Tomcat内置JspServletservlet-name,您可以在其/conf/web.xml中找到它)

(jsp is the servlet-name of the Tomcat's builtin JspServlet which you can locate in its /conf/web.xml)

这篇关于如何将javascript文件编译为jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆