tau*_*ino 102 jsp constants el
如何在JSP页面上引用EL常量?
我有一个Addresses名为常量的接口URL.我知道我可以通过以下方式用scriplet引用它:<%=Addresses.URL%>,但是我如何使用EL来做到这一点?
Bal*_*usC 151
如果您已经使用Java EE 7/EL 3.0,那么它@page import还将导入EL范围中的类常量.
<%@ page import="com.example.YourConstants" %>
Run Code Online (Sandbox Code Playgroud)
这将在封面下导入ImportHandler#importClass()并作为${YourConstants.FOO}.
请注意,所有java.lang.*的类都已经隐式导入的和可用的,像这样${Boolean.TRUE}和${Integer.MAX_VALUE}.这只需要一个更新的Java EE 7容器服务器,因为早期版本中存在错误.例如GlassFish 4.0和Tomcat 8.0.0-1x失败,但GlassFish 4.1+和Tomcat 8.0.2x +工作.并且您需要确保web.xml声明符合服务器支持的最新servlet版本.因此,web.xml声明符合Servlet 2.5或更早的版本,Servlet 3.0+功能都不会起作用.
另请注意,此工具仅在JSP中可用,而不在Facelets中.对于JSF + Facelets,您最好的选择是使用OmniFaces<o:importConstants>,如下所示:
<o:importConstants type="com.example.YourConstants" />
Run Code Online (Sandbox Code Playgroud)
或者添加一个EL上下文监听器,其调用ImportHandler#importClass()如下:
@ManagedBean(eager=true)
@ApplicationScoped
public class Config {
@PostConstruct
public void init() {
FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() {
@Override
public void contextCreated(ELContextEvent event) {
event.getELContext().getImportHandler().importClass("com.example.YourConstants");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是不是可以在EL 2.2及以上.有几种选择:
将它们放入Map<String, Object>您放入应用程序范围的位置.在EL,地图的值是通过访问常用的Javabean方式${map.key}或${map['key.with.dots']}.
使用<un:useConstants>的的非标标签库(maven2的回购这里):
<%@ taglib uri="http://jakarta.apache.org/taglibs/unstandard-1.0" prefix="un" %>
<un:useConstants className="com.example.YourConstants" var="constants" />
Run Code Online (Sandbox Code Playgroud)
通过这种方式,他们可以通过常用的Javabean方式访问${constants.FOO}.
使用Javaranch的CCC <ccc:constantsMap>,如本文底部所描述的那样.
<%@ taglib uri="http://bibeault.org/tld/ccc" prefix="ccc" %>
<ccc:constantsMap className="com.example.YourConstants" var="constants" />
Run Code Online (Sandbox Code Playgroud)
通过这种方式,它们也可以通过常用的Javabean方式访问${constants.FOO}.
如果您正在使用JSF2,那么你可以使用<o:importConstants>的OmniFaces.
<html ... xmlns:o="http://omnifaces.org/ui">
<o:importConstants type="com.example.YourConstants" />
Run Code Online (Sandbox Code Playgroud)
通过这种方式,它们也可以通过常用的Javabean方式访问#{YourConstants.FOO}.
创建一个包装类,通过Javabean样式的getter方法返回它们.
创建自定义EL解析器,首先扫描常量的存在,如果不存在,则委托默认解析器,否则返回常量值.
anr*_*nre 11
以下内容一般不适用于EL,而是仅适用于SpEL(Spring EL)(在Tomcat 7上使用3.2.2.RELEASE进行测试).我认为值得一提的是,如果有人搜索JSP和EL(但是使用JSP和Spring).
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<spring:eval var="constant" expression="T(com.example.Constants).CONSTANT"/>
Run Code Online (Sandbox Code Playgroud)
您通常将这些常量放在Configurationservlet上下文中的对象(具有getter和setter)中,并使用${applicationScope.config.url}
EL中无法访问静态属性.我使用的解决方法是创建一个非静态变量,将自己分配给静态值.
public final static String MANAGER_ROLE = 'manager';
public String manager_role = MANAGER_ROLE;
Run Code Online (Sandbox Code Playgroud)
我使用lombok来生成getter和setter,这样就可以了.你的EL看起来像这样:
${bean.manager_role}
Run Code Online (Sandbox Code Playgroud)
完整代码,网址为http://www.ninthavenue.com.au/java-static-constants-in-jsp-and-jsf-el
我执行了如下:
public interface Constants{
Integer PAGE_SIZE = 20;
}
Run Code Online (Sandbox Code Playgroud)
-
public class JspConstants extends HashMap<String, String> {
public JspConstants() {
Class c = Constants.class;
Field[] fields = c.getDeclaredFields();
for(Field field : fields) {
int modifier = field.getModifiers();
if(Modifier.isPublic(modifier) && Modifier.isStatic(modifier) && Modifier.isFinal(modifier)) {
try {
Object o = field.get(null);
put(field.getName(), o != null ? o.toString() : null);
} catch(IllegalAccessException ignored) {
}
}
}
}
@Override
public String get(Object key) {
String result = super.get(key);
if(StringUtils.isEmpty(result)) {
throw new IllegalArgumentException("Check key! The key is wrong, no such constant!");
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
下一步将此类的实例放入servlerContext
public class ApplicationInitializer implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setAttribute("Constants", new JspConstants());
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
Run Code Online (Sandbox Code Playgroud)
将侦听器添加到web.xml
<listener>
<listener-class>com.example.ApplicationInitializer</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
在jsp中访问
${Constants.PAGE_SIZE}
Run Code Online (Sandbox Code Playgroud)
我一开始就在我的 jsp 中定义了一个常量:
<%final String URI = "http://www.example.com/";%>
Run Code Online (Sandbox Code Playgroud)
我在我的 JSP 中包含了核心标签库:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Run Code Online (Sandbox Code Playgroud)
然后,我通过以下语句使常量可用于 EL:
<c:set var="URI" value="<%=URI%>"></c:set>
Run Code Online (Sandbox Code Playgroud)
现在,我可以稍后使用它。这是一个示例,其中该值仅作为 HTML 注释写入以用于调试:
<!-- ${URI} -->
Run Code Online (Sandbox Code Playgroud)
使用常量类,您只需导入类并将常量分配给局部变量。我知道我的回答是一种快速的技巧,但是当人们想直接在 JSP 中定义常量时,这个问题也会出现。
| 归档时间: |
|
| 查看次数: |
66227 次 |
| 最近记录: |