Send URL Message as Parameters - struts2

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>

Related

How can I change action class entry in struts.xml dynamically?

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"
}
}

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.

Unable to map when file is not existed in file downloading in struts 2

I am developing a web application using struts2 .. In that I am downloding a file when user clicks a link ..
When user clicks the link I am checking in my action class whether requested file is existed or not if it is existed then it is working fine ...
WHen it is not existed I am giving a action error message .. But That page is redirecting to error page that i have mapped globally .. IN my console ther is no exception message except these error lines ..
Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
And I can see that action message in my error page ... I have debugged the problem also .. Mapping also perfect ..
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
Is It possible to map back to the same JSP when The file is not existed in SYSTEM .. Thanks
Check whether the file exists or not in the action, and return success & error respectively. You can do something like this :
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
<result name="error">/error.jsp</result>
error.jsp
<s:actionerror/>
<s:actionmessage/>
or your hard-coded message
But since, you need to go back to the same page, then I suggest calling the download action using ajax and return 200 on success and 204 on error something like this :
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
<result name="error" type="httpheader">
<param name="status">204</param>
</result>
The ajax call can be something like - docs
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
},
204: function() {
alert( "file not found" );
}
}
});

Change Struts2 interceptor error messages

I am using Struts 2 + Hibernate (full-hibernate-plugin-for-struts2), and I have this code to check if image to upload is png:
struts.xml
<struts>
<package name="mypack" namespace="/" extends="hibernate-default">
...
<action name="myaction" class="com.actions.MyAction" >
<interceptor-ref name="defaultStackHibernate">
<param name="fileUpload.allowedTypes">image/png</param>
</interceptor-ref>
<result name="success" type="tiles" >baseLayout</result>
<result name="error" type="tiles" >error</result>
<result name="input" type="tiles" >baseLayout</result>
</action>
...
</package>
</struts>
The error message that comes out if I upload not allowed image types is:
Content-Type not allowed: image "img.jpg" "upload__2988a871_13b93535e21__7fc1_00000009.tmp" image/jpeg
How can I modify that error message? Using something like "hibernate-messages.properties"?
You can override this message by creating text for this key:
struts.messages.error.content.type.not.allowed

How to send parametrs to the url in struts.xml

Hi any body know how to send parametrs to the url in struts.xml
mycode is
<action name="deleteDocDetails" class="com.myDrDirect.doctor.action.DoctorEditAction"
method="deleteSingleDoctor">
<result name="success" type="redirect">salesManDoctorhome </result>
</action>
i want to pass searchIndicator=Allsearch&search=&pageLink=doctor these variable to this parameter.
i want my url to be like this salesManDoctorhome?searchIndicator=Allsearch&search=&pageLink=doctor
i have already tried this
<action name="deleteDocDetails" class="com.myDrDirect.doctor.action.DoctorEditAction"
method="deleteSingleDoctor">
<result name="success" type="redirect">salesManDoctorhome </result>
<param name="searchIndicator">Allsearch</param>
<param name="pageLink">doctor</param>
</action>
But it is not working..
the variable i am sending is static variable. I am using struts2 in my project
You may want use variable in url, you may have {variable name} this variable must have public get method in action.

Resources