【问题标题】:Data from JSF page (XHTML) is not setting into the object whose reference passed in managed bean.来自 JSF 页面 (XHTML) 的数据未设置到其引用在托管 bean 中传递的对象中。
【发布时间】:2026-01-31 09:10:01
【问题描述】:
    My XHTML:-->

    <?xml version="1.0" encoding="ISO-8859-1" ?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"`enter code here`
     xmlns:f="http://java.sun.com/jsf/core"
     xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui"
    template="/templates/mainTemplatePage.xhtml">

    <ui:define name="content">
        <h:form id="addProduct">


    <f:loadBundlebasename="com.erudishield.framework.propertyfile.
    ScreenConstants"
                var="constants" />
            <f:loadBundle

    basename="com.erudishield.framework.propertyfile.ScreenButtonNameConstants"
                var="buttonConstant" />
            <p:panel id="addProductPanel">

                <h:panelGrid columns="3">
                    <h:outputLabel value="Add Product" />

            <!--        <h:outputLabel
                        value="#
    {addProductBB.screenDisplayData.message.screenResponseMessageForAddProduct}"
                        rendered="#
    {addProductBB.renderer.productDataRenderer.productTextRenderer}"
                        style="#  
    {productMaintenanceBB.screenDisplayData.message.screenResponseMessageColor}"
                        styleClass="outputText" escape="false">
                    </h:outputLabel>
     -->
                </h:panelGrid>

                <h:panelGrid columns="2" cellpadding="5">
                    <h:outputText id="outputText" value="#
    {constant.screenDisplayData.productName}"></h:outputText>
                    <p:inputText id="addProductInput"
                        value="#{addProductBB.screenDisplayData.productName}">  
    </p:inputText>
                </h:panelGrid>

                <p:spacer width="415" height="20" />
                <h:panelGrid>
                    <p:commandButton value="#{buttonConstant.addProduct}"
                        update=":addProduct" action="#         
    {addProductBB.addProductAction}">    </p:commandButton>
                </h:panelGrid>

            </p:panel>

        </h:form>
    </ui:define>
    </ui:composition>

-------------------------------------------

    Managed Bean:

    package com.erudishield.erudishieldweb.bb.businessProcess;

    import java.io.Serializable;

    import javax.annotation.PostConstruct;
   import javax.faces.bean.ManagedBean;
   import javax.faces.bean.ViewScoped;

   import com.erudishield.erudishieldutil.validator.StringValidator;
    import com.erudishield.erudishieldweb.bb.businessProcess.bo.
    AddProductScreenDisplayData;
     import com.erudishield.erudishieldweb.bd.AddProductBD;
    import com.erudishield.erudishieldweb.bo.businessProcess.ProductRenderer;
    import com.erudishield.framework.application.constants.ApplicationConstants;
    import com.erudishield.framework.messages.MessageConstants;
    import com.erudishield.service.businessprocess.bo.AddProductBO;
    import com.erudishield.service.businessprocess.bo.AddProductPKBO;
    import    com.erudishield.service.businessprocess.bo.
    AddProductScreenResponseMessageBO;
    import com.erudishield.service.businessprocess.bo.ProductBO;
    import com.erudishield.service.businessprocess.bo.ProductPKBO;

    @ManagedBean
    @ViewScoped
    public class AddProductBB implements Serializable {

    private static final long serialVersionUID = -1089990299636039243L;

    private AddProductScreenDisplayData screenDisplayData;
    private ProductRenderer renderer;
    private String addProductName;
    private AddProductScreenResponseMessageBO message;

    public AddProductScreenDisplayData getScreenDisplayData() {

        return screenDisplayData;
    }

    public void setScreenDisplayData(
            AddProductScreenDisplayData screenDisplayData) {
        this.screenDisplayData = screenDisplayData;
    }

    public ProductRenderer getRenderer() {

        return renderer;
    }

    public void setRenderer(ProductRenderer renderer) {
        this.renderer = renderer;
    }


    @PostConstruct
    public void init() {

          AddProductScreenDisplayData screenDisplayData = new
          AddProductScreenDisplayData();
          setScreenDisplayData(screenDisplayData);
         ProductRenderer renderer = new ProductRenderer();
         setRenderer(renderer);

    }

    public void addProductAction() {
        System.out.println("Entering in method addProductAction");
    /*        initializeAllScreenMesages();
          getMessage().setScreenResponseMessageForAddProduct("");
    */       
        if (productDataValidation()) {
            getScreenDisplayData().getMessage().setScreenResponseMessageColor
    (ApplicationConstants.ERROR_MESSAGE_COLOR);
        }
        else {

            AddProductBO addProductBO = convertScreenDataToAddProductBO();

            AddProductBO productBO = new AddProductBD().addProduct(
                    addProductBO, getMessage());

            if (productBO.getId() == 0) {

                commonScreenMessageRendering(
                        ApplicationConstants.ERROR_MESSAGE_COLOR,
                        getScreenDisplayData().getMessage()
                                .getScreenResponseMessageForAddProduct(),"Add");
            } else {
                commonScreenMessageRendering(
                        ApplicationConstants.SUCCESS_MESSAGE_COLOR,
                        MessageConstants.SUCCESS_PRODUCT_CREATED,"Add");
            }
        }
    }

    /*
     * public void commonMessageRendering(String message, String messageColor) {
     * getScreenDisplayData().getMessage().setScreenResponseMessageColor(
     * messageColor); getScreenDisplayData().getMessage()
     * .setScreenResponseMessageForAddProduct(message);
     * 
     * }
     */

    public void commonScreenMessageRendering(String message,
            String messageColor, String action) {

        if (action.equals("Add")) {
            getScreenDisplayData().getMessage()
                    .setScreenResponseMessageForAddProduct(message);
        }
        getScreenDisplayData().getMessage().setScreenResponseMessageColor(
                messageColor);
    }

    public void initializeAllScreenMesages() {
        getScreenDisplayData().setMessage(
                new AddProductScreenResponseMessageBO());

    }

    public AddProductBO convertScreenDataToAddProductBO() {
        AddProductBO addProductBO = new AddProductBO();
        addProductBO.setProductName(getAddProductName());
        return addProductBO;
    }

    public boolean productDataValidation() {
        boolean isError = false;

        if       (StringValidator.isBlankOrNull(getScreenDisplayData().
    getProductName())) {
            //getScreenDisplayData().setProductName("");
            getScreenDisplayData().getMessage()
                    .addScreenResponseMessageForAddProductPanel(
                            MessageConstants.ERROR_PRODUCT_NAME_REQUIRED);
            isError = true;
        } else if (getAddProductName().length() >= 100) {
            //setAddProductName("");
            getScreenDisplayData().getMessage()
                    .addScreenResponseMessageForAddProductPanel(
                            MessageConstants.ERROR_LENTHIER_PRODUCT_NAME);
            isError = true;
        }
        return isError;
    }

    public String getAddProductName() {
        return addProductName;
    }

    public void setAddProductName(String addProductName) {
        this.addProductName = addProductName;
    }

    public AddProductScreenResponseMessageBO getMessage() {
        return message;
    }

    public void setMessage(AddProductScreenResponseMessageBO message) {
        this.message = message;
    }

    }

