Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

struts2 - Action redirect in struts.xml

Can I redirect to another action from within a struts actions ? So the result of an action is another action i.e - here is a snippet of struts.xml

    <action name="newRedirect" >
        <result>formsearch</result>
    </action>

    <action name="formsearch" class="com.event.action.SearchForm"
    method="execute">
        <result name="success">/form.jsp</result>
    </action>

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes. You can redirect and you can chain. Redirect starts from scratch, it is like you called the other action for the first time while chain keeps the values on the value stack and adds the variables of the new action.

To forward:

<action name="newRedirect" >
    <result type="redirect">/formsearch.action</result>
</action>

To chain:

<action name="newRedirect" >
    <result type="chain">formsearch</result>
</action>

As a convenience the redirect result type can be changed to a "redirectAction" result type... which lets us write:

 <action name="newRedirect" >
    <result type="redirectAction">formsearch</result>
</action>

This last one is probably what you want.

Now a warning, chaining/action redirection is up there with the "goto" statement. Not evil but easy to abuse, you should probably look to moving the deciding logic (the logic that determines what action to call of several to an interceptor) or if the logic is mostly setup related then some type of utility class that is invoked by the actions prepare method (or into the prepare method outright)... If the action needs parameters before prepare is called then use the paramsPrepareParamsStack.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...