简体   繁体   English

有没有办法将OGNL与Struts2 UI标记绑定

[英]Is there a way to bind OGNL with Struts2 UI tags

I'm developing a webapp using Struts2. 我正在使用Struts2开发一个webapp。 I used to work with Struts1 我曾经使用过Struts1

is there a way to reference a session object directly to the JSP so that if I change the value in the formular, the value in the referenced session object will also be updated. 有没有办法直接将会话对象引用到JSP,这样如果我更改了公式中的值,引用的会话对象中的值也将更新。

<s:textfield name="%{#session.order.amount}"/>

for eg I have a session object order which has attribute amount with getter and setter. 例如,我有一个会话对象订单,其中包含具有getter和setter的属性数量。

it seems like, after I put some values into the textfield and then submit the page, the value in the session didn't get updated. 看起来,在我将一些值放入textfield然后提交页面后,会话中的值没有得到更新。

currently I'm using another approach which involves session.put() inside the setter of an action attributes. 目前我正在使用另一种方法,它涉及动作属性的setter中的session.put() Personally I dislike my current solution. 我个人不喜欢我目前的解决方案。

You could try having the name be the string session.order.amount and make your action implement SessionAware, and expose the session. 您可以尝试将名称作为字符串 session.order.amount ,并使您的操作实现SessionAware,并公开会话。 Currently you're setting the name to the value of session.order.amount . 目前,您将名称设置为session.order.amount

I don't know if it would work, and you might need to use array/collection notation, but off the top of my head I don't know why it wouldn't work. 我不知道它是否会工作,你可能需要使用数组/集合符号,但是从我的头顶,我不知道为什么它不会工作。

That said, I feel like direct view-layer writes into web app internals is a Bad Idea. 也就是说,我觉得直接视图层写入 Web应用程序内部是一个坏主意。

From the Struts1 where form bean placed to the session scope by default or just by setting an attribute value scope="session" and everything was good. 从Struts1默认情况下将表单bean放置到会话范围,或者只是通过设置属性值scope="session" ,一切都很好。

In the Struts2 you have not form beans nor session scoped beans. 在Struts2中,您没有形成bean或会话范围的 bean。 And to use session scoped beans you need to either implement it yourself, or use other frameworks like Spring, Guice or CDI where session scoped is implemented and available to user. 要使用会话范围的 bean,您需要自己实现它,或者使用其他框架,如Spring,Guice或CDI,其中实现了会话作用域并且可供用户使用。

On the other hand Struts2 is in heavy use of interceptors, which provide you additional features like scope interceptor , or scopedModelDriven interceptor that allows you to put some action properties to the session scope. 另一方面,Struts2大量使用拦截器,它为您提供了scope拦截器scopedModelDriven拦截器等附加功能,允许您将某些操作属性放入会话范围。 It will initialize the properties each time the action is executed referencing objects are put on the session scope. 每次执行操作时,它都会初始化属性,引用对象放在会话范围上。

Without this helper interceptors you could always intercept a session object by implementing SessionAware (see How do we get access to the session ) and initialize the properties somewhere when the action is executed, ie prepare() method, because it's comming after the session map is injected. 如果没有这个辅助拦截器,你总是可以通过实现SessionAware拦截会话对象(参见我们如何获得对会话的访问权限 )并在执行动作时初始化某些属性,即prepare()方法,因为它在会话映射之后出现注射。 Putting initializer to the accessors is a bad idea. 将初始化程序放入访问器是一个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM