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"
}
}
Related
<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.
im in learning struts 2.
i create a simple project that can add and list the PRODUCT that user add. list is my first page and show all product that added. it is my struts.xml file:
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="listProduct" />
<action name="listProduct" class="control.ProductHandler" method="list">
<result name="success">/list.jsp</result>
</action>
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success">/listProduct</result>
</action>
<action name="*Form">
<result>{1}.jsp</result>
</action>
</package>
</struts>
when i enter localhost:8080/product in then browser it show me list.jsp file. in this file i have a link that with it i can goto add.jsp file. href of this link is addForm.
in add.jsp file i have 3 text field and a submit button. when entered data added action class return "success" and i most goto localhost:8080/product (default page) but it show me :
HTTP Status 404 - /product/listProduct
this is my action file:
public String list(){
products=Database.get();
return "success";
}
public String add(){
if (add!=null){
Product product=new Product(name, producer, price);
Database.add(product);
}
return "success";
}
where is my mistake?
thanks.
In order to redirect to another action use redirectAction result type.
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success" type="redirectAction">listProduct</result>
</action>
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
In servlets I used
response.sendRedirect("index.jsp?msg=Login failed");
msg after ? to send message while redirecting.
How to do same things with struts2
Since redirection is via struts.xml
<result name="error">/index.jsp</result>
I am just a beginner in struts framework
in your action class which extends ActionSupport:
addActionError("Login failed");
in your index.jsp:
<s:actionerror/>
<result name="error" type="redirect" >
<param name="location">index.jsp?msg=${actionMessage}</param>
<param name="parse">true</param>
<param name="encode">true</param>
</result>
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.