日期:2014-05-17  浏览次数:20796 次

struts2 request问题
Java code
public class CreateTableSpaceAction extends ActionSupport{
    
    private static final long serialVersionUID = 1L;
    
        private HttpServletRequest request;

        public HttpServletRequest getRequest() {
            return request;
        }
        public void setRequest(HttpServletRequest request) {
            this.request = request;
        }

        
        @Override
        public String execute() throws Exception{
            
            System.out.println("======CreateTableSpaceAction=====");
            //这里报空指针
            String spaceName = (String) request.getAttribute("spaceName");//34行
            System.out.println("spaceName:"+spaceName);
            String spaceSize = request.getParameter("spaceSize");
            String spacePath = request.getParameter("spacePath");
            SysUserPOJO sysUserPOJO = getsysUser(request);
            InitSysDAO initSysDAO = new InitSysDAO();



严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at com.asideal.action.sysInit.CreateTableSpaceAction.execute(CreateTableSpaceAction.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

------解决方案--------------------
你在提交时有没有传spaceName这个参数
------解决方案--------------------
表单提交问题,看参数名对应上了嘛,还有struts2最好不要这么传值,直接get/set接值比较好
------解决方案--------------------
我记得request有其他方法可以取,你那个申明get set方法应该取不到
------解决方案--------------------
request null? 还是request.getAttribute("spaceName") null 可以用输出先调试的看下
------解决方案--------------------
public class CreateTableSpaceAction extends ActionSupport{

private static final long serialVersionUID = 1L;
private String spaceName;
public String getSpaceName(){
return spaceName;
};
public void setSpaceName(spaceName){
this.spaceName=spaceName;
}

private HttpServletRequest request;

public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}

//如果是用post的话这样就行了,如果是get的话就要传spaceName=?(?号是你要传的值)

@Override
public String execute() throws Exception{

System.out.println("======CreateTableSpaceAction=====");
System.out.println("spaceName:"+spaceName);
String spaceSize = request.getParameter("spaceSize");
String spacePath = request.getParameter("spacePath");
SysUserPOJO sysUserPOJO = getsysUser(request);
InitSysDAO initSysDAO = new InitSysDAO();

------解决方案--------------------
request那样能取到吗?试试这样:httpservletrequest request=servletactionsupport.getrequest();
------解决方案--------------------
拜托,request.getParameter("键名")
------解决方案--------------------
request.getAttribute("spaceName");
改为 request.getParameter("spaceName");
试试
------解决方案--------------------