簡體   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