问题
Is it possible to change session scope properties using ognl
?
For example, if I have on my session an attribute called PROCESS_CONFIG
which is an object with an attribute name
, how can one change this attribute name
on a JSP?
I've tried the following but it doesn't work:
<s:textfield value="%{#session.PROCESS_CONFIG.name}" id="PROCESSNAME" name="#session.PROCESS_CONFIG.name"/>
When I submit the form and access the session object in my action, through ServletActionContext.getRequest().getSession().getAttribute("PROCESS_CONFIG")
, the attribute name
has not changed.
EDIT:
The object saved in session as PROCESS_CONFIG
, is a very deep complex object (composed by numerous references to other objects, with lists of lists of objects) and on my view I just want to present a very tiny subset of its attributes (including attributes from its composed objects). So, polluting my JSP with all other fields as hidden is impractical! The view in question is a form where one can change the value of those fields and I would like to directly and automatically update the object saved on my struts 2 session, PROCESS_CONFIG
, as if PROCESS_CONFIG
object was a property of my action. For example, given the previous code snippet, PROCESSNAME
is an attribute of PROCESS_CONFIG
object and I would like to update it automatically in PROCESS_CONFIG
object instead of having an PROCESSNAME
property on my action and then having to explicitly do the setting of PROCESSNAME
on my
PROCESS_CONFIG
object.
回答1:
The session in S2 is a map where you could put the attributes before you use it with OGNL in the JSP. To have this working around let your action implement the SessionAware
and look at the official site for the description and usages, and read How do we access to the session from the FAQ.
To your question: why didn't you get the attribute in JSP. Because you are using S2 and OGNL to get it (via #session
reference) and you didn't put the attribute to S2 session. S2 session implementation differs from the standard http session. However, if you set attribute to the standard http session you can still access it in JSP 2.0 manner. The opposite is also true.
来源:https://stackoverflow.com/questions/14650499/how-to-change-session-scope-properties-using-ognl