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.
Related
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>
I have an application where I want to modify(or overwrite) action mapping in struts.xml with new entry.
For detailed explanation here is the scenario.
In my application I have different stages like registration, change_data, I am managing these using context Param.
Web.xml
<context-param>
<description>Current stage of the application</description>
<param-name>stage</param-name>
<param-value>registration</param-value>
</context-param>
I have an action which is redirect request for Payment Gateway i.e payment gateway is sending user to this action.
struts2.xml (demo entries)
<!-- for registration -->
<action name="paymentResponse" class="abc.xyz.PaymentResponse">
<result name="input" type="tiles">paymentForReg.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
<!-- for change data -->
<action name="paymentResponse" class="abc.xyz.PaymentResponseForChangeData">
<result name="input" type="tiles">paymentForChangeData.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
So when stage of the application changes, what I am doing is changes stage in web.xml and commenting one entry from of action from struts.xml
So to summarize I have multiple actions with same name and I want to trigger(or change class name of action) action on the basis of context param. is there any way to do it?
If i am not wrong i think you have to use dynamic action like below then you can use the same action and same entry in xml
struts.xml:
<action name="paymentResponse" class="abc.xyz.RedirectPaymentResponse" method="updateCriteria">
<result name="response" type="tiles">paymentForChangeData.tiles</result>
<result name="responsechanged" type="tiles">paymentForReg.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
Action class:
public class RedirectPaymentResponse extends AbstractAppAction {
public String execute () throws Exception
{
// some code
return "success";
}
public String updateCriteria(){
//logic here
if(PaymentResponse){
// code here
return "response";
}
if(PaymentResponseChanged){
// code here
return "responsechanged"
}
}
my task is on struts2 action chaining,my program name is chaining ,i am using correct syntax,but it is not working.
struts.xml
<action name="register" class="RegisterAction">
<result name="success" type="chain">login.action</result>
</action>
<action name="login" class="LoginAction" >
<result name="success">/login.jsp</result>
</action>
RegisterAction.java
public class RegisterAction {
public String execute() {
return "success";
}
}
LoginAction.java
public class LoginAction {
public String execute() {
return "success";
}
}
but when i run program,it gives the following error occurs
There is no Action mapped for namespace [/] and action name [login.action] associated with context path [/chaining].
Remove the suffix from your chained Action name, from this
<result name="success" type="chain">login.action</result>
to this
<result name="success" type="chain">login</result>
Note that Action Chaining is not recommended, Redirect Action or some other ways should be preferred.
From the official documentation:
Don't Try This at Home
As a rule, Action Chaining is not recommended. First explore other options, such as the Redirect After Post technique.
Take a look at this answer too: https://stackoverflow.com/a/4761955/1654265
i have done some pages with Struts 2.(J2EE project)
All was ok until i try to add an interceptor.
It seems that the Interceptor delete all properties of my Class Action and Parameters send by the jsp with url like: action?param=xxx
here is the interceptor:
public class SessionInterceptor extends AbstractInterceptor{
#Override
public String intercept(ActionInvocation invocation) throws Exception {
return invocation.invoke();
}
here is the struts.xml:
<action name="movefc_ShowFjt" class="struts2.ShowFjtAction" method="movefc">
<interceptor-ref name="sessionInterceptor"></interceptor-ref>
<result name="input" type="dispatcher">jsp/showFjt.jsp</result>
<result name="success" type="dispatcher">jsp/showFjt.jsp</result>
</action>
in the class action,
public class ShowFjtAction extends ActionSupport {
private String param;
private Personne p;
param property never receive value from the jsp (it is ok when interceptor is off). Worse, other properties in Class action seems to be erased.
Is that an normal effect of the return invocation.invoke(); of the interceptor ?
Is there anything i can do to fix that ?
y defining your own interceptor are you causing all of the default interceptors to be discarded?
Should you perhaps be defining an interceptor stack which includes your interceptor and the default stack?
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="sessionInterceptor" class="SessionInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="sessionInterceptor"/>
</interceptor-stack>
</interceptors>
<action name="movefc_ShowFjt"
class="struts2.ShowFjtAction">
<interceptor-ref name="myStack"/>
<result name="input" type="dispatcher">jsp/showFjt.jsp</result>
<result name="success" type="dispatcher">jsp/showFjt.jsp</result>
</action>
The entire concept is explained as follows
1] First when user does not writes any interceptors, then interceptors defined in struts-default.xml will be used. It is defined in struts-core.jar, it is accomplished by extending the "struts-default" extended in our package xml tag.
2] When user writes his own interceptor if you add one mode code block after sessionInterceptor ref name i.e interceptor-ref name="defaultStack" will solve your problem.
Befor trying this try to unzip the struts-core.jar and move forward with your implementation.
<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