set property of BackingBean from the view (no action event with GET request components)
imagine there is BeanA bound to ViewA.xhtml and BeanB bound to ViewB.xhtml. BeanA and BeanB are backing beans with just one attribute per bean: "Object obj;" with getters and setters.
Now i am in ViewA doin开发者_开发百科g the following:
<ui:repeat value="#{someOtherBean.listOfObjects}" var="objSelected">
<h:commandLink value="someValue" action="ViewB">
<f:setPropertyActionListener target="#{beanB.obj}" value="#{objSelected}" />
</h:commandLink>
</ui:repeat>
This works great! I iterate through a List and the object i click on (via commandLink) is set into BeanB.
Problem: I need to work with a GET Request, so i have so use h:link or h:outputLink (btw, where is the difference?). But doing so, i can't use the f:setPropertyActionListener component, because a GET request does not fire an action event.
Since there is no action event i have to use a GET request, giving the ID of the object to BeanB as a parameter. BeanB then uses a dao to get the object with the specified ID. But this sux. I don't want to use the dao. So my specific question:
What is the equivalent to f:setPropertyActionListener for GET request links? How can I set a property of a BackingBean when I click on a h:link or h:outputLink component?
I hope there is a solution, thanks in advance.
Pass it as a request parameter. Request parameters can be only strings, so you need to convert it (or better, the smallest possible part which uniquely identifies the object in question) to string first.
Here's an example which uses the (database) identifier of the object for this:
<ui:repeat value="#{someOtherBean.listOfObjects}" var="objSelected">
<h:link outcome="ViewB">
<f:param name="id" value="#{objSelected.id}" />
</h:link>
</ui:repeat>
And in BeanB
add:
@ManagedProperty("#{param.id}")
private Long id;
private SomeObject objSelected;
@PostConstruct
public void init {
objSelected = objService.find(id);
}
// ...
精彩评论