-------------------------------------------------------
    AddProductScreenDisplayData class

    package com.erudishield.erudishieldweb.bb.businessProcess.bo;

    import java.io.Serializable;
    import java.util.List;

    import       com.erudishield.service.businessprocess.bo.
    AddProductScreenResponseMessageBO;

    public class AddProductScreenDisplayData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8033274203833911923L;

    private String productName;
    private AddProductScreenResponseMessageBO message;
    private String testScreenDisplay;

    //  private List<Object> productList;


    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public AddProductScreenResponseMessageBO getMessage() {
        return message;
    }

    public void setMessage(AddProductScreenResponseMessageBO message) {
        this.message = message;
    }

    public String getTestScreenDisplay() {
        return testScreenDisplay;
    }

    public void setTestScreenDisplay(String testScreenDisplay) {
        this.testScreenDisplay = testScreenDisplay;
    }
    }

================================================ ========== 我的问题是,当我从用户产品名称中获取输入并将其设置为 AddProductScreenDisplayData 对象中的变量 productName 时,它​​设置为 null(似乎设置为 null)。和代码中断。我放了 System.out.println() 语句来确保它是否进入了 addProductAction() 方法。流程正在进入该方法,但是当检查的验证很少时,代码流程再次中断。但是,如果我在托管 bean 本身中设置产品名称,而不是在 AddProductScreenDisplayData 中设置,那么一切正常。请帮帮我。问题出在哪里

【问题讨论】:

  • 请更正您的代码布局。把它分成多个块。这种方式不可读
  • 感谢投票而不是任何解决方案。我已经解决了这个问题。
  • 不客气。而且,如果您阅读了一些有关否决的元帖子,您会发现像您一样抱怨而不是对我昨天所做的评论做任何事情,您将来获得帮助的机会肯定不会增加。祝你好运
  • 哦,下次如果您发布代码,还请阅读 jsf 信息页面中的 mcve 和 How to Ask

标签: java jsf-2


【解决方案1】:

我必须在 BO 和屏幕显示数据中设置值才能正确访问对象。现在已经解决了。

【讨论】:

  • 我怀疑很多人会理解您的答案,原因有几个。最重要的是难以将其与您的问题内容联系起来(由于不可读的代码)
最近更新 更多