WebWork中如何利用定义在JAVA文件中的常量?
比如,在MyConstants.java中,定义有如下常量
public static final String HIGH="H";
public static final String MIDDLE="M";
public static final String LOW="L";
在某JSP页面中,应用WebWork的Tag输出如下信息:
<ww:iterator value="eventList">
<ww:if test="eventPriority=='H'">
High<br>
</ww:if>
...
</ww:iterator>
这样就有Hard Code的问题,如何把上面'H'换成MyConstants.HIGH呢?
在OGNL的表达式语法中,可以用如下方法来引用常量:
@full.path.to.MyClass@myStaticField
因此,上面的<ww:if test="eventPriority=='H'>可以改写成这个样子:
<ww:if test="eventPriority==@mypackge.MyConstants@HIGH">
当然,也有人用下面的方法(不推荐,这样就加入了JSP 的 ScriptLet了):
<%
mypackge.MyConstants myConstants = new mypackge.MyConstants();
%>
<ww:if test="eventPriority == '<% myConstants.HIGH%>'">
High<br>
</ww:if>
http://lvxy.spaces.live.com/blog/cns!49386EA353D8F86F!109.trak
比如,在MyConstants.java中,定义有如下常量
public static final String HIGH="H";
public static final String MIDDLE="M";
public static final String LOW="L";
在某JSP页面中,应用WebWork的Tag输出如下信息:
<ww:iterator value="eventList">
<ww:if test="eventPriority=='H'">
High<br>
</ww:if>
...
</ww:iterator>
这样就有Hard Code的问题,如何把上面'H'换成MyConstants.HIGH呢?
在OGNL的表达式语法中,可以用如下方法来引用常量:
@full.path.to.MyClass@myStaticField
因此,上面的<ww:if test="eventPriority=='H'>可以改写成这个样子:
<ww:if test="eventPriority==@mypackge.MyConstants@HIGH">
当然,也有人用下面的方法(不推荐,这样就加入了JSP 的 ScriptLet了):
<%
mypackge.MyConstants myConstants = new mypackge.MyConstants();
%>
<ww:if test="eventPriority == '<% myConstants.HIGH%>'">
High<br>
</ww:if>
http://lvxy.spaces.live.com/blog/cns!49386EA353D8F86F!109.trak
本文介绍如何在WebWork框架的JSP页面中使用定义于JAVA文件中的常量,通过OGNL表达式引用这些常量,避免硬编码问题。

被折叠的 条评论
为什么被折叠?



