如何使用ognl更改会话范围属性? [英] How to change session scope properties using ognl?
问题描述
是否可以使用ognl
更改会话范围属性?
Is it possible to change session scope properties using ognl
?
例如,如果我在会话中有一个名为PROCESS_CONFIG
的属性,它是一个具有属性name
的对象,那么如何在JSP上更改此属性name
?
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"/>
当我提交表单并通过ServletActionContext.getRequest().getSession().getAttribute("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.
在会话中另存为PROCESS_CONFIG
的对象是一个非常深的复杂对象(由对其他对象的大量引用组成,并包含对象列表),在我看来,我只想呈现一个很小的对象属性的子集(包括来自其组合对象的属性).因此,用隐藏的所有其他字段污染我的JSP是不切实际的!该视图是一种可以更改这些字段的值的形式,我想直接自动更新保存在我的struts 2会话PROCESS_CONFIG
中的对象,就像PROCESS_CONFIG
对象是我的行为的财产.例如,给定前面的代码片段,PROCESSNAME
是PROCESS_CONFIG
对象的属性,我想在PROCESS_CONFIG
对象中自动更新它,而不是在操作中具有PROCESSNAME
属性,然后必须显式地执行我的PROCESSNAME
的设置
PROCESS_CONFIG
对象.
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.
推荐答案
S2中的会话是一个映射,您可以在其中将属性与JSP中的OGNL一起使用之前放置属性.要解决此问题,请让您的操作实现SessionAware
并查看官方的我们如何从常见问题解答访问会话.
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.
对于您的问题:为什么没有在JSP中获得该属性.因为您正在使用S2和OGNL来获取它(通过#session
引用),并且没有将属性放入S2会话中. S2会话的实现不同于标准的http会话.但是,如果将属性设置为标准的http会话,您仍然可以以JSP 2.0的方式访问它.反之亦然.
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.
这篇关于如何使用ognl更改会话范围属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!