Backing Bean Attributes
Posted by Steve on April 4th, 2007This article was originally posted (by me) at the Java Answers Forum
This has caused me considerable headache, but I think I’ve pinned it down now….
Suppose you have a backing bean with a getter and a setter named, for example, configItem – so you have a getConfigItem and setConfigItem.
For whatever reason you want to ensure your setter is called when the request URL has ?config=bah, so that the backing bean is prepopulated with required data even though you’ve not issued a Postback.
To do this is relatively straight forward, but has a HUGE wrinkle which I’ll iron out for you in a moment.
First, edit your faces-config.xml and add something like;
Code:
<managed-property>
<property-name>configItem</property-name>
<property-class>java.lang.String</property-class>
<value>#{param['config'] != null ? param['config'] : ''}</value>
</managed-property>
Now, assuming your backing bean is REQUEST scoped (I don’t think this will work if the bean is session scoped) the setter will be called when your page loads.
You may be tempted to call your parameter configItem instead, so you would expect http://foo.bar.com/baz.jsp?configItem=bob to work if you modified the faces-config.xml appropriately. This is the wrinkle. After much experimentation I have discovered that this ONLY works for LOWER CASED PARAMETERS….
?configitem=bob will work
?configItem=bob will silently fail.
Much hair pulling has led me to this. I have no idea why – perhaps the Faces Servlet automatically converts all parameters to lower case?
No related posts.