简体   繁体   中英

Why am i not getting initialize HttpServletRequest object in Struts2 Action?

I am facing a issue with struts2 framework. I want to get data of excel file in Action class from html form. but i am able to finding HttpServletRequest object to get inputStream of this request. I am also implementing ServletRequestAware interface in my Action class of Struts2. My code is being displayed in please............

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

import jxl.*;
import jxl.read.biff.BiffException;

public class UpdateZflexRecordsAction extends ActionSupport implements ServletRequestAware{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private boolean result = false;
private String status = null;
private String msg = null;
private ServletInputStream inputStream = null; 
private HttpServletRequest request = null;

public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}


public String getMsg() {
    return msg;
}
public void setMsg(String msg) {
    this.msg = msg;
}

public String execute()
{   
    try {
        inputStream = getServletRequest().getInputStream();

        byte[] junk = new byte[1024];
        int bytesRead = 0;
        // strip off the HTTP information from input stream
        // the first four lines are request junk
        bytesRead = inputStream.readLine(junk, 0, junk.length);
        bytesRead = inputStream.readLine(junk, 0, junk.length);
        bytesRead = inputStream.readLine(junk, 0, junk.length);
        bytesRead = inputStream.readLine(junk, 0, junk.length);

        // create the workbook object from the ServletInputStream
        Workbook workbook = Workbook.getWorkbook(inputStream);
        Sheet sheet = workbook.getSheet(0);
        Cell cell = null;
        System.out.println(sheet.getName());



    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BiffException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (result) {
        this.setStatus("Success");
        this.setMsg("Congratulation ! Your request has been accepted.");
    } else {
        this.setStatus("Failled");
        this.setMsg("Sorry ! Your request has not been accepted.");
    }
    return Action.SUCCESS;
}

public void setServletRequest(HttpServletRequest request) {
    this.request = request;

}
public HttpServletRequest getServletRequest() {
    return this.request;
}
}

I am very confused with issue that how to get HttpServletRequest object instance and i am getting an other error on console -

INFO: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir

And strut.xml file is being displayed in below -

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<constant name="struts.devMode" value="true" /> 
<constant name="struts.multipart.maxSize" value="10000000" />

<package name="default" extends="struts-default, json-default" namespace="/">

<action name="downloadZflexFormat" class="sadagi.my.pro.action.RootMapper" method="downloadZflexFormatAction">  
<interceptor-ref name="accessRequired"/>
<interceptor-ref name="scope" />
<result name="login" type="redirect">/</result>  

Please give any suggestion solve it. thank to give your imp time.

You need to configure the action to include defaultStack .

<action name="downloadZflexFormat" class="sadagi.my.softhuman.action.RootMapper" method="downloadZflexFormatAction">  
<interceptor-ref name="accessRequired"/>
<interceptor-ref name="scope" />
<interceptor-ref name="defaultStack" />
<result name="login" type="redirect">/</result> 

Because your action is implemented ServletRequestAware the action instance will be populated with the HttpServletRequest object instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM