【发布时间】:2016-03-19 12:16:23
【问题描述】:
我使用 JSF 2.2 和 Primefaces 5.3。我必须创建一个像这样工作的表单:
按下按钮后:
- 如果填写了所有必填字段,则只要发送数据,页面就会被阻止(由于向数据库发送数据)。
- 如果未填写必填字段之一,则不会阻止页面。
你能告诉我该怎么做吗?
这是一个 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;
}
上面的代码当然可以,但是每次我按下按钮时页面都会被阻止(即使没有填写字段)。我想我应该在p:blockUI 组件中使用widgetVar 而不是trigger,但我不知道怎么做,也许我错了。对我来说最好的方法是如果我可以从 CDI bean 中的 send 方法阻止/解锁页面,但我不知道这是否可能,除此之外 - 这不是必需的。每一种方式都会对我有所帮助。
【问题讨论】:
标签: forms jsf primefaces blockui