2016-03-19 27 views
0

私は、Primefaces 5.3でJSF 2.2を使用しています。すべての必須フィールドが原因データベースへのデータの送信に(ページがブロックされている、で満たされている場合はフィールド検証に成功した後のUIのブロック

  • :ボタンを押した後

    :私はこのような作業になりますフォームを作成する必要があります)データが送信される限り。

  • 必須フィールドのいずれかが入力されていない場合、ページはブロックされません。

どうすればいいですか教えてください。

これは、XHTMLページです(それが動作上のコードもちろん

package com.system.controller; 

import java.io.Serializable; 
import java.util.logging.Logger; 

import javax.enterprise.context.RequestScoped; 
import javax.enterprise.inject.Produces; 
import javax.faces.application.FacesMessage; 
import javax.faces.context.FacesContext; 
import javax.faces.view.ViewScoped; 
import javax.inject.Inject; 
import javax.inject.Named; 

import org.primefaces.event.FileUploadEvent; 
import org.primefaces.model.UploadedFile; 

import com.system.model.UserDatabase; 
import com.system.service.DataService; 

@Named 
@ViewScoped 
public class DataController implements Serializable { 

    private static final long serialVersionUID = 1383572529241805730L; 

    public void handleFileUpload(FileUploadEvent event){ 
     uploadFile=event.getFile(); 

     FacesMessage message = new FacesMessage("Successful", event.getFile().getFileName() + " is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, message);   
    } 

    public String getFileName(){ 

     if(uploadFile==null) return ""; 
     else return uploadFile.getFileName();    
    } 

    public void send(){ 

     if(uploadFile==null){ 

      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "The file isn't uploaded", "You should upload a file")); 
     } 
     else{ 

      //Sending the data to the database... (tha page should be blocked) 

      try { 
       Thread.sleep(5000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      //After successful of sending the data, the page should be unlocked. 

      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "The data has been added.", ""));   
     } 
    } 

    @Named 
    @Produces 
    @RequestScoped 
    private UserDatabase userDatabase=new UserDatabase(); 

    @Inject 
    private DataService dataService; 

    @Inject 
    private Logger log; 

    private UploadedFile uploadFile; 
} 

が、ページが、私はボタンを押すたびにブロックされている:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:pm="http://primefaces.org/mobile" 
     xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"> 
    <h:head></h:head> 
    <h:body id="page"> 

     <f:metadata> 
      <f:viewAction action="#{loginController.start()}" /> 
     </f:metadata> 

     <p:growl id="messages" showDetail="true" /> 

     <h:form> 
      <p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign">      
       <p:outputLabel for="databaseName" value="Database name:" /> 
       <p:inputText id="databaseName" required="true" value="#{userDatabase.name}" /> 
       <p:outputLabel for="databaseFile" value="File:" /> 
       <p:fileUpload id="databaseFile" required="true" fileLimit="1" update="file messages" fileUploadListener="#{dataController.handleFileUpload}" mode="advanced" dragDropSupport="true" sizeLimit="1000000000" uploadLabel="Upload" cancelLabel="Delete" allowTypes="/(\.|\/)(txt|binetflow)$/" />    
       <h:panelGroup /> 
       <h:outputText id="fileDescription" value="#{dataController.fileName}" />       
       <p:commandButton id="buttonSend" value="Send" update="messages" action="#{dataController.send()}" />       
      </p:panelGrid> 

      <p:blockUI block="page" trigger="buttonSend"> 
       Sending of the data...    
      </p:blockUI>    

     </h:form> 

    </h:body> 
</html> 

これはCDI Beanですフィールドが塗りつぶされていなくても)。私はp:blockUIコンポーネントで isteadのtriggerを使用すべきだと思いますが、どうやって私が間違っているのかわかりません。私にとって最良の方法は、CDI Beanのsendメソッドからページをブロック/ロック解除できるかどうかですが、可能かどうかは分かりませんが、これは必要ありません。それぞれの方法は私の役に立つでしょう。

答えて

1

を提出しますときにそれを更新し確認することができます最後に、私は解決策を見つけました。私はDataController豆に私自身の検証メソッドを作成し

