成功完成字段验证后阻止用户界面

时间:2016-03-19 12:16:23

标签: forms jsf primefaces blockui

我使用带有Primefaces 5.3的JSF 2.2。我必须创建一个像这样工作的表单:

按下按钮后:

  • 如果填写了所有必填字段,只要数据被发送,页面就会被阻止(由于数据发送到数据库)。
  • 如果未填写其中一个必填字段,则不会阻止该页面。

你能告诉我怎么办?

这是一个xhtml页面:

<!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:

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;
}

当然上面的代码可以工作但每次按下按钮时页面都会被阻止(即使没有填写字段)。我想我应该在widgetVar组件中使用trigger而不是p:blockUI,但我不知道怎么也许我错了。对我来说最好的方法是,如果我可以从CDI bean中的send方法阻止/解锁页面,但我不知道它是否可能,除此之外 - 这不是必需的。每种方式对我都有帮助。

2 个答案:

答案 0 :(得分:1)

最后我找到了解决方案。这些是我做的步骤:

  1. 我使用widgetVar属性而不是blockUI组件中的trigger

    <p:blockUI block="page" widgetVar="widgetBlock">
        Sending of the data...          
    </p:blockUI>
    
  2. 我在DataController bean中创建了自己的验证方法:

    public boolean isValidation(){
    
        if(uploadFile==null || userTable.getName()==null || userTable.getName().trim().equals("")) return false;
        else return true;
    }
    
  3. 我添加了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()" />
    
  4. 我将commandButton id放入fileupload组件中的update属性。除此之外,我从fileupload中删除了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 />
    
  5. 我也从inputtext组件中删除了require属性并添加了<p:ajax>标记(我删除了此属性,因为<p:ajax>标记无法使用此属性像我想要的属性,看看我的帖子here):

    <p:inputText  id="tableName" value="#{userTable.name}" >
        <p:ajax event="keyup" update="buttonSend" />                    
    </p:inputText>
    
  6. 最后,我将字段的验证从xhtml页面移动到send bean中的DataController方法:

    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.", ""));           
        }
    }
    

答案 1 :(得分:0)

您可以检查验证是否失败,如

只需将rendered =“#{facesContext.validationFailed}”附加到您的blockui,并在表单提交时进行更新