Getting NullPointer Exception on FileUpload using Struts2 and Ajax - struts2

I am implementing Struts2 File Upload using Ajax; I have added the jquery code as below
$("#btnUploadAttach").click(function () {
alert("Uploading your file");
//stop submit the form, we will post it manually.
event.preventDefault();
// Get form
var form = $('#fileUploadForm')[0];
// Create an FormData object
var data = new FormData(form);
alert("Uploading your file" + data);
$.ajax({
type: 'POST',
enctype: 'multipart/form-data',
data: data,
contentType: false,
processData: false,
cache: false,
timeout: 600000,
url: "/Dashboard/UploadFile.action",
success: function (data) {
alert("file saved successfully");
}
});
});
Also Please find the below code of JSP :
<s:form id="fileUploadForm" enctype="multipart/form-data" method="POST">
<div class="col-md-12">
<s:label for="CashReqFileUpload">Select Your File:</s:label>
</div>
<div class="col-md-12">
<div class="col-md-4">
<s:file name="CashReqFileUpload" id="CashReqFileUpload"/>
</div>
<div class="col-md-4"><input type="submit" value="Upload Document" id="btnUploadAttach" ></div>
<div class="col-md-4">
<button id="btnPrint" class="btn btn-success">Print</button>
</div>
</div>
</s:form>
And my action class as below :
private File CashReqFileUpload;
private String CashReqFileUploadFileName;
private String CashReqFileUploadContentType;
private String destPath;
public String execute(){
return SUCCESS;
}
public String uploadFileOnServer() throws IOException {
destPath = "D:/Temp/";
System.out.println("CashReqFileUpload File name: " + CashReqFileUpload);
System.out.println("CashReqFileUploadFileName File name: " + CashReqFileUploadFileName);
System.out.println("destPath File Name : "+destPath);
File destFile = new File(destPath, CashReqFileUploadFileName);
FileUtils.copyFile(CashReqFileUpload, destFile);
return SUCCESS;
}
public File getCashReqFileUpload() {
return CashReqFileUpload;
}
public void setCashReqFileUpload(File CashReqFileUpload) {
this.CashReqFileUpload = CashReqFileUpload;
}
public String getCashReqFileUploadFileName() {
return CashReqFileUploadFileName;
}
public void setCashReqFileUploadFileName(String CashReqFileUploadFileName) {
this.CashReqFileUploadFileName = CashReqFileUploadFileName;
}
public String getCashReqFileUploadContentType() {
return CashReqFileUploadContentType;
}
public void setCashReqFileUploadContentType(String CashReqFileUploadContentType) {
this.CashReqFileUploadContentType = CashReqFileUploadContentType;
}
I have tried with Serialize(), but it didn't workout, I found one solution on https://www.mkyong.com/jquery/jquery-ajax-submit-a-multipart-form/ to implement using Ajax. It is giving me NullPointerException as below :
Severe: java.lang.NullPointerException
at java.io.File.<init>(File.java:317)
at com.ISG.CIA.CTI.operations.UploadFile.uploadFileOnServer(UploadFile.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:899)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1544)
at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:96)
at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:88)
at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1620)
at ognl.ASTMethod.getValueBody(ASTMethod.java:91)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.Ognl.getValue(Ognl.java:470)
at ognl.Ognl.getValue(Ognl.java:434)
at com.opensymphony.xwork2.ognl.OgnlUtil$3.execute(OgnlUtil.java:371)
at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecuteMethod(OgnlUtil.java:423)
at com.opensymphony.xwork2.ognl.OgnlUtil.callMethod(OgnlUtil.java:369)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:436)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:253)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:260)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:52)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.doIntercept(ConversionErrorInterceptor.java:139)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:134)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:134)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:199)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:69)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:115)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:88)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:246)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:99)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:139
Severe: hain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:744)
I want to implement file upload using Struts and Ajax only. Not using with any other plugin. I searched on StackOverflow, but their solution is not what my requirement is.
EDIT
Uploading Struts.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name = "struts.devMode" value = "true" />
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.custom.i18n.resources" value="operationProp.OperationalProp" />
<package name = "CTI" extends = "struts-default">
<interceptors>
<interceptor-stack name="modelParamsPrepareParamsStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="params">
<param name="excludeParams">^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
</interceptor-ref>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name = "OnLoadAction"
class = "com.ISG.CIA.CTI.CashRequestManagement"
method = "execute">
<result name = "success">/ApprovedHistory.jsp</result>
</action>
<action name = "OnClickOfCreateNewReq"
class = "com.ISG.CIA.CTI.CashRequestManagement"
method = "getCashReqDataForNewReq">
<result name = "success">/CashRequestForm.jsp</result>
</action>
<action name = "onLoadForm"
class = "com.ISG.CIA.CTI.CashRequestManagement"
method = "getCashReqDataForSavedRequest">
<result name = "success">/CashRequestForm.jsp</result>
</action>
<action name = "OnClickOfSaveRequest"
class = "com.ISG.CIA.CTI.CashRequestManagement"
method = "SaveCashRequest">
<result name = "success">/CashRequestForm.jsp</result>
</action>
<action name="CashReqRej"
class="com.ISG.CIA.CTI.CashRequestManagement"
method="cancelCashReq">
<result name="success">/CashRequestForm.jsp</result>
</action>
<action name="CashReqApprove"
class="com.ISG.CIA.CTI.CashRequestManagement"
method="approveCashReq">
<result name="success">/CashRequestForm.jsp</result>
</action>
<action name="UploadFile"
class="com.ISG.CIA.CTI.operations.UploadFile"
method="uploadFileOnServer">
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<result name="none">/CashRequestForm.jsp</result>
</action>
</package>
May be struts.xml may help to identify the issue.

