2015-01-07 46 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"; 
    }