2015-01-07 43 views
0

我想要求用戶在提交文件之前輸入密碼。所以點擊提交按鈕後應該呈現<h:panelGroup>。但是,<h:panelGoup>永遠不會呈現。基於bean字段值呈現表格

test.xhtml

<ui:define name="body"> 
    <h:form id="uploadForm" enctype="multipart/form-data"> 
     <table> 
      <t:inputFileUpload id="uploadedFile" storage="file" 
           value="#{UpdateBean.uploadedFile}"/> 
      <h:commandButton value="Submit" action="#{UpdateBean.submit()}"/> 
     </table> 
    </h:form> 

    <h:panelGroup id="checkPassword" rendered="#{UpdateBean.submitIsPerformed}"> 
     <h:outputText id="message" value="${UpdateBean.message}" /> 
     <h:inputText id="password" value="#{UpdateBean.password}" /> 
     <h:commandButton value="submit" action="#{UpdateBean.submitPassword()}"/> 
    </h:panelGroup> 
</ui:define> 

UpdateBean.java

@ManagedBean(name = "UpdateBean") 
@SessionScoped 
public class UpdateBean { 
    protected boolean submitIsPerformed = false; 
    protected String password = ""; 
    protected String message = "Input your password "; 

    // omit getter and setter 

    public void submit() { 
     this.setSubmitIsPerformed(true); 
     System.out.println(submitIsPerformed); // output is true 

     while(true) { 
      if(password.equals("123")) { 
       break; 
      } 
     } 

     // then process uploadedFile 
    } 

    public void submitPassword(){ 
     if(password.equals("123")) { 
      message = "Password confirmed !"; 
     } else { 
      message = "Password is wrong !"; 
     } 
    } 
} 
+0

凡'this.setSubmitIsPerformed(真);'定義? – ryanyuyu

+0

@ryanyuyu它是爲submitIsPerformed設置的。 – Bryan

+0

@你的意思是?我試過這個,但不起作用。 – Bryan

回答

2

你的錯誤是在submit()方法:

while(true) { 
    if(password.equals("123")) { 
     break; 
    } 
} 

while(true)防止action方法返回。只要操作方法沒有返回,服務器就不會返回帶有更新視圖的HTTP響應。實際上,服務器的一個CPU會陷入100%,客戶端無限期地等待HTTP響應。您應該通過檢查瀏覽器的進度指示器(如果有)來注意它。

你應該立即基本上切換布爾後返回:

public void submit() { 
    submitIsPerformed = true; 
} 

並執行密碼檢查和上傳文件中submitPassword()方法保存。但是,由於這不是相同的格式,上傳的文件將會丟失。即使你把它放在同一個表格中,它也會被上傳兩次。但這是一個不同的問題。我建議以相反的方式來完成這項工作。

+0

謝謝!你的解決方案確實奏效我也想出了uploadFile。 – Bryan

+0

不客氣。既然你是新來者,不要忘了標記接受的答案,只要它有助於(大部分)理解和解決具體問題。另請參閱[接受答案的工作方式?](http://meta.stackexchange.com/a/5235)對[先前提出的問題]執行相同操作(http://stackoverflow.com/users/3987006/bryan ?tab = questions),如適用。 – BalusC

+0

再次感謝!我對你的評論有問題「只要操作方法沒有返回,服務器就不會返回帶有更新視圖的HTTP響應。」在一種情況下,我想上傳文件,然後將數據保存到數據庫。如果有任何數據是新的,我想提示一個窗口要求用戶決定是否保留數據。所以使用表單不能解決這個問題?你有什麼建議嗎? – Bryan

0

按照來自@BalusC的建議,這是我更新的代碼。

test.xhtml

<ui:define name="body"> 
     <h:form> 
         <h:commandButton value="Upload a file" action="#{UpdateBean.submit()}"> 
          <f:ajax render=":checkPassword" /> 
         </h:commandButton> 
     </h:form> 

     <h:form id="checkPassword" styleClass="toggle" rendered="#{UpdateBean.submitIsPerformed}"> 
      <table> 
       <tr> 
        <td><h:outputText value="Password" /></td> 
        <td><h:inputText id="password" value="#{UpdateBean.password}" /></td> 
        <td> 
        <h:commandButton value="Submit" action="#{UpdateBean.submitPassword()}"> 
         <f:ajax execute="password" render=":uploadFile" /> 
        </h:commandButton> 
        </td> 
       </tr> 
      </table> 
     </h:form> 

     <h:form id="uploadFile" enctype="multipart/form-data" 
       styleClass="toggle" rendered="#{UpdateBean.uploadFileIsPerformed}"> 

      <t:inputFileUpload id="uploadedFile" storage="file" 
           value="#{UpdateBean.uploadedFile}"> 
      </t:inputFileUpload> 
      <h:commandButton value="Submit" action="#{UpdateBean.uploadFile()}" /> 
     </h:form> 
    </ui:define> 

UploadBean.java

public String submit() { 

     setSubmitIsPerformed(true); 
     return "SUCCESS"; 
    } 

    public String submitPassword(){ 
     if(password.equals("123"){ 
      setUploadFileIsPerformed(true); 
      setSubmitIsPerformed(false); 
     } 
     return "SUCCESS"; 
    } 

    public String uploadFile(){ 
     return "SUCCESS"; 
    }