Struts 2 Action tags, retrieving properties

萝らか妹 提交于 2019-12-02 14:22:57

问题


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" />

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>

How do I access myVariable when using an action tag like above? I tried <s:property value="myVariable" /> but it doesn't work.


回答1:


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" />

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:

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" />

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



来源:https://stackoverflow.com/questions/12149190/struts-2-action-tags-retrieving-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!