Custom Component - properties not available after form post

Hi.

I'm having some trouble with my first ever custom UIComponent in JSF.

Primarily, none of my properties are set following a form post. By this I mean that my component tag has a bunch of attributes like:

customStyle="color:red"

and this works on a GET request of my JSF page, but if I have an component that causes a post back (pardon the terminology - been an ASP.NET developer for a number of years) none of my setters get called.

I've got arround this by refering to my "properties" using:

this.getAttributes().get("customStyle")

and this works.

My component is a subclass of UIOutput and I'm not using a Tag handler implementation as I am using facelets as the view framework.

If anyone can explain to me what is happening I would be greatful.

Cheers

Andy

[870 byte] By [Mooso21a] at [2007-11-26 19:41:39]
# 1

If you are adding tag attributes to your component, you'll need a tag class as well. The reason being, is that the outputText tag doesn't know about the new attributes you've added. Thus, it doesn't know to add them to your custom component.

A tag class is really easy to put together. Create a new Java class that extends the OutputTextTag class. Implement the constructor and the setProperties method. The constructor should just call super(). Add a setter for each of your new attributes (the setters will take a String argument).

Then just write up a quick setProperties method. Base yours off of this example:

/**

* @see javax.faces.webapp.UIComponentTag#setProperties(UIComponent)

*/

protected void setProperties(UIComponent component) {

super.setProperties(component);

//Set the field reference property

if (fieldRef != null) {

if (UIComponentTag.isValueReference(fieldRef)) {

ValueBinding vb = getFacesContext().getApplication().

createValueBinding(fieldRef);

component.setValueBinding("fieldRef", vb);

} else {

component.getAttributes().put("fieldRef", fieldRef);

}

}

}

The attribute I've added to my code example is called FieldRef. Then in your custom component class, retrieve the 'FieldRef' attribute like this:

/**

* Gets the field reference name attribute.

*

* @return

*/

public String getFieldRef() {

if (fieldRef != null)

return fieldRef;

ValueBinding vb = getValueBinding("fieldRef");

if (vb != null)

return (String)vb.getValue(getFacesContext());

else

return null;

}

/**

* Sets the field reference name attribute. Is a short description of the

* field. This description can be used in any error message text.

*

* @param fieldRef

*/

public void setFieldRef(String fieldRef) {

this.fieldRef = fieldRef;

}

That should do it for you!

CowKing

IamCowKinga at 2007-7-9 22:23:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for that, but my issue is that I am using facelets, not JSP as the view framework, so Tags classes dont apply. You just write a UIComponent based class and register it with faces-config and facelets-config and off you go.

But this always leads to my problem because of the (in my opinion) dumb and overly complex way of setting up and retrieving component attributes within a custom component.

I guess I need to dig into some example components that work ok within facelets and see what they are doing. Maybe they have some underlying plumbing that is facelet specific in order to have their attributes processed properly.

Thanks anyway.

Mooso21a at 2007-7-9 22:23:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...