How to update a composite component form from anot

2019-08-07 06:02发布

问题:

I'm having problem trying to update an external form. To be more clear, I have a primary form that includes 2 different composite components, lets call include1 and include2. The page I want to update is the include2 being update after a search from a include1.

this is how the 2 pages are being included.

<ui:define name="include1">
    <ui:param name="mbean" value="#{currentBean}" />
    <libcomp:include1 />
</ui:define>

<ui:define  name="include2">
    <ui:param name="mbean" value="#{currentBean}" />
    <libcomp:include2>
</ui:define>

Now, in include1 I have a button that tries to update the form inside include2

update="include2Form"

and in the include2 I have

<cc:implementation>
    <h:form 
        id="include2Form">

When I try to load the page I always get an Error 500 saying that the "include2Form" has not been found!

I tried some research before coming here but none helped me, I tried to change the form to a div, tried to pass id by parameter, a panel, form with prependId=false etc...

Using fireBug I found out that JSF or Primefaces is adding an random String to my form/components ID...as

id="j_idt99:include2Form:myTable"

I think that is the reason of my problem and I'm trying to find a work around.

Could anyone help me please???

回答1:

First of all, the additional string in your ID directly comes frome JSF, unrelated to PrimeFaces, because the Composite Component itself is a UINamingContainer. This is expected behavoir and even necessary, because otherwise you would end up in duplicate ID conflicts, when using the same CC multiple times in the same view.

In my opinion it is bad design to have a form inside a Composite Component at all. To improve reusability just get rid of that form and work with process, partialSubmit, maybe f:subView etc..

Beside that you should rethink your design. The point that one Composite Component has to update s.th. outside the component might be an indicator, that a Composite Component is not the perfect approach to cover your requirements.

However, if you really have to update some parts of your view outside the composite component, just pass the ID of what to update via a composite attribute to your CC and let the CC not care about what and where to update

<composite:interface>
    <composite:attribute name="update" required="true" />
</composite:interface>