2014-04-12 46 views
0

我有這個問題,我真的很瘋狂,沒有結果。 我有一個窗體,所有的字段都需要通過validator.xml 我的表單包含一個上傳圖像的字段(必填) 當我點擊提交按鈕呈現等待通過(在struts.xml中配置execAndWait)paggina。 我的大問題是這樣的: 等待的頁面總是重定向到我的表單頁面的文本(文件字段和強制性)。 下面是代碼:Struts2 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

真的感謝您的幫助

回答

0

我想你已經爲file tag放錯字段名。你能向我們展示你的行動課嗎?關於該代替:

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

嘗試使用:

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

或只是在你的動作類檢查文件屬性的名稱。

+0

我把我的行動的代碼。我希望你能幫助我非常感謝你。 – Diego

0

是肯定......我下面的Action類:

公共類註冊擴展了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; 


} 

}

非常感謝你的幫助......

0

我通過把我的bean放入session來解決它。經過數小時的工作後,我找不到更好的解決方案。 如果其他人有同樣的問題,我可以看到我在我的。行動

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; 

    } 

} 

我希望我能幫上忙。 感謝您的支持。