<p:blockUI block="page" widgetVar="widgetBlock"> 
    Sending of the data...   
</p:blockUI> 
    1. は私がblockUIコンポーネントでtriggerwidgetVar属性isteadを使用:これらは私がやった手順は次のとおりです
      public boolean isValidation(){ 
      
          if(uploadFile==null || userTable.getName()==null || userTable.getName().trim().equals("")) return false; 
          else return true; 
      } 
      
    2. 私はonstartを追加しました(ここで私は私の検証方法を呼び出し、私はブロックします)。私は、ページのロックを解除するページ私はtrueを取得する場合)とoncompleteは()のcommandButton属性:

      <p:commandButton id="buttonSend" value="Send" action="#{dataController.send()}" update="messages" 
             onstart="if(#{dataController.validation}){PF('widgetBlock').show()}" 
             oncomplete="PF('widgetBlock').hide()" /> 
      
    3. は、私はファイルアップロードコンポーネントでupdate属性にコマンドidを置きます。それとは別に、私はファイルアップロードからrequire属性を削除:

      <p:fileUpload id="file" 
             fileLimit="1" 
             update="buttonSend fileDescription messages" 
             fileUploadListener="#{dataController.handleFileUpload}" 
             mode="advanced" dragDropSupport="true" sizeLimit="1000000000" 
             uploadLabel="Upload" cancelLabel="Delete" allowTypes="/(\.|\/)(txt|binetflow)$/" />    
      <h:panelGroup /> 
      
    4. 私ものinputTextコンポーネントからrequire属性を削除すると<p:ajax>タグを追加<p:ajax>タグはこれで動作しないので(私はこの属性を削除しました私が欲しいのように、属性)私のポストhereを見て:

      <p:inputText id="tableName" value="#{userTable.name}" > 
          <p:ajax event="keyup" update="buttonSend" />      
      </p:inputText> 
      
    5. 終わりに私はに私のsend方法にXHTMLページからフィールドの検証を移動しました豆:

      public void send(){    
      
          boolean validation=true; 
      
          if(userTable.getName()==null || userTable.getName().trim().equals("")){ 
      
           FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "The name field isn't filled", "You should fill this field")); 
      
           validation=false; 
          } 
      
          if(uploadFile==null){ 
      
           FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "The file isn't uploaded", "You should upload a file")); 
      
           validation=false; 
          }  
      
          if(validation){ 
      
           //Sending the data to the database... (tha page is blocked) 
      
           try { 
            Thread.sleep(5000); 
           } catch (InterruptedException e) { 
            e.printStackTrace(); 
           } 
      
           //After successful of sending the data, the page is unlocked. 
      
           FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "The data has been added.", ""));   
          } 
      } 
      
  • 0

    あなたは検証がこの

    のように失敗した場合は、ちょうどあなたのblockuiにレンダリング=「#{facesContext.validationFailedを}」添付してもフォームが

    +0

    私はあなたのアイデアを実装しようとしたが、それは動作しませんか私が何か間違ったことをしました。 blockUIコンポーネントに 'id'と' rendered'を追加しました。それ以外は、commandButtonコンポーネントの 'update'属性にblockUI 'id'を追加しました。その後、blockUIコンポーネントはまったく動作しません。なぜあなたは考えていますか?まず第一に、私がレンダリング= {{}のためにEclipseでコンテンツアシスタントを使用したとき、EclipseがfacesContextオブジェクトを表示していないことに気付きました。多分私は何かを追加することを忘れてしまったでしょうか? – Robert

    +1

    コンテンツアシストは表現言語afaik のために働いていませんか? (#{facesContext.validationFailed()}){ウィジェットのようにあなたのコメントであなたがそれなしでそれを持っている場合 あなたのブロックボタンにあなたのコマンドボタンのonstartまたはonclickにウィジェットvarを追加することを試みることができます。 –

    +0

    もちろん私のコードに '#{}'を入れています。私はコメントでそれを忘れてしまった。残念ながら、2番目のアイデアはどちらもうまくいきません。私の意見では、問題は '#{facesContext.validationFailed()}'です。私は戻り値をチェックし、それぞれの場合にはまだ 'false'を取得します。なぜあなたは考えていますか? – Robert

    関連する問題