2014-07-25 75 views
0

我正面临着struts2框架的一个问题。我想从html表单中获取Action类中的excel文件的数据。但我能找到HttpServletRequest对象来获取此请求的inputStream。 我也在Struts2的Action类中实现了ServletRequestAware接口。我的代码显示在请............为什么我没有在Struts2 Action中初始化HttpServletRequest对象?

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> 

请给任何建议解决它。 感谢给你的imp时间。

回答

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对象实例进行填充。

+0

但问题仍然存在 – user3860841

+0

请给我建议HttpServletRequest对象实例 – user3860841

+0

如果实现'ServletRequestAware',HttpServletRequest对象实例由拦截器注入。你需要一个'defaultStack'来包含这些拦截器。 –