为什么我没有在Struts2 Action中初始化HttpServletRequest对象?

时间:2014-07-25 07:34:14

标签: java struts2

我正面临着struts2框架的问题。我想从html表单中获取Action类中excel文件的数据。但我能够找到HttpServletRequest对象来获取此请求的inputStream。                             我还在我的Struts2的Action类中实现了ServletRequestAware接口。我的代码正在显示............

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

我对如何获取HttpServletRequest对象实例以及我在控制台上收到其他错误的问题非常困惑 -

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

strut.xml文件显示在下面 -

<?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>  

    

请提出任何解决方案。 谢谢你给我的时间。

1 个答案:

答案 0 :(得分:0)

您需要将操作配置为包含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> 

由于您的操作已实施ServletRequestAware,因此将使用HttpServletRequest对象实例填充操作实例。