簡體   English   中英

具有POJO和CommandButton的Primefaces自動完成

[英]Primefaces autoComplete with POJO and CommandButton

使用Primefaces自動完成功能時遇到一些麻煩。 首先,我想顯示我的xhtml文件。

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:o="http://openfaces.org/"
xmlns:om="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions"
xmlns:p="http://primefaces.org/ui" >
<ui:composition template="/Template/basicTemplate.xhtml">
    <ui:define name="content">
        <h:form> <h1>Firma anlegen</h1>
        <h:panelGrid columns="2" cellpadding="5">
            <h:outputLabel for="name" value="Name:" style="font-weight:bold" />
            <p:inputText id="name" value="#{registerCompanyBean.company.name}" />
            <h:outputLabel for="name" value="Straße:" style="font-weight:bold" />
            <p:inputText id="street" value="#{registerCompanyBean.postalAddress.street}" />
            <h:outputLabel for="name" value="PLZ:" style="font-weight:bold" />
            <p:inputText id="zipCode" value="#{registerCompanyBean.postalAddress.zipCode}" />
            <h:outputLabel for="name" value="Ort:" style="font-weight:bold" />
            <p:inputText id="city" value="#{registerCompanyBean.postalAddress.city}" />
            <h:outputLabel for="name" value="Land:" style="font-weight:bold" />
            <p:inputText id="country" value="#{registerCompanyBean.postalAddress.country}" />

            <p:outputLabel value="Branche:" for="dd"  style="font-weight:bold" />
            <p:autoComplete id="dd" dropdown="true" value="#{registerCompanyBean.buisnessCategory.name}" completeMethod="#{registerCompanyBean.completeBuisnessCategory}"
                            var="buisnessCategory" itemLabel="#{buisnessCategory.name}" itemValue="#{buisnessCategory}"  converter="buisnessCategoryConverter" forceSelection="true" >
            </p:autoComplete>

        </h:panelGrid>

        <H1>Wartelisten</H1>
        <h:panelGrid columns="2" cellpadding="5">
        </h:panelGrid>
        <p:commandButton id="save" value="Save" action="#{registerCompanyBean.save}" ajax="false"/>
        </h:form>
    </ui:define>
    </ui:composition>
</html>

頁面開始沒有任何錯誤。 我可以填寫輸入字段,並可以使用方面的下拉框。 我什至可以使用autoComplete函數,但是當我按下CommandButton時,會收到NullPointerException。 如果我刪除了commandButton的ajax屬性,則CommandButton將不執行任何操作(根本不執行任何函數調用)。 因此,對於我來說,似乎必須添加ajax = false屬性。 有任何想法嗎???

謝謝

為了更好地理解,我添加了所有其他文件。

轉換器:

/**
*
* @author mibschmidt
*/
@FacesConverter("buisnessCategoryConverter")
public class BuisnessCategoryConverter implements Converter{

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                BuisnessCategoryService service = (BuisnessCategoryService) fc.getExternalContext().getApplicationMap().get("buisnessCategoryService");
                return service.findAll().get(Integer.parseInt(value));
            } catch(NumberFormatException e) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid BuisnessCategory."));
            }
        }
        else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            return String.valueOf(((BuisnessCategory) object).getID());
        }
        else {
            return null;
        }
    }   
} 

ManageBean:

@Controller
@Scope("session")
public class RegisterCompanyBean extends GenericCRUDController<Company, CompanyService> implements Serializable {


    @Autowired
    private BuisnessCategoryService buisnessCategoryService;
    private BuisnessCategory buisnessCategory;

    public BuisnessCategoryService getBuisnessCategoryService() {
        return buisnessCategoryService;
    }

    public void setBuisnessCategoryService(BuisnessCategoryService buisnessCategoryService) {
        this.buisnessCategoryService = buisnessCategoryService;
    }

    public BuisnessCategory getBuisnessCategory() {
        return buisnessCategory;
    }

    public void setBuisnessCategory(BuisnessCategory buisnessCategory) {
        this.buisnessCategory = buisnessCategory;
    }

    @Autowired
    private PostalAddressService postalAddressService;
    private PostalAddress postalAddress =new PostalAddress();

    public PostalAddressService getPostalAddressService() {
        return postalAddressService;
    }

    public void setPostalAddressService(PostalAddressService postalAddressService) {
        this.postalAddressService = postalAddressService;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    @Autowired
    private CompanyService companyService;
    private Company company= new Company();


    public PostalAddress getPostalAddress() {
        return postalAddress;
    }

    public void setPostalAddress(PostalAddress postalAddress) {
        this.postalAddress = postalAddress;
    }

    public CompanyService getCompanyService() {
        return companyService;
    }

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    public List<BuisnessCategory> getBuisnessCategoryList() {
        return buisnessCategoryService.findAll();
    }

    @Override
    protected CompanyService getService() {
        return companyService;
    }

    public void saveButtonAction(ActionEvent actionEvent) {
        addMessage("Speichern der Firma");
    }



    @Override
    public String save() {

        List <PostalAddress> pAL = new ArrayList<>();
        pAL.add(postalAddressService.save(postalAddress));
        company.setPostalAddress(pAL);
        company.setBuisnessCategory(buisnessCategoryService.save(buisnessCategory));
        company=companyService.save(company);
        if(company==null){
            addMessage("Fehler beim Registrieren der Firma");
        }else{
            addMessage(String.valueOf(company.getID()));
        }

        return super.save(); //To change body of generated methods, choose Tools | Templates.
    }

    public List<BuisnessCategory> completeBuisnessCategory(String query) {
        List<BuisnessCategory> allBuisnessCategory = buisnessCategoryService.findAll();
        List<BuisnessCategory> filteredBuisnessCategory = new ArrayList<>();

        for (int i = 0; i < allBuisnessCategory.size(); i++) {
            BuisnessCategory bk = allBuisnessCategory.get(i);
            if(bk.getName().toLowerCase().contains(query.toLowerCase())) {
                filteredBuisnessCategory.add(bk);
            }
        }

        return filteredBuisnessCategory;
    }

    public void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary,  null);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

}

錯誤消息:

at com.wedusch.waitlist.jsf.util.BuisnessCategoryConverter.getAsObject(BuisnessCategoryConverter.java:33)
at org.primefaces.component.autocomplete.AutoCompleteRenderer.getConvertedValue(AutoCompleteRenderer.java:604)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

使用前必須先初始化對象(buisnessCategory)( #{registerCompanyBean.buisnessCategory.name} )。

初始於構造函數

public RegisterCompanyBean(){
    buisnessCategory = new BuisnessCategory();
}

或由@PostConstruct初始化

@PostConstruct
public void init(){
   buisnessCategory = new BuisnessCategory(); 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM