When using struts2 I can access my session attributes inside struts2 tags using OGNL expression like this:

#session['object_key']

The problem is that I use this expression on different pages, every time hardcoding that 'object_key' string. Is there any way I can use any sort of named constant, so that I can easily change the key used to store my object in session map?

有帮助吗?

解决方案

Sure, use a static constant; there are several options for how to go about this.

Default, access using fully-qualified name:

<s:property value="#session[@app.Constants@OBJECT_KEY]" />

For details, see Accessing static properties in the OGNL Basics page.

Another trick is to set up an interceptor that pushes a class of constants onto the value stack, with constant-"looking" getters. You could do the same in an action as well, for example, a base action class could be used.

Then all that's needed in the JSP is this:

${session[OBJECT_KEY]}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top