I am application is based on struts and in one of the module I am generating reports in pdf and excel format .The reports gets generated in system directory but download pop up dosen't appear on browser .
This is my .xml mapping of action
<action name="report" class="reportAction" method="generateReport">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename=${filename}</param>
<param name="bufferSize">1024</param>
</result>
<result name="error">
</result>
</action>
this is my cal to action from jsp :-
function fetchReport()
{
//alert("I am here");
$.ajax({
type : "POST",
url : "report.action",
data : jQuery("#parameterGrid").serialize(),
async : false,
success : function(dataJSP) {
//alert("Hi");
//alert(dataJSP);
/* jQuery("#showReportDIV").load("Please wait...").html(
dataJSP); */
return true;
}
});
}
Now I have changed my button on whose click event I generate the report to :-
<sj:submit button="true"
theme="simple" onclick= "fetchReport();" key="Generate Report" />
now the pop up appears but a flat file is download ,I have to browse to open it either in excel or pdf
Related
My application displays the login page, but cannot login.
On the login page, user enters the username and password but on pressing the login button nothing happens.
We have in our JSP :
<s:form id="loginForm" name="loginForm" action="login" namespace="/framework">
The struts-framework.xml file has the action defined as follows:
<global-results>
<result name="loginShow" type="redirect">loginShow.action</result>
<result name="toMain">/page/framework/main.jsp</result>
<result name="logout">/page/framework/logout.jsp</result>
</global-results>
<action name="login" class="com.abc.action.framework.LoginAction" method="loginCheck"> </action>
In our logs we get the Request start for the action framework/login.action, but no response is obtained thereafter and same is evident from the logs.
For reference I have included the following to my struts.xml after migrating struts to 2.3.24 :
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
Thanks for all your efforts. It was an issue with struts-convention plugin.Works well for me on removing this plugin and mapped dependancies in struts.xml
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
I am having an issue with the Message Store Interceptor in Struts 2. I have the following action in my struts.xml file:
<action name="rfi" method="add" class="org.test.action.RfiAction">
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="error">../display/irrt/rfi.jsp</result>
<result name="input">../display/irrt/rfi.jsp</result>
<result name="success" type="redirectAction">
<param name="actionName">rfis</param>
<param name="namespace">/irrt</param>
</result>
</action>
When the action returns successfully, it redirects to the action listed and the success message is properly saved and retrieved by the interceptor.
However, when an error occurs, there is no redirect and it goes to the listed JSP page but the error doesn't show up (all other data does). It's like the MessageStoreInterceptor wipes out the contents of the error variables when it runs so that if no redirect occurs, the current action no longer has the error message.
This happens when the interceptor is set to either STORE or AUTOMATIC mode (even though the interceptor shouldn't even run if it's in AUTOMATIC mode and the result doesn't include a redirect).
My code only ever adds errors or messages. It never deletes them. The action code is below:
private String add()
{
try
{
// add the rfi entry
this.rfiService.addRfi(this.rfiEntry, this.getLoggedInUser());
}
catch(ConnectionException e)
{
this.addActionError("Could not add RFI entry.");
e.printStackTrace();
return ERROR;
}
// set success message
this.addActionMessage("RFI entry added.");
return SUCCESS;
}
This is the code in the JSP being used to display the messages:
<s:if test="hasActionErrors() == true">
<s:iterator value="actionErrors">
<p class="text"><b><font color="red"><s:property /></font></b></p>
</s:iterator>
</s:if>
<s:elseif test="hasActionMessages() == true">
<s:iterator value="actionMessages">
<p class="text"><b><font color="green"><s:property /></font></b></p>
</s:iterator>
</s:elseif>
Any help with this issue would be greatly appreciated.
<result name="success" type="redirectAction">
<param name="actionName">rfis</param>
<param name="namespace">/irrt</param>
</result>
The redirectAction rfis should also go through the store interceptor.
<action name="rfis" class="..." method="...">
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result ..../>
</action>
I was missing the include on this particular page that included the errors.
Moral of the story: don't assume code that's on every single other page is on the one that's not working.
Scenario: On click of a Submit button I have unmarshalled the XML to Java and then displaying the Java objects(created a formbean and mapped the JAXB java Objects to this formbean - PolicyForm and using this formbean to display in JSP) in the JSP. This is working fine.
Issue: I have one more button -Calculate on the same page with in the same form of Submit button. On click of the 'Calculate' button I need to get the acordform values which was unmarshalled during the click of Submit button. Here I am not able to get the acordform values instead I am getting the new object of acordform.
When I have googled I see - Struts 2 doesn't have thread-safety issues as "Action objects are instantiated for each request".
Please let me know here when I am sending the request again is the Action object- acordform being instantiated everytime? If yes, how can I avoid this? Since I need the acordformobject values even in the next request too.
Action Class:
public class RateAction extends ActionSupport implements
ServletRequestAware,SessionAware {
/* ... */
// ACORD xml form bean
private ACORD acordform = new ACORD();
//To display the values in the JSP
private PolicyForm policyForm;
public ACORD getAcordform() {
return acordform;
}
public void setAcordform(ACORD acordform) {
this.acordform = acordform;
}
public String doSubmit() {
/*...Unmatshalling from XML to Java -acordform is done..*/
}
public String doRateSubmimt()
{
/*..trying to get the acordform values which are being set
previously in doSubmit() method...*/
}
}
Strust.xml:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/" >
<action name="fileUploadAction"
class="com.main.common.action.RateAction" >
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/xml</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="params"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="basicStack"/>
<result name="success">First.jsp</result>
<result name="input">First.jsp</result>
</action>
<action name="submitAction" class="com.main.common.action.RateAction" method="doSubmit">
<result name="success">First.jsp</result>
</action>
<action name="rateAction" class="com.main.common.action.RateAction" method="doRateSubmit">
<result name="success">First.jsp</result>
</action>
</package>
JSP:
<s:form id="rtr" action="fileUploadAction" method="POST"
enctype="multipart/form-data">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<s:file name="uploadFile" label="Upload Request XML" />
<s:submit value="Submit" action="submitAction" onclick="displayDetails();" />
<s:hidden name="submitSuccess" />
<s:submit value="Calculate" action="rateAction" />
</tr>
</table>
</s:form>
Can't you perform the operation in one click ? I mean Submit the XML, and draw the result with the unmarshalled XML...
If no, since you need to share that in-memory XML (not saved on HD nor DB, just posted to the Action) between multiple requests / actions, put it in the Session Map by implementing SessionAware in your Action.
Note that you are using too much Interceptors...
This should be enough:
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">text/xml</param>
</interceptor-ref>
I am developing one application , where people will download the required file from a location mentioned in the DB to their Local. I am using struts 2 for downloading the file from the server . I can download the file without any exception and it works perfectly.
But the files am download has the filename i specified in struts.xml , i want it to be the exact filename which am downloading . example if the original file name is struts.pdf , i am downloading it as download.pdf, how to prevent it and download the file with actual filename
My struts.xml configuration as follows ,
<action name="download" class="action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="download.log"</param>
<param name="bufferSize">1024</param>
</result>
<result name="error">/live/useradminerror.jsp</result>
</action>
And i forgot to mention am using struts2-jquery for developing the UI .Please help me in this , as am in very critical stage of my project .
IF i am correct you want to pass the file which is being stored in your DB, if this is the case you can easily do this by passing all those parameters from you action class like
class MyFileDownloadAction extends ActionSupport{
private String fileName;
// getter and setter
public String fileDownload() throws exception{
// file download logic
fileName ="abc" // can set name dynamic from DB
}
}
<action name="download" class="action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="bufferSize">1024</param>
</result>
<result name="error">/live/useradminerror.jsp</result>
</action>
You can pass each parameter dynamically in your struts.xml class.Hope this will help you
This is how you will use this file name in your XML
For annotations in struts, its same. The solution was very helpful. Thank you. The "contentType" for me did not make much difference.
#Action(value = "/download", results = { #Result(name = "success", type = "stream",
params= {"contentType", "application/octet-stream", "inputName","fileInputStream",
"contentDisposition","attachment; filename=\"${fileName}\"", "bufferSize", "1024" })
})