繁体   English   中英

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

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

我面临着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>  

请给任何建议解决它。 感谢您给我的印象时间。

您需要将操作配置为包括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对象实例进行填充。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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