JSF: Controller that relies on another controller?
I'm working on a project where we have a FooViewController
, and a BarListController
. The list of Bars
now needs to depend on the Foo
being viewed. So does anyone have a recommendation of how to do this?
I don't need an answ开发者_如何学Pythoner from an implementation perspective, necessarily, but more from a design perspective. That is:
- Should the
FooViewController
somehow tell theBarListController
whatFoo
is being viewed? - Should the
BarListController
ask theFooViewController
whatFoo
is being viewed? - In either case, how do you inject these things into one another? (This part I need implementation help ;-) )
Thanks for any help!
Basically, the bean where you're invoking the concrete action should ask for it as method argument or as managed property.
So, if you're using a Servlet 3.0 / EL 2.2 capable container, then pass Foo
as method argument:
<h:commandLink value="Bar list"
action="#{barListController.list(fooViewController.foo)}" />
with
public void list(Foo foo) {
this.list = barService.list(foo);
}
If you're not on EL 2.2 yet, then set Foo
as managed property:
<h:commandLink value="Bar list"
action="#{barListController.list}" />
with
@ManagedBean
@ViewScoped
public class BarListController {
@ManagedProperty("#{fooViewController.foo}")
private Foo foo;
public void list() {
this.list = barService.list(foo);
}
// ...
}
精彩评论