Struts 2 with same action but different method - struts2

I'm migrating my application from Struts1 to 2.5. All the navigation url is defined as per below :
../menumanager.do?method=adduser
../menumanager.do?method=deleteuser
In struts 1 we extend dispather action class and reading the method name from query parameter. Using action forward we are redirecting to particular method.
Is there any way in struts 2, we can define same action name in struts.xml and besed on method name in the url we will redirect to paricular method.
I tried below code. When I hit ../menumanager.do?method=adduser it is going to last method of action class.
<action name="menumanager" method="adduser" class="X.Action">
<result name="login">Some JSP</result>
<result name="logout">Some JSP</result>
</action>
<action name="menumanager" method="deleteuser" class="X.Action">
<result name="login">Some JSP</result>
<result name="logout">Some JSP</result>
</action>

Related

Struts won't pass parameter to jsp file

I have an action which (depending on the result) redirects to a suitable file:
<!-- /web/addaccount -->
<action name="addaccount" class="com.x.y.z.WebCreateAccountAction">
<result name="INVALIDLOGIN">/delete/confirm.jsp?err=SIGNIN</result>
<result name="ERROR">/delete/error.html</result>
</action>
For some reason, when redirecting to delete/confirm.jsp, the parameter erris not passed in. I'm at a loss to why this is happening. Is this an incorrect way of passing params to JSP via Struts?
Was just missing a:
type="redirect"
<result name="INVALIDLOGIN" type="redirect">/delete/confirm.jsp?err=SIGNIN</result>
Note: This is advised against, it would redirect directly to a JSP, which would be considered an S2 anti-pattern. See comments below.

How to redirect with parameter

<action name="AddedPaid" class="iland.payment.SupplierPaidAction" method="insert">
<result name="success" type="redirect">ShowPaid</result>
<result name="input">/pages/payment/addToPay.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
<action name="ShowPaid" class="iland.payment.SupplierPaidAction" method="fetchAllByfk">
<result name="success">/pages/paid/showPaidDetails.jsp</result>
<result name="input">/pages/payment/ShowPay.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
Here AddedPaid Action is used to add form data in to database.
After adding data in to database I am redirecting result to ShowPaid action.
This is working properly.
Now I want whenever I redirect AddedPaid action to ShowPaid.
ShowPaid must show data of perticular supplierPaymentId for which I have added data in AddedPaid.
After redirect it is howing url
http://localhost:8082/ClothStore/ShowPaid
I want
http://localhost:8082/ClothStore/ShowPaid?supplierPaymentId=4
It's strange, usually people have 2 and want 1 :)
Btw, since you are using PostRedirectGet, you need to manually pass the parameter in Struts configuration.
Assuming you have a variable named supplierPaymentId with getter and setter, it's achievable like follows:
<action name="AddedPaid" class="iland.payment.SupplierPaidAction" method="insert">
<result name="success" type="redirectAction">
<param name="actionName">ShowPaid</param>
<param name="supplierPaymentId">${supplierPaymentId}</param>
</result>
<result name="input">/pages/payment/addToPay.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
Also use redirectAction instead of redirect, that is meant to be used to redirect to external URLs or non-Action URLs
First of all use redirectAction result instead of redirect to redirect to another action.
And use param tag to add parameters in your result configuration.
<result type="redirectAction">
<param name="actionName">ShowPaid</param>
<param name="supplierPaymentId">${supplierPaymentId}</param>
</result>
Note you need to have getter/setter for supplierPaymentId in your action class.

Create multiple actions in Struts2

How to create multiple actions in Struts2 in single action class?
Please give example ?
I found following code on searching internet but it is giving errors OR Do I need to write separate action class for every request ?
Beside what #Quaternion said, S2 actions are in itself java class and i am not sure what you are asking (multiple actions in Struts2 in single action class).
We have several ways to use a single S2 action class here are few of them
Map single action class with different alias like.
<action name="A" class="MyActionClass">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="B" class="MyActionClass">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
But i believe you want to map different action request to different action method. S2 provides a way to define any number of methods and we can tell S2 to call which method on which action class from UI.
for e.g let's say we have a UserAction class which is responsible for handling user interaction like
Login
Logout
Registration
for this we need not to create different Action classes but we can create a single action class say UserAction and can define different methods in side it and can configure S2 to call different methods like
<action name="Logon" class="UserAction" method="logon">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="Logout" class="UserAction" method="logout">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="Register" class="tUserAction" methood="register">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
Hope this might help you to clear your doubts
In above use case MyActionClass has been mapped with two alias A and B and you can map to any number.
I got your question exactly that you want to write multiple redirects in single action based on action selected like add/edit/delete code at single place. You should look for DispatchAction for your requirement.
Following are few examples you can look at, all provides how to implement DispatchAction.
Link1
Link2
Link3
Another method:
Here is the action class with 3 actions (execute, action1, action2)
public class MyAction extends ActionSupport{
public String execute(){
return SUCCESS;
}
public String action1(){
return SUCCESS;
}
public String action2(){
return SUCCESS;
}
}
Here is config:
<action name="myAction" class="MyAction">
<result>showMe.jsp</result>
<result name="input">showMe.jsp</result>
</action>
Action "execute" will be called by default.
To call action "action1" or "action2" you must put parameter in your request whith name "method:action1" or "method:action2".
Call default action (execute): /path_to_action/myAction.action
Call action1: /path_to_action/myAction.action?method:action1
Call action2: /path_to_action/myAction.action?method:action2
Your can change default method:
<action name="myAction" class="MyAction" method="action1">
<result>showMe.jsp</result>
<result name="input">showMe.jsp</result>
</action>
So, when your call /path_to_action/myAction.action, action1 will be executed.

I want to call/redirect to a Portal page from Struts2Portlet Action Class?

Hi We are developing a Struts2Portlet Application in WebSpherePortal6.0.1. In my application I want to redirect to another portlet page after fullfilling the validations in my struts action class.How to achieve it. Please help me.
Thanks in Advance.
I have done some R&D to fix this issue,finally i found a solution.We cant call/redirect the portal page from struts2Portlet Action class.We can have to give a result type in strut action class,then we have to configure the action in the result.
<action name="view" class="com.ibm.rock.ViewAction" method="prepareview">
<result name="view">*/view/viewportalpage.action*</result>
</action>
<action name="viewportalpage" class="com.ibm.rock.ViewAction" method="prepareview">
<result name="preview">/_Rock/jsp/html/Preview.jsp</result>
</action>
Synatax:
<result name="success">*/Namespace/view.action*</result>
<action name="view" class="com.ibm.rock.ViewAction" method="prepareview">
<result name="preview">/_Rock/jsp/html/Preview.jsp</result>
</action>

Please explain Action in Struts 2.0 XML file

<action name="userLogin" class="com.cc.ad.web.common.UserLoginAction">
<result name="error">/user-login.jsp</result>
<result name="redirect" type="redirect">${retUrl}</result>
<result name="customerRedirect" type="redirect">${customerRedirect}</result>
<result name="supplierRedirect" type="redirect">${supplierRedirect}</result>
<result name="diamondViewRedirect" type="redirect">${diamondViewRedirect}</result>
<result name="supplierPopupRedirect" type="redirect">${supplierPopup}</result>
<result name="customerPopupRedirect" type="redirect">${customerPopup}</result>
</action>
Above I show one of many things written in my Struts XML file. Here nothing is define in the method attributes so I am confused about which method of UserLoginAction class is called when this action is called.
If method name is not specified in the action configuration, Struts will look for a method named execute by default.
Struts 2.0 Action Configuration Doc

Resources