Java Struts call another action after executed action finished - java

I'm using Java and Struts2.
In my struts.xml configure file i have blow config.
<action name="copyTestSuite" class="testSuiteAction"
method="copyTestSuite">
<result name="success">/WEB-INF/jsp/FormSuccessfulWithOutCloseWindow.jsp
</result>
</action>
After i executed action copyTestSuite, the default page will go to FormSuccessfulWithOutCloseWindow.jsp. and this page is just used to redirect to previous page.
But i want to let it go to another action matched page directly. How can i do this?
Below is another action toUpdateTestSuite i want to redirect to:
<action name="toUpdateTestSuite" class="testSuiteAction"
method="toUpdateTestSuite">
<result name="success">/WEB-INF/jsp/testsuite/updateTestSuite.jsp
</result>
</action>

You can use this <result type="redirectAction"></result>
<action name="copyTestSuite" class="testSuiteAction"
method="copyTestSuite">
<result type="redirectAction">/newAction.action</result>
</action>

You can redirect using something like:
<action name="copyTestSuite" class="testSuiteAction" method="copyTestSuite">
<result type="redirectAction">
<param name="actionName">toUpdateTestSuite</param>
</result>
</action>

Related

struts.xml namespace causes 404 not found page

I have the problem on struts.xml of Struts 2 web project.
I have the login page and after login, the URL will add /secure/*.action.
I tried to have two packages in struts.xml. One is normal / and second is /secure. But it will cause 404 not found page after login. If I make the second package also \ in namespace and call the membersite.action with only / namespace , it will be OK and successfully direct but with only /*.action in URL. I need to have this URL /secure/*.action in infrastructure issue. Is there any hints?
<package name="awip" namespace="/" extends="tiles-default, struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<global-results>
<result name="sessionTimeout" type="tiles">.logon.LogonForm</result>
<result name="errorRedirect" type="tiles">.errorPage</result>
</global-results>
<action name="logon" class="logonAction" method="displayLogonForm">
<result name="displayLogonForm" type="tiles">.logon.LogonForm</result>
</action>
<action name="doLogon" class="logonAction" method="doLogon">
<result name="displayLogonForm" type="tiles">.logon.LogonForm</result>
<result name="displayMainPage" type="redirectAction">
<param name="namespace">/secure</param>
<param name="actionName">membersite.action</param>
</result>
</action>
</package>
<package name="secureAwip" namespace="/secure" extends="tiles-default, struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<global-results>
<result name="sessionTimeout" type="tiles">.logon.LogonForm</result>
<result name="errorRedirect" type="tiles">.errorPage</result>
</global-results>
<action name="membersite" method="unspecified" class="membersiteAction">
<result name="displayMembersiteMain" type="tiles">.membersite.Main
</result>
</action>
</package>
The action name should not have an extension .action. If you write it with the action name the action mapper can't find the appropriate mapping for the action's result.
The code
<param name="actionName">membersite.action</param>
should be changed to
<param name="actionName">membersite</param>

How can I map the root path of my web application using struts2?

When I run my strust2 web application, I want to execute an action for the root path [/].
It would be something like an action with no name or just "/", something like:
<action name="/" class="ControllerName" method="execute">
<result name="success">ShowTheFirstPageAfterTheAction.jsp</result>
</action>
...
<struts>
...
<package name="user" namespace="/" extends="struts-default">
<action name="" class="ControllerName" method="execute">
<result name="success">ShowTheFirstPageAfterTheAction.jsp</result>
</action>
// Other actions depending on how you've designed your application
<package
// Other packages (also) depending on how you've designed your application
</struts>

Struts2 param within result not working

It seems I should be able to have parameters in strut.xml defined PER result, and not globally, but I cannot get it to work. Here is what does work :
<action name="actThing" class="Thing" method="execute">
<interceptor-ref name="newStack" />
<param name="parentObject">Parent</param>
<result name="Edit">jspEditThing.jsp</result>
<result name="Add">jspAddThing.jsp</result>
</action>
In this case when Thing.execute gets called, the parentObject variable is set. But here :
<action name="actThing" class="Thing" method="execute">
<interceptor-ref name="newStack" />
<result name="Add">
<param name="location">jspAddThing.jsp</param>
<param name="parentObject">Parent</param>
</result>
<result name="Edit">jspEditThing.jsp</result>
</action>
it does not. Since it works in the first case I certainly have the proper settings/getters, and I don't get any kind of error. What am I missing?
Thanks.
It does not work, and it shouldn't because parameters are applied to the result, not the action. The result is executed after the action, and all parameters should be already set.
The param tag sets a property on the Result object. The most commonly-set property is location, which usually specifies the path to a web resources. The param attribute is another intelligent default.
<action name="actThing" class="Thing" method="execute">
<result name="Add">
<param name="includeProperties">Parent.*</param>
</result>
</action>
Now your Parent object with all its attributes and child objects will be available.

Struts 2 Actions Redirect

I am using Struts 2.
localhost:8084/Web/viewProductsAction?idProducts=1
After I see this product, I leave a comment
And it successful but I got moved to
localhost:8084/Web/listProductsAction
Here is code in struts.xml:
<action name="listProductsAction" class="com.struts2.action.ProductsAction" method="viewProducts">
<result name="success">/products.jsp</result>
</action>
<action name="sendComments" class="com.struts2.action.CommentsAction" method="sendComments">
<result name="success" type="redirect">listProductsAction</result>
</action>
But I want after leave a comment, it forward to
localhost:8084/Web/viewProductsAction?idProducts=1
How can I do that?
Change the location of the redirect result to redirectAction result and location to viewProductsAction. After the post it will forward to the previous page.
<action name="sendComments" class="com.struts2.action.CommentsAction" method="sendComments">
<result name="success" type="redirectAction">viewProductsAction?idProducts=1</result>
</action>

Struts 2 remove .action extension when redirect

Before starting, I'm sorry for my bad English, because English is not my first language. However, I have a problem with Struts 2 redirecting.
One part of my Struts.xml file is like this:
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="Action.loginAction">
<result name="success" type="redirectAction">
<param name="actionName">search</param>
<param name="namespace">/</param>
</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
I have a loginAction that's responsible for logging in the user (no matter what is the content of this file). When the result of this action is success, I want to redirect to search action. Everything works fine, but my problem is, when I redirect to search action, the URL looks like this: http://localhost:8080/MyWebApplication/search.action
I want to have the .action removed from the URL, when redirecting to a particular action. What do I need to change to fix this?
Thanks for your attention.
By default struts2 framework will add .action you can override it by following configuration.
<constant name="struts.action.extension" value=""/>
For ref:
http://struts.apache.org/release/2.3.x/struts2-core/apidocs/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.html

Categories

Resources