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" })
})
Related
Trying to redirect to external URL with post parameter.
My code is below:
<action name="testit" class="TestAction" method="test">
<result name = "success" type = "chain">
<param name="location">${at.url}</param>
<param name="login">${at.login}</param>
<param name="pass">${at.pass}</param>
</result>
</action>
Method in action class is:
public String test(){
at.setUrl("http://www.test.com");
at.setLogin("ssfd");
at.setPass("ssfd");
}
I am facing the following error:
Info: 2017-11-29 15:23:39 ERROR Dispatcher:38 - Exception occurred during processing request: null
java.lang.NullPointerException
at com.opensymphony.xwork2.util.OgnlTextParser.evaluate(OgnlTextParser.java:22)
at com.opensymphony.xwork2.util.TextParseUtil.translateVariables(TextParseUtil.java:170)
at com.opensymphony.xwork2.util.TextParseUtil.translateVariables(TextParseUtil.java:127)
at com.opensymphony.xwork2.util.TextParseUtil.translateVariables(TextParseUtil.java:49)
at com.opensymphony.xwork2.ActionChainResult.execute(ActionChainResult.java:207)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)
How to resolve this?
The chain result type doesn't redirect. Instead use redirect result type.
<action name="testit" class="TestAction" method="test">
<result name = "success" type = "redirect">
<param name="location">${at.url}</param>
<param name="login">${at.login}</param>
<param name="pass">${at.pass}</param>
</result>
</action>
I am trying to implement the file upload process in my web application using struts2 fileUpload interceptor. below is my code in
index.jsp
<tags:form action="fileUpload" method="post" enctype="multipart/form-data">
<tags:file name="fileUpload" label="Choose File"/>
<tags:submit value="Upload"/>
</tags:form>
struts.xml
<action name="fileUpload" class="com.hibernate.action.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024000</param>
<param name="allowedTypes">application/pdf</param>
</interceptor-ref>
<result name="success">/viewChapters.jsp</result>
</action>
FileUploadAction.java
public class FileUploadAction extends ActionSupport
{
private File fileUpload;
private String contentType;
private String fileName;
private String destPath;
/// setter and getter methods
public String execute()
{
destPath="C:\\WebPortal_testing";
try
{
System.out.println("Source File Name:"+fileUpload);
System.out.println("Destination File Name:"+fileName);
File destFile= new File(destPath,fileName);
FileUtils.copyFile(fileUpload, destFile);
}
catch(IOException exception)
{
exception.printStackTrace();
return ERROR;
}
return SUCCESS;
}
when I select a pdf file in the index.jsp page and click on upload button it is giving null value to the fileUpload field of the action class.
I am executing the application in debug mode and gave this
System.out.println("Source File Name:"+fileUpload);
to check what it is returning and I am getting null.
1. Interceptor configuration is wrong
FileUploadStack is:
<!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack>
then what you're really defining is:
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024000</param>
<param name="allowedTypes">application/pdf</param>
</interceptor-ref>
Using
two times the fileUpload interceptor
applying your limitations on maximumSize and allowedTypes only to the second.
Just do
<interceptor-ref name="fileUploadStack">
<param name="fileUpload.maximumSize">1024000</param>
<param name="fileUpload.allowedTypes">application/pdf</param>
</interceptor-ref>
2. File attributes are wrong
Content type and file name attributes must start with the File attribute name.
In your case:
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
You can find a full example on this question.
3. You are printing the File instead of the filename
System.out.println("Source File Name:"+fileUpload);
That is the file, not the filename, and btw the filename is passed in the other variable.
Fix this and retry. Also note that is not safe to use <tags: as prefix when the whole world is using <s:. There's no gain in doing that, only complications. Just use <s:.
It seems I should be able to have parameters in strut.xml defined PER result, and not globally, but I cannot get it to work. Here is what does work :
<action name="actThing" class="Thing" method="execute">
<interceptor-ref name="newStack" />
<param name="parentObject">Parent</param>
<result name="Edit">jspEditThing.jsp</result>
<result name="Add">jspAddThing.jsp</result>
</action>
In this case when Thing.execute gets called, the parentObject variable is set. But here :
<action name="actThing" class="Thing" method="execute">
<interceptor-ref name="newStack" />
<result name="Add">
<param name="location">jspAddThing.jsp</param>
<param name="parentObject">Parent</param>
</result>
<result name="Edit">jspEditThing.jsp</result>
</action>
it does not. Since it works in the first case I certainly have the proper settings/getters, and I don't get any kind of error. What am I missing?
Thanks.
It does not work, and it shouldn't because parameters are applied to the result, not the action. The result is executed after the action, and all parameters should be already set.
The param tag sets a property on the Result object. The most commonly-set property is location, which usually specifies the path to a web resources. The param attribute is another intelligent default.
<action name="actThing" class="Thing" method="execute">
<result name="Add">
<param name="includeProperties">Parent.*</param>
</result>
</action>
Now your Parent object with all its attributes and child objects will be available.
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
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