Struts 2 Action标签,检索属性 [英] Struts 2 Action tags, retrieving properties
问题描述
如果我有一个典型的设置,其中一个动作转发到JSP,我会请求我的属性如下:
If I had a typical setup with an action that forwards to the JSP I would request my properties like so:
< s: property value =myVariable/>
其中myVariable是动作中的变量。
where myVariable is a variable in the action.
我想在另一页中使用这样的动作标签:
I want to use action tags like this in another page:
<s:action name="actionName" executeResult="false">
<s:param name="switch">true</s:param>
</s:action>
使用时如何访问 myVariable
动作标签如上?我尝试了< s:property value =myVariable/>
但它不起作用。
How do I access myVariable
when using an action tag like above? I tried <s:property value="myVariable" />
but it doesn't work.
推荐答案
当你写< s:property value =myVariable/>
时,Struts会查找属性 myVariable
在其价值堆栈中。当前操作在ValueStack中,这就是典型设置的原因。现在,在
When you write <s:property value="myVariable" />
, Struts looks for the property myVariable
in its "Value Stack". The current action is in the ValueStack, and that's why the typical setup works. Now, in the case of
<s:action name="actionName" executeResult="false">
<s:param name="switch">true</s:param>
</s:action>
<s:property value="myVariable" />
执行最后一行 actionName
动作已经执行,当前动作不是主动(外部)动作。如果你想访问内部动作的属性,你有几个选择,其中两个显示在 docs :
when the last line is executed the actionName
action has already executed, the current action is not that but the main ("outer") one . If you want to acces properties of your "inner" action, you have several alternatives, two of them are shown in the docs:
添加 var
属性以便执行(内部)操作被分配给变量并使用#syntax 引用它:
Either add the var
attribute so that the executed (inner) action is assigned to a variable and reference it with the # syntax:
<s:action name="actionName" var="innerAction" executeResult="false">
<s:param name="switch">true</s:param>
</s:action>
<s:property value="#innerAction.myVariable" />
或者,在您的操作方法中,将您的属性值显式添加到某个范围(例如:attribute)
Or, in your action method, add your property value explicitly to some scope ( eg: attribute)
// in your inner action:
ServletActionContext.getRequest().setAttribute("myVariable", "blah blah");
<s:property value="#attr.myVariable" />
免责声明:我没有测试过这个
Disclaimer: I've not tested this
这篇关于Struts 2 Action标签,检索属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!