You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

复合组件中p:inputText无法设置Managed Bean属性问题排查

问题:PrimeFaces复合组件内的p:inputText无法更新实体fieldValue属性

在集成PrimeFaces与JSF的Java Spring Boot项目中,定义了包含泛型对象列表的通用控制器,以及基于实体类实现的具体控制器。当前遇到的问题是:复合组件内的PrimeFaces p:inputText 无法更新实现类实体的fieldValue属性,提交后该值仍为null。


通用控制器

public abstract class TreeSearchController<S extends TreeSearchParamsEntity<S>> {

    final private TreeSearchService<S> service;
    final private UserSession userSession;
    protected List<S> searchParamsList;
    protected String treeType;

    public TreeSearchController(TreeSearchService<S> service, String treeType, UserSession userSession) {
        this.service = service;
        this.treeType = treeType;
        this.userSession = userSession;
        getSearchParamsList();
    }

    public List<S> getSearchParamsList() {
        System.out.println("GET CALLED");
        if (Objects.isNull(searchParamsList)) {
            System.out.println("GET CALLED WITH NULL");
            searchParamsList = loadSearchParamList(treeType);           
        }
        return searchParamsList;
    }

    public void setSearchParamsList(List<S> searchParamsList) {
        System.out.println("SET CALLED");
        this.searchParamsList = searchParamsList;
    }


    protected List<S> loadSearchParamList(String treeType) {
        return service.getSearchParamList(treeType);
    }

    protected String getSearchJsonString(String operator) {
        System.out.println("SEARCH CALLED");
        /* for (var entry : searchParamsList) {
            System.out.println(entry.getLabelName());
            System.out.println(entry.getFieldValue());
        } */
        return service.getSearchJsonString(searchParamsList, operator);
    }

    // --GETTERS AND SETTERS--

    public String getTreeType() {
        return treeType;
    }


    public void setTreeType(String treeType) {
        this.treeType = treeType;
    }

    public TreeSearchService<S> getService() {
        return service;
    }

    public UserSession getUserSession() {
        return userSession;
    }
}

泛型实体接口

public interface TreeSearchParamsEntity<S> {
    String getTreeType();

    void setTreeType(String treeType);

    String getTableName();

    void setTableName(String fieldName);
    
    String getFieldName();

    void setFieldName(String fieldName);
    
    String getLabelName();

    void setLabelName(String labelName);
    
    String getDataType();

    void setDataType(String dataType);
    
    String getFieldValue();

    void setFieldValue(String fieldValue);
}

具体控制器实现

@Named
@ViewScoped
public class FinancialTreeSearchController extends TreeSearchController<FinancialSearchFieldsClass> implements Serializable {
    private final FinancialTreeController treeController;

    @Inject
    public FinancialTreeSearchController(UserSession userSession, FinancialSearchService financialSearchService, FinancialTreeController treeController) {
        super(financialSearchService, "HIERARCHY", userSession);
        this.treeController = treeController;
    }

    public void findFinClass() {
        treeController.findFinClass(getSearchJsonString("AND"));
    } 

    public FinancialTreeController getTreeController() {
        return treeController;
    }

}

对话框控制器

@Named
@ViewScoped
public class FinancialClassSearchDlgController {    
    private final FinancialTreeSearchController financialTreeSearchController;
    

     @Inject
    public FinancialClassSearchDlgController(FinancialTreeSearchController financialTreeSearchController) {        
        this.financialTreeSearchController = financialTreeSearchController;
    }

   
    public void onFind() {
        PrimeFaces.current().dialog().closeDynamic(financialTreeSearchController.getSearchParamsList());        
    }

    public void onCancel() {
        PrimeFaces.current().dialog().closeDynamic(null);
    }

    public FinancialTreeSearchController getFinancialTreeSearchController() {
        return financialTreeSearchController;
    }   
}

主页面xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:search="http://xmlns.jcp.org/jsf/composite/resources/components/tree-search">

<h:head>
    <f:facet name="first">
        <meta http-equiv="X-UA-Compatible"
            content="IE=edge" />
        <meta http-equiv="Content-Type"
            content="text/html; charset=UTF-8" />
        <meta name="viewport"
            content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
        <meta name="apple-mobile-web-app-capable"
            content="yes" />
    </f:facet>
    <title>Search Hierarchy</title>
</h:head>

<h:body>
    <h:form prependId="false">
        <p:toolbar styleClass="sub-toolbar">
            <p:toolbarGroup>
                <p:commandButton value="Find"
                    styleClass="p-mr-2 p-mb-2"
                    action="#{financialClassSearchDlgController.onFind}"
                    process="@form" />
                <p:commandButton value="Cancel"
                    styleClass="p-mr-2 p-mb-2"
                    action="#{financialClassSearchDlgController.onCancel}"
                    process="@this" />
            </p:toolbarGroup>
        </p:toolbar>
        <search:tree-search controller="#{financialTreeSearchController}" />
    </h:form>

    <h:outputStylesheet name="omega-layout/css/primeicons.css" />
    <h:outputStylesheet name="omega-layout/css/primeflex.min.css" />
    <h:outputStylesheet name="omega-layout/css/layout-blue.css" />
    <h:outputStylesheet name="imms-layout/css/layout-custom.css" />
</h:body>

</html>

复合组件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:p="http://primefaces.org/ui"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:cc="http://xmlns.jcp.org/jsf/composite">

<cc:interface>
    <cc:attribute name="controller"
        required="true"
        type="com.imms.components.treeSearch.TreeSearchController" />

</cc:interface>
<cc:implementation>
    <p:card>
        <p:scrollPanel>
            <p:dataTable var="entry"
                value="#{cc.attrs.controller.searchParamsList}">
                <p:column headerText="Field Name">
                    <p:outputLabel value="#{entry.labelName}:"
                        styleClass="form-label" />
                </p:column>
                <p:column headerText="Value">
                    <p:inputText id="#{entry.fieldName}"
                        value="#{entry.fieldValue}" />
                </p:column>
            </p:dataTable>
        </p:scrollPanel>
    </p:card>
</cc:implementation>

</html>

已尝试的方案

  • 在closeDialog()中返回实现类实体列表
  • 在closeDialog()中返回字符串等任意内容
  • 在复合组件的dataTable上方添加inputText尝试更新列表首个值,未成功
  • 更换复合组件属性中的变量
  • 多次调试排查

内容的提问来源于stack exchange,提问作者DelonPrinsloo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 04:35:54