Hi everybody,
I use digester in my application to access the constants in the whole
application. This is what I've done:
lvb_digester_rules.xml
________________________
<digester-rules>
<object-create-rule pattern="lv-beans"
classname="java.util.ArrayList"/>
<pattern value="lv-beans/lv-bean">
<object-create-rule
classname="org.apache.struts.util.LabelValueBean" />
<set-properties-rule />
<set-next-rule methodname="add" />
</pattern>
</digester-rules>
struts-config.xml
__________________________
<plug-in className="org.apache.struts.plugins.DigestingPlugIn">
<set-property property="key" value="serverConstants"/>
<set-property property="configPath"
value="/WEB-INF/server-constants.xml"/>
<set-property property="digesterPath"
value="/WEB-INF/lvb-digester-rules.xml"/>
</plug-in>
server-constants.xml
___________________________
<lv-beans>
<lv-bean label="USER_KEY" value="UserBean id in session" />
<lv-bean label="MAX_INACTIVE_SESSION_INTERVAL" value="600" />
</lv-beans>
This is the class used to read a value for a label in the
server-constants.xml
public class Constants {
public static String getConstant(ServletConfig servletConfig,
String key) {
ServletContext servletContext =
servletConfig.getServletContext();
ArrayList lvArray =
(ArrayList)
servletContext.getAttribute("serverConstants");
// LabelValueBeans are equal if their values are both
null or equal.
Iterator iterator = lvArray.iterator();
LabelValueBean lvBean;
while (iterator.hasNext()) {
lvBean = (LabelValueBean) iterator.next();
if (lvBean.getLabel().equals(key)) {
return lvBean.getValue();
}
}
return null;
}
}
By doing this way, if I want to add more constants to my application, I
just need to put them in the server-constants.xml file (i.e. no change
in Java code).
Now, I put a String object in the session like this:
session.setAttribute(Constants.getConstant(servletConfig, "USER_KEY"),
myStringObj);
In Java code, I can get that String object with:
Object obj = session.getAttribute(Constants.getConstant(servletConfig,
"USER_KEY"));
How can I print out that object in jsp file with bean:write? I tried
<bean:write name='Constants.getConstant(servletConfig, "USER_KEY")'/>,
but it doesn't work. Do you know how to work around this problem? Or do
you know any other implementation of using constants in Struts
applications?
Sincerely,
Thai Dang Vu
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]