如何将javascript文件编译为jsp?
我希望能够在 JavaScript 文件上使用 JSP servlet 以实现国际化目的。以下面的 JavaScript 为例:
function you_did_it_wrong() {
alert("<fmt:message key="you.did.it.wrong" />");
}
我尝试在 web.xml 中设置 JspServlet,如下所示:
<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 处理。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
博佐给出了正确的暗示。不过,我想回答具体的问题。
当
fmt
标签库未在文件顶部声明时,给定的代码片段将失败:因此只需确保它位于 JS 文件的上方即可。
JSP servlet 条目看起来不错,尽管我想我宁愿只使用这个:
(
jsp
是 Tomcat 内置JspServlet 的
您可以在其servlet-name
/conf/web.xml
中找到它)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: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:
(
jsp
is theservlet-name
of the Tomcat's builtinJspServlet
which you can locate in its/conf/web.xml
)有比通过 jsp servlet 提供 .js 文件更好的方法来做到这一点。
检查这个问题。我最终在 .js 文件中声明了所有变量,并让它们通过 init 方法传递:
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: