tags:

views:

2402

answers:

3

I have Facelet component and I have backing bean for it. When I include my component to some page I pass bean from page to my component:

<ui:include src="./WEB-INF/templates/myTemplate.xhtml">
                    <ui:param name="pageBean" value="#{thisPageBean}" />
</ui:include>

My component also have backing bean and I try to pass value from invoking page bean to my:

<c:set target="#{componentBean}" property="field" value="#{pageBean.field}" />

But this method doesn't work? There is some way to pass value from one bean to another?

I am trying create difficult custom component that must do some validation and other action on things that was pass to it? Maybe I am in wrong way?

A: 

Usually you assign values to some input controls like:

<h:inputText value='#{pageBean.field}'/>

That implies both getting and setting the value of someField property. Please provide details on what should determine the value of #{pageBean.field} in your case.

Grzegorz Oledzki
+1  A: 

You could pass the bean as an attribute on the component (I assume when you say "component" you are using ui:component).

For a component:

<ui:component binding="#{componentBean.myComponent}">
  <f:attribute name="foo" value="#{pageBean.field}" />
  <h:outputText value="#{componentBean.something}" />
</ui:component>

You could have a backing bean with these methods:

private Object field;
private UIComponent component;

public void setMyComponent(UIComponent c) { component = c; }
public UIComponent getMyComponent() { return component; }

private void lookupFields() {
  field = component.getAttributes().get("foo");
}

public String getSomething() {
  if (field == null) {
    lookupFields();
  }
  return "" + field;
}

Not very elegant, but I'm not all that familiar with the mechanics of Facelets includes and this is the first thing that occurred to me. Note that the attributes might be persisted when the view is saved - I can't remember for stuff that gets set on the attributes map.

McDowell
Thank it's works for me, but I find some other way to do what I want.
masterzim
A: 

Here is some code from one of my facelets files. As you can see the value of the bean passed as a parameter, ie:

<ui:param name="bean" value="#{thisPageBean}" />

and the property of the bean, dto, can be accessed using the [dto] notation.

  <h:panelGroup>
   <h:selectOneMenu id="titleMenu" value="#{bean[dto].title}">
    <f:selectItems value="#{suburbsMBean.titles}" />
   </h:selectOneMenu>
   <h:message for="titleMenu" styleClass="error" />
  </h:panelGroup>
Martlark