当前位置: 首页 > 知识库问答 >
问题:

当自动完成字段中有输入时,primefaces验证不起作用

夏令秋
2023-03-14

我有一个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。

谢谢大家!

很好的一天。

共有1个答案

郭志
2023-03-14

问题出在映射自动完成值上。对于第一个自动完成的示例,该值映射为“#{dprCreate.supplier代码}”,其类型为字符串-

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" />

其他的自动完成也是一样

 类似资料:
  • 问题内容: 我有一个禁用自动完成功能的表单,但是它无法正常工作,并且使自动完成功能在Firefox和更高版本的chrome中启用 当类型从密码更改为文本时,它可以在所有浏览器中使用。谁能帮助解决这个问题? 问题答案: 浏览器通常具有两个与表单相关但又不同的功能: 表单自动完成,类型(和类似类型)的项目收集键入的值并以下拉列表的形式提供给他们。 (这是一个简单的功能,效果很好。) 密码管理器,当浏览

  • 我有一个带有验证器的自动完成-如果我向自动完成器提交一个“AAA”的查询,那么我会得到一个以“AAA”开头的建议列表。我选择了“AAA001”,然后调用验证器,但在验证器中,当我期望“AAA001”时,值为“AAA”。 这是我的代码: 代码只是一个带有getter和setter以及我的验证器的字符串: 即时代码=‘AAA’,而不是预期的‘AAA001’。我添加了object属性只是为了确保对象上没

  • 问题内容: eclipse的自动完成功能现在无法使用,我搜索了几个小时,但找不到解决方案。我已经使用很长时间了,所以即使我已经安装了秋千,打开它之后的第一件事就是要安装SWT和Windows Builder。我的意思是,这可能是由于swt和swing引起的一些冲突(我读到一些有关冲突的信息,但如果可能的话,我不会)。 奇怪的是,当我按ctrl + space时它什么都没有显示,但是当我进入“编辑”

  • 我遵循了这个问题答案的教程:Kafka SASL动物园管理员身份验证 并且我设置了zookeeper.set。acl在服务器中为true。属性,但我仍然可以访问2181端口上的动物园管理员,任何人都可以通过:

  • 我刚接触spring,我使用《spring in action 5》一书对其进行了探索。我对我的网站做了类似的操作,我已经为页面编写了2个控制器和2个html文件。第一页是比萨饼制作表单。第二个是订单提交。我的控制器代码实际上与书中的相同。在我开始验证表单输入之前,一切都很顺利。我做了比萨饼、订购课程和控制器所需的一切。 订单类: 比萨饼类: 披萨制作控制器: 表单提交控制器: 问题在于验证比萨饼

  • 我的布局中有以下内容:; 我试图在<code>中使用的任何XML标记 我尝试了以下方法:; < li >文件- 似乎什么都不起作用。 智能感知/自动完成在标记外部工作,但不在标记内工作。 拜托,有人有解决方案吗? 我更改了<代码>