我有一个jsf表单,它包含p:autoCompelete字段。你在这里;
<h:form prependId="false" id="form1" enctype="multipart/form-data">
<!-- DPR FORM -->
<p:panel header="Delivery Problem Report">
<h:panelGrid columns="2">
<p:outputLabel value="To (Supplier) :" for="supplierAuto" />
<p:autoComplete id="supplierAuto" dropdown="true" value="#{dprCreate.supplierCode}"
completeMethod="#{dprCreate.completeSupplier}"
var="supplier" itemLabel="#{supplier.supplierName}" itemValue="#{supplier.supplierCode}"
scrollHeight="250" required="true" />
<h:outputText value="DPR Date: *" style="font-size: small" />
<p:calendar id="dprDate" value="#{dprCreate.dprDate}"
pattern="dd/MM/yyyy" locale="en" title="Begin Date" />
<h:outputText value="Invoice No:" />
<p:inputText value="#{dprCreate.invoiceNo}" >
</p:inputText>
<h:outputText value="Waybill No:" />
<p:inputText value="#{dprCreate.waybill}" >
</p:inputText>
<p:outputLabel value="Part Number:" for="PartNumberAuto" />
<p:autoComplete id="PartNumberAuto" dropdown="true" value="#{dprCreate.partNumber}"
completeMethod="#{dprCreate.completePartNumbers}"
var="partNumber" itemLabel="#{partNumber.partNumber}" itemValue="#{partNumber.partNumber}"
scrollHeight="250" required="true" />
<h:outputText value="Color Code:" />
<p:inputText value="#{dprCreate.color}" >
</p:inputText>
<h:outputText value="Quantity:" />
<p:inputText value="#{dprCreate.quantity}" >
</p:inputText>
<p:outputLabel value="Problem Type:" for="ProblemAuto" />
<p:autoComplete id="ProblemAuto" value="#{dprCreate.problemId}"
completeMethod="#{dprCreate.completeProblems}" dropdown="true"
var="problem" itemLabel="#{problem.problemName}" itemValue="#{problem.problemId}"
scrollHeight="250" required="true"/>
<p:commandButton action="#{dprCreate.save}" value="Save" update="form1" ajax="false" style="width:150px" />
<p:button outcome="/dpr/list" value="See All DPR" style="width:150px"/>
</h:panelGrid>
</p:panel>
</h:form>
表格中有3种验证。它们是“必需=真”。此外,它们位于p:自动填充字段中。
所以,问题是当我点击没有输入的“保存按钮”时,验证工作正常。 但是,当我在一个p:autocomplete字段中输入一个输入然后单击“保存”按钮时,primefaces验证不起作用,我得到java错误。我的意思是我希望得到前端验证错误,因为其他两个空的必填字段。但是系统通过了前端验证,它到达我的后端代码。
这是我的java bean代码。
package com.honda.srm.controller;
import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import com.honda.srm.dao.ConnectionManager;
import com.honda.srm.model.Authentication;
import com.honda.srm.dao.PartNumberDAO;
import com.honda.srm.dao.ProblemDAO;
import com.honda.srm.dao.DprDAO;
import com.honda.srm.model.PartNumber;
import com.honda.srm.model.Problem;
import com.honda.srm.model.Dpr;
import com.honda.srm.model.Supplier;
import com.honda.srm.model.User;
import com.honda.srm.controller.SendEmail;
import com.honda.srm.dao.SupplierDAO;
import com.honda.util.FacesUtil;
import java.io.IOException;
import javax.mail.MessagingException;
@ManagedBean(name = "dprCreate")
@ViewScoped
public class DprCreate implements Serializable {
private String formId;
private String to;
private Date dprDate = new Date();
private Date formDate = new Date();
private String invoiceNo;
private String waybill;
private String partNumber;
private String color;
private Integer quantity;
private String supplierCode;
private List<Problem> problems;
private List<Supplier> suppliers;
private List<Dpr> unreportedDprs;
private List<Dpr> dprsOfSupplier;
private Integer problemId;
private Integer statusId;
private String filledBy;
public Dpr dpr = new Dpr();
private User user;
public String username;
private List<Supplier> selectedSuppliers;
public DprCreate() throws ClassNotFoundException, SQLException {
Authentication auth = (Authentication) FacesUtil
.getSessionAttribute("auth");
user = auth.getUser();
username=user.getUsername();
unreportedDprs = new DprDAO(ConnectionManager.getConnection())
.getunreportedDprs();
dprsOfSupplier = new DprDAO(ConnectionManager.getConnection())
.getDprsOfSupplier(username);
suppliers = new SupplierDAO(ConnectionManager.getConnection())
.getSuppliers();
}
public List<Supplier> completeSupplier(String query) throws ClassNotFoundException, SQLException {
List<Supplier> allSuppliers = new SupplierDAO(ConnectionManager.getConnection())
.getSuppliers();
List<Supplier> filteredSuppliers = new ArrayList<Supplier>();
for (int i = 0; i < allSuppliers.size(); i++) {
Supplier supplier = allSuppliers.get(i);
if(supplier.getSupplierName().toLowerCase().startsWith(query)) {
filteredSuppliers.add(supplier);
}
}
return filteredSuppliers;
}
public List<Problem> completeProblems(String query) throws ClassNotFoundException, SQLException {
List<Problem> allProblems = new ProblemDAO(ConnectionManager.getConnection())
.getAllProblems();
List<Problem> filteredProblems = new ArrayList<Problem>();
for (int i = 0; i < allProblems.size(); i++) {
Problem problem = allProblems.get(i);
if(problem.getProblemName().toLowerCase().startsWith(query)) {
filteredProblems.add(problem);
}
}
return filteredProblems;
}
public List<PartNumber> completePartNumbers(String query) throws ClassNotFoundException, SQLException {
List<PartNumber> allPartNumber = new PartNumberDAO(ConnectionManager.getConnection())
.getPartNumbers();
List<PartNumber> filteredPartNumbers = new ArrayList<PartNumber>();
for (int i = 0; i < allPartNumber.size(); i++) {
PartNumber partNumber = allPartNumber.get(i);
if(partNumber.getPartNumber().toLowerCase().startsWith(query)) {
filteredPartNumbers.add(partNumber);
}
}
return filteredPartNumbers;
}
private String createFormNumber()
throws ClassNotFoundException, SQLException {
String query = " CALL TESTAE.FORMSEQR (?)";
CallableStatement cstmt = ConnectionManager.getCallableStatement(query);
cstmt.registerOutParameter(1, Types.VARCHAR);
cstmt.execute();
String as400formid = cstmt.getString(1);
cstmt.close();
return as400formid;
}
public String save() throws ClassNotFoundException, SQLException, ParseException, MessagingException, IOException {
DprDAO dprDAO = new DprDAO(ConnectionManager.getConnection());
dpr.setFormId(createFormNumber());
dpr.setTo(supplierCode);
dpr.setDprDate(dprDate);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date today = new Date();
formDate = formatter.parse(formatter.format(today));
dpr.setFormDate(formDate);
dpr.setInvoiceNo(invoiceNo);
dpr.setWaybill(waybill);
dpr.setPartNumber(partNumber);
dpr.setColor(color);
dpr.setQuantity(quantity);
dpr.setProblemId(problemId);
dpr.setStatusId(1);
dpr.setFilledBy(username);
dprDAO.insertHeader(dpr);
SendEmail.main(dpr.getFormId(), dpr.getTo(), dpr.getStatusId());
return "/dpr/create?faces-redirect=true";
}
public List<Supplier> getSuppliers() {
return suppliers;
}
public void setSuppliers(List<Supplier> suppliers) {
this.suppliers = suppliers;
}
public String getSupplierCode() {
return supplierCode;
}
public void setSupplierCode(String supplierCode) {
this.supplierCode = supplierCode;
}
public String getFormId() {
return formId;
}
public void setFormId(String formId) {
this.formId = formId;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public Dpr getDpr() {
return dpr;
}
public void setDpr(Dpr dpr) {
this.dpr = dpr;
}
public Date getDprDate() {
return dprDate;
}
public void setDprDate(Date dprDate) {
this.dprDate = dprDate;
}
public String getInvoiceNo() {
return invoiceNo;
}
public void setInvoiceNo(String invoiceNo) {
this.invoiceNo = invoiceNo;
}
public String getWaybill() {
return waybill;
}
public void setWaybill(String waybill) {
this.waybill = waybill;
}
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public List<Problem> getProblems() {
return problems;
}
public void setProblems(List<Problem> problems) {
this.problems = problems;
}
public Integer getProblemId() {
return problemId;
}
public void setProblemId(Integer problemId) {
this.problemId = problemId;
}
public Integer getStatusId() {
return statusId;
}
public void setStatusId(Integer statusId) {
this.statusId = statusId;
}
public String getFilledBy() {
return filledBy;
}
public void setFilledBy(String filledBy) {
this.filledBy = filledBy;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Dpr> getUnreportedDprs() {
return unreportedDprs;
}
public void setUnreportedDprs(List<Dpr> unreportedDprs) {
this.unreportedDprs = unreportedDprs;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getFormDate() {
return formDate;
}
public void setFormDate(Date formDate) {
this.formDate = formDate;
}
public List<Dpr> getDprsOfSupplier() {
return dprsOfSupplier;
}
public void setDprsOfSupplier(List<Dpr> dprsOfSupplier) {
this.dprsOfSupplier = dprsOfSupplier;
}
public List<Supplier> getSelectedSuppliers() {
return selectedSuppliers;
}
public void setSelectedSuppliers(List<Supplier> selectedSuppliers) {
this.selectedSuppliers = selectedSuppliers;
}
}
后端错误;
root cause
javax.el.PropertyNotFoundException: /dpr/create.xhtml @27,58 itemLabel="#{supplier.supplierName}": Property 'supplierName' not found on type java.lang.String
com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
org.primefaces.component.autocomplete.AutoComplete.getItemLabel(AutoComplete.java:148)
org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeInput(AutoCompleteRenderer.java:190)
org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeSingleMarkup(AutoCompleteRenderer.java:147)
org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeMarkup(AutoCompleteRenderer.java:130)
org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeEnd(AutoCompleteRenderer.java:108)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:81)
org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:204)
org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:121)
org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:58)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.honda.service.UTF8EncodingFilter.doFilter(UTF8EncodingFilter.java:30)
你有什么想法吗?
PS:我使用的是jsf 2.2,primafaces 5.0,Eclipse Luna。 Wondows 7。
谢谢大家!
美好的一天。
答案 0 :(得分:0)
问题在于映射自动填充值。 例如,对于第一个自动完成,该值映射为&#34;#{dprCreate.supplierCode}&#34;它的类型为String - &gt;你必须声明一个#&#34;供应商&#34;在您的managedBean中(不要忘记实例化他)
private Supplier supplier
并使用此对象映射值
<p:autoComplete id="supplierAuto" dropdown="true" value="#{dprCreate.supplier}"
completeMethod="#{dprCreate.completeSupplier}"
var="supplier" itemLabel="#{supplier.supplierName}" itemValue="#{supplier}"
scrollHeight="250" required="true" />
和其他自动填充相同