Try file upload interceptor before other like
<action name="UploadFile"
class="com.ISG.CIA.CTI.operations.UploadFile"
method="uploadFileOnServer">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="basicStack"/>
<result name="none">/CashRequestForm.jsp</result>
</action>

Related

How to invoke prepare method of Preparable Interface after my own custom interceptor

I have created my own Interceptor, MyInterceptor, as follows:
public class MyInterceptor extends MethodFilterInterceptor
{
String inv=null;
#Override
protected String doIntercept(ActionInvocation invocation)
{
System.out.println("MyInterceptor fired");
try
{
inv=invocation.invoke();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return inv;
}
}
My action class is this:
public class MyStruts2Class extends ActionSupport implements Preparable
{
#Override
public void prepare()
{
System.out.println("HI Prepare");
}
public String execute()
{
System.out.println("Hi Execute");
return "success";
}
}
... and my struts.xml is this:
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/> </interceptors>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<interceptor-ref name="myIntercept"/> <result name="success">success.jsp</result>
</action>
</struts>
My problem is when I fire my defaultStrutsAction class, MyInterceptor runs fine but the prepare method in the Action class is not invoked which it should, as I have implemented the Preparable interface.
If I omit this line:
<interceptor-ref name="myIntercept"/>
... in struts.xml, the prepare method in the Action class runs fine.
I am new in Struts2, but can't figure out what is wrong here.
You have to add the prepare interceptor to the interceptor stack for the prepare method to get called in the action. You can do this in a few ways, one is to define the action itself to have the prepare interceptor. Like so. The order matters.
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/> </interceptors>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="prepare"/>
<result name="success">success.jsp</result>
</action>
</struts>
You can also define the interceptor stack in the interceptors tag and use the default-interceptor-ref tag
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="prepare"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<result name="success">success.jsp</result>
</action>
</struts>
And finally, what I think your interceptor stack should look like,
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<result name="success">success.jsp</result>
</action>
</struts>
In this example, I ref defaultStack instead of just prepare, because defaultStack already has prepare defined along with other common interceptors that you may want to use.
in your example when you omit this line
<interceptor-ref name="myIntercept"/>
the prepare interceptor gets called because it is in the defaultStack already defined by struts2.
Take a look at the stacks defined in link provided by Steven Benitez.
Under default configuration.
http://struts.apache.org/release/2.3.x/docs/interceptors.html
Hope that helps.
here is an example of my reply to your comment.
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/> </interceptors>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="defaultStack"/>
<result name="success">success.jsp</result>
</action>
</struts>

Struts 2 server side validation using XML not working for struts action

I am using Struts 2 Validator framework with XML. But the server side validations are not working. Following is the code snippet.
Struts.xml
<interceptor-stack name="MyStack">
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,.*\\.*,.*\(.*,.*\).*,.*#.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse,reset</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse,reset</param>
</interceptor-ref>
</interceptor-stack>
<action name="process" method="process" class="org.web.action.MyAction">
<interceptor-ref name="MyStack" />
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
<result name="wait">wait.jsp</result>
<result name="input">Index.jsp</result>
</action>
MyAction.java
public class MyAction extends ActionSupport {
private String amount;
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String process() throws Exception {
//some processing done here
}
}
MyAction-process-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"##xwork.validator.dtd##">
<validators>
<field name="amount">
<field-validator type="requiredstring" short-circuit="true">
<message key="order.amount.required"/>
</field-validator>
<field-validator type="amountValidator" short-circuit="true">
<message key="order.amount.invalid" />
</field-validator>
</field>
</validators>
AmountValidator.java
public class AmountValidator extends FieldValidatorSupport {
public Struts2DonationAmountValidator() {
super();
}
public void validate(Object object) throws ValidationException {
//some validation check here
}
}
validators.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"https://s3.amazonaws.com/static/xwork-validator-config-1.0.dtd">
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator" />
<validator name="amountValidator" class="org.web.validator.AmountValidator"/>
</validators>
Index.jsp
<input type="text" id="amountTemp" name="amount">
I have put sysouts at the first line of validator as well as in action method. The sysout in AmountValidator.java is not called. Even while debugging, the validation doesn't get called and the control jumps to action method. There is no hint of any exception in console. The action method flow works fine.
Thanks in advance!!!
I found a other solution on http://struts.apache.org/release/2.1.x/docs/validation.html. I changed the name of my MyAction-process-validation.xml to MyAction-processAction-validation.xml and named my action as processAction instead of process. That means instead of method I used action name.
Thanks for your support.

struts2 global result not redirecting to action

I have a custom interceptor (TransferInterceptor) that checks for a function change within my application. When it changes, the interceptor returns a post-processing string (the result) containing the global results name of the action that I want to redirect to. I have the results defined in the global results of my struts.xml file, but they do not redirect to the specified action. I have a 'Login' global result that works fine, but it is being called from a JSP with:
<s:a action="Login.action">Login</s:a>
The struts.xml file:
<package name="default" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="AuthenticationInterceptor" class="com.purchasing.utils.AuthenticationInterceptor" />
<interceptor name="TransferInterceptor" class="com.purchasing.utils.TransferInterceptor" />
<interceptor-stack name="secureStack">
<interceptor-ref name="TransferInterceptor" />
<interceptor-ref name="AuthenticationInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="secureStack" />
<global-results>
<result name="Login">login.jsp</result>
<result name="RESULTA" type="redirect">ACTIONA</result>
<result name="RESULTB" type="redirect">ACTIONB</result>
</global-results>
<action name="Login" class="com.purchasing.actions.Login">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">welcome.jsp</result>
<result name="error">login.jsp</result>
<result name="input">login.jsp</result>
</action>
<action name="ACTIONA" class="com.purchasing.actions.ACTIONA">
<result name="success">ACTIONA.jsp</result>
<result name="error">ACTIONA.jsp</result>
<result name="input">ACTIONA.jsp</result>
</action>
<action name="ACTIONB" class="com.purchasing.actions.ACTIONB">
<result name="success">ACTIONB.jsp</result>
<result name="error">ACTIONB.jsp</result>
<result name="input">ACTIONB.jsp</result>
</action>
</package>
The TransferInterceptor:
public String intercept(ActionInvocation ai) throws Exception
{
String result = ai.invoke();
session = ai.getInvocationContext().getSession();
fl = (FunctionLine) session.get(Constants.FUNCTION_LINE);
function = fl.getFl_function();
last_function = (Integer) session.get(Constants.LAST_FUNCTION);
if (function != last_function) {
session.put(Constants.LAST_FUNCTION, new Integer(function));
switch (function) {
case 61: return "RESULTA";
case 62: return "RESULTB";
}
return "error";
}
else {
return result;
}
}
Any help would be greatly appreciated!
As per #Lukasz comment :
If you want to redirect to an action use redirectAction result type instead of redirect which is used to redirect to static resources
It still won't work, because the ACTIONB & ACTIONA never executes since they go through the same interceptor stack, which returned redirectAction result & hence, it's kinda stuck in an infinite loop.

i can't generate my report with jasperReport and struts2

i'm trying to implement jasper report with struts2,for now i can't get my report.i do realy nead help,here is my struts.xml
<struts>
<package extends="jasperreports-default" name="default" namespace="/">
<result-types>
<result-type class="org.apache.struts2.views.jasperreports.JasperReportsResult" name="jasper"/>
</result-types>
<action class="action.JasperAction" name="StrutsJasperAction" method="execute">
<result name="success" type="jasper">
<param name="location">/jasper/sample.jasper</param>
<param name="dataSource">stList</param>
<param name="format">PDF</param>
</result>
</action>
</package>
</struts>
and i want to know in what location should i do.here is my structure of my app.
and in my action JasperAction what should i do at JasperCompileReportToFile.compileReportToFile(....??????) .
try {
JasperCompileManager.compileReportToFile(
"D:/Test/workspace/testJasper/WebContent/jasper/sample.jrxml",
"D:/Test/workspace/testJasper/WebContent/jasper/sample.jasper");
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
if you need more details just ask.

Struts 2 using tokenSession Interceptor unable to get form values after submit

I have following simple code for login & secured login. For secured login I am using tokenSession interceptor at the time of form submit . In success.jsp I am unable to get the userId value if I submit secureLogin but fine with Login . Can anyone pls help me what is the reason behind it.
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<body><LOGIN.JSP>
<s:form action="login" >
<s:textfield name="userId" label="Login Id"/><br>
<s:password name="password" label="Password"/><br>
<s:submit value="Login" align="center"/>
</s:form>
<s:form action="secureLogin" >
<s:textfield name="userId" label="Login Id"/><br>
<s:password name="password" label="Password"/><br>
<s:token />
<s:submit value="secureLogin" align="center"/>
</s:form>
</body>
</html>
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="tokenSession" class = "org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
</interceptors>
<global-results>
<result name="invalid.token">/error.jsp</result>
</global-results>
<action name="secureLogin" class="com.actions.HelloAction" method="secureLogin">
<result name="success">/success.jsp</result>
<result name="failure">/login.jsp</result>
<interceptor-ref name="tokenSession" />
</action>
<action name="login" class="com.actions.HelloAction" method="login">
<result name="success">/success.jsp</result>
<result name="failure">/login.jsp</result>
</action>
</package>
</struts>
package com.actions;
public class HelloAction {
private String userId;
private String password;
public String execute() {
return "success";
}
public String login(){
return "success";
}
public String secureLogin(){
return "success";
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>SUCCESS.JSP</title>
</head>
<body>
Welcome <b><s:property value="userId"/></b> , you have loged in. <br />
</body>
</html>
you need a interceptor-stack
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="tokenSession" class = "org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
<interceptor-stack name="yourStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="tokenSession"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="yourStack"/>
<global-results>
<result name="invalid.token">/error.jsp</result>
</global-results>
</package>
</struts>
Here is a link: Interceptors

Resources