通过EL 3.0访问JSP中的静态属性或方法(JEE7; Tomcat 8) [英] Access static property or method in JSP via EL 3.0 (JEE7; Tomcat 8)
问题描述
我正在使用tomcat 8.0.9(servlet 3.1,jsp 2.3,el 3.0)并尝试从jsp页面访问静态属性,如下所示:
I'm using tomcat 8.0.9 (servlet 3.1, jsp 2.3, el 3.0) and trying to access a static property from a jsp page like so:
${Boolean.TRUE}
没有错误,但是渲染结果中不显示任何输出。我究竟做错了什么?
There is no error, but no output appears in the rendered result. What am I doing wrong?
编辑
这个问题(标记为重复的问题)声称自从EL 3.0(JSR-341,Java EE 7的一部分),可以为所有java.lang。*类引用常量,因为它们是隐式导入的,并且可以这样使用
The answer to this question (marked as duplicate question) claims that since EL 3.0 (JSR-341, part of Java EE 7), it is possible to reference constants for all java.lang.* classes as they are implicitly imported and available like so
${Boolean.TRUE}
这个答案对我不起作用,至少不适用于tomcat 8。
This answer is NOT working for me, at least not with tomcat 8.
编辑2
来自 Oracle的JEE7教程(9.3.1.2引用对象属性或集合元素)
From Oracle's JEE7 Tutorial (9.3.1.2 Referencing Object Properties or Collection Elements)
您可以使用语法classname.field引用静态字段或方法,如下例所示:
Boolean.FALSE
classname是类wit的名称hout包名。默认情况下,将导入所有java.lang包。您可以根据需要导入其他包,类和静态字段。
You can reference a static field or method using the syntax classname.field, as in the following example:
Boolean.FALSE
The classname is the name of the class without the package name. By default, all the java.lang packages are imported. You can import other packages, classes, and static fields as needed.
推荐答案
更新:
Tomcat中存在一个错误(至少从8.0.9开始)jsp-api.jar。根据更改日志,它已在Tomcat 8.0版中修复。 15。
There is a bug in Tomcat's (at least as of 8.0.9) jsp-api.jar. According to the change log, it is fixed in Tomcat version 8.0.15.
作为一种解决方法,在apache-tomcat-8.0.9 \lib文件夹中将jsp-api.jar替换为 javax.servlet.jsp-api-2.3.2-b01.jar 。在eclipse中刷新项目,你会看到输出
As a workaround, in the apache-tomcat-8.0.9\lib folder replace jsp-api.jar with javax.servlet.jsp-api-2.3.2-b01.jar. Refresh the project in eclipse and you will see the output for
Testing: ${Boolean.TRUE}
as:
Testing: true
这被认定为GLASSFISH中的错误这里。
This was identified as a bug in GLASSFISH as well here.
为了访问以外的静态字段或方法 java.lang
包,必须将这些特定的包或类添加到EL上下文中(BalusC也讨论过这里)。
In order to access static fields or methods outside of the java.lang
package, those specific packages or classes must be added to the EL context (also discussed by BalusC here).
这是一个允许静态访问Web应用程序中所有jsp文件的 java.time
包中的类的示例:
Here's an example allowing static access to classes in the java.time
package for all jsp files in your web application:
@WebListener
public class Config implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
JspFactory.getDefaultFactory().getJspApplicationContext(event.getServletContext()).addELContextListener((ELContextEvent e) -> {
e.getELContext().getImportHandler().importPackage("java.time");
});
}
@Override
public void contextDestroyed(ServletContextEvent event) {}
}
现在从jsp返回当前的LocalDate,例如:
And now from the jsp, to return the current LocalDate, for example:
$ { LocalDate.now()}
请注意 $ {java.time.LocalDate.now()}
不起作用。
这篇关于通过EL 3.0访问JSP中的静态属性或方法(JEE7; Tomcat 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!