Struts2 execAndWait和文件上传无效

时间:2014-04-12 22:23:56

标签: file file-upload struts2 struts-validation

我遇到了这个问题,如果没有结果,我真的很疯狂。 我有一个表单,所有字段都需要通过validator.xml 我的表单包含上传图片的字段(必填) 当我单击提交按钮时出现一个等待的paggina(在struts.xml中配置了execAndWait)。 我的大问题是: 等待页面总是重定向到带有文本的表单页面(文件字段和必填项)。 这是代码:

             /register.jsp     

<!-- al submit chiama l'action register -->
<action name="register" class="action.Register" method="execute" >

<interceptor-ref name="defaultStack" />

 <interceptor-ref name="fileUpload">
            <param name="maximumSize">10000000</param>
             <param name="allowedTypes">image/jpeg,image/gif,image/jpg</param>

 </interceptor-ref>

   <interceptor-ref name="params"></interceptor-ref>
  <interceptor-ref name="execAndWait">
  </interceptor-ref>        
    <result name="success">index.jsp</result>
    <result name="input">/register.jsp</result>  
    <result name="wait">/test.jsp</result>  
</action>

等待页面:

<meta http-equiv="refresh" content="5;url=<s:url includeParams="all" />"/>
</head>
<body>

<p>your request is processing...</p>
<img src="images/indicator.gif"/>

我的表格:

 <s:form method="post" action="register" validate="false" enctype="multipart/form-data">
            <s:textfield key="utenteBean.nome" name="utenteBean.nome" value="a" />
            <s:textfield key="utenteBean.nickname" name="utenteBean.nickname" value="a" />
            <sj:datepicker key="utenteBean.nato" name="utenteBean.nato"
                showButtonPanel="true" displayFormat="dd/mm/yy"  value="25/09/1983"/>
            <s:textfield key="utenteBean.professione" name="utenteBean.professione"   value="a"/>
            <s:textfield key="utenteBean.eta" name="utenteBean.eta" value="3"/>
            <s:textfield key="utenteBean.dj_preferito" name="utenteBean.dj_preferito" value="a" />
            <s:textfield key="utenteBean.rave_fatti" name="utenteBean.rave_fatti" value="3" />
            <s:textfield key="utenteBean.sito_preferito" name="utenteBean.sito_preferito" value="a" />
            <s:textfield key="utenteBean.come_siveste" name="utenteBean.come_siveste" value="a" />
            <s:textarea key="utenteBean.messaggio" name="utenteBean.messaggio" value="a"/>
            <s:file label="file" name="file" requiredLabel="true"" ></s:file>

enter image description here enter image description here enter image description here

非常感谢您的帮助

3 个答案:

答案 0 :(得分:0)

我认为您为file tag添加了错误的字段名称。你能告诉我们你的动作课吗?关于那而不是:

<s:file label="file" name="file" requiredLabel="true"" ></s:file>

尝试使用:

<s:file label="file" name="utenteBean.file" requiredLabel="true"" ></s:file>

或者只是在动作类中检查文件属性的名称。

答案 1 :(得分:0)

是的确定...在我的Action类之下:

public class Register extends ActionSupport实现了SessionAware {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private UtenteBeanAction utenteBean;

private File file;
private String fileContentType;
private String fileFileName;
private String filesPath;
private ServletContext context;
Map<String, Object> session;


public String execute(){

    if (file != null) {
        File file = this.file;
        // /System.out.println(file.getName());
        try {
            Util.saveFile(file, fileFileName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ERROR;
        }
    } else {
        return ERROR;
    }   
    return SUCCESS;
}



 public void validate(){

     if(this.fileFileName==null){
         this.addFieldError("file", "errore");  
     }
 }


public UtenteBeanAction getUtenteBean() {
    return utenteBean;
}

public void setUtenteBean(UtenteBeanAction utenteBean) {
    this.utenteBean = utenteBean;
}


public File getFile() {
    return file;
}

public void setFile(File file) {
    this.file = file;
}

public String getFileContentType() {
    return fileContentType;
}

public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
}

public String getFileFileName() {
    return fileFileName;
}

public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
}

public String getFilesPath() {
    return filesPath;
}

public void setFilesPath(String filesPath) {
    this.filesPath = filesPath;
}

public ServletContext getContext() {
    return context;
}

public void setContext(ServletContext context) {
    this.context = context;
}

@Override
public void setSession(Map<String, Object> session) {
    // TODO Auto-generated method stub

    this.session=session;


}

}

非常感谢你的帮助......

答案 2 :(得分:0)

我通过将我的bean放入会话来解决它。经过数小时的工作后,我找不到更好的解决方案。 如果其他人有同样的问题我可以看到我在我的变化。操作

package action;



import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import Bean.UtenteBeanAction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import Utility.Util;


public class Register extends ActionSupport implements SessionAware {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private UtenteBeanAction utenteBean;


    private ServletContext context;
    Map<String, Object> session;


    public String execute(){


        session.put("utente", utenteBean);
        if (utenteBean.getFile() != null) {
            File file = utenteBean.getFile();
            // /System.out.println(file.getName());
            try {
                Util.saveFile(file, utenteBean.getFileFileName());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return ERROR;
            }
        } else {
            return ERROR;
        }   

        return SUCCESS;
    }



    public void validate() {


        if (session.get("utente") != null) {
            this.utenteBean = (UtenteBeanAction) session.get("utente");
        }

        if (this.utenteBean.getFileFileName() == null) {
                this.addFieldError("utenteBean.file", "errore");
        }

    }


    public UtenteBeanAction getUtenteBean() {
        return utenteBean;
    }

    public void setUtenteBean(UtenteBeanAction utenteBean) {
        this.utenteBean = utenteBean;
    }




    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        // TODO Auto-generated method stub

        this.session=session;

    }

}

我希望我可以提供帮助。 感谢您的支持。