如何使用 ognl 更改会话范围属性?

2022-01-16 00:00:00 session java jsp struts2 ognl

是否可以使用 ognl 更改会话范围属性?

Is it possible to change session scope properties using ognl?

例如,如果我的会话中有一个名为 PROCESS_CONFIG 的属性,它是一个具有属性 name 的对象,那么如何更改此属性 name 在 JSP 上?

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 对象是我操作的属性.例如,鉴于前面的代码片段,PROCESSNAMEPROCESS_CONFIG 对象的一个​​属性,我想在 PROCESS_CONFIG 对象中自动更新它而不是在我的操作上有一个 PROCESSNAME 属性,然后必须在我的操作上明确地设置 PROCESSNAMEPROCESS_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 并查看官方 site 获取描述和用法,并阅读 我们如何从常见问题解答中访问会话.

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.

相关文章