views:

23

answers:

1

Hi!

I'm working on the development for a web application using Java Server Faces for a group project. The majority of us have experience using request-based frameworks in PHP and we are having problems getting into the event-based (?) mindset of JSF-development.

In PHP you would generally have a front controller (like the Faces Servlet) that dispatches requests to other controllers (unlike the Faces Servlet). The point is that you have full control of the requests from you controllers. This control is something that seems to be lacking in JSF, since the beans are basically just business logic that is not necessarily run when accessing URLs (i.e. by entering the URL for a .xhtml file it will be loaded without the involvement of any beans).

My question is if there is a way to make all requests be handled by managed beans without using a framework such as Spring or Struts (which from I understand are request-based)? This would make the beans more like controllers and, in turn, enable us to manage such things as authentication/authorization, redirects, etc. in a more familiar way.

I hope this makes sense.. Anyhow, me and my group would be very grateful if someone could help us out!

Best regards, Erik

A: 

You can use @ManagedProperty to set request parameters as a managed bean property.

@ManagedProperty(value="#{param.foo}")
private Long foo;

With http://example.com/page.jsf?foo=123 this will set 123 as foo.

You can use @PostConstruct to indicate a method which has to be executed after the bean is been construct and those managed properties have been set.

@PostConstruct
public void init() {
    someResult = doSomethingWith(foo);
}

If you rather want to control this from inside the view side on (the XHTML page), then you can use <f:metadata>.

<f:metadata>
    <f:viewParam name="foo" value="#{bean.foo}" />
</f:metadata>

This will basically do bean.setFoo(request.getParameter("foo")) during view's construction. In that case, you don't need those annotations, but you won't be able to use the @PostConstruct functionality.

See also:

BalusC
Thanks for your reply! However the question remains how to actually match the URL to a bean, i.e. how to make any request to page.jsf actually construct the PageBean (or whatever it might be called).
erik.brannstrom
Just bind the bean somewhere in the view the usual way by `#{bean.foo}`. JSF will create and prepare the bean accordingly.
BalusC
That will actually be very useful, however this still means that the view decides which bean to be called. The question is rather if one can have the bean be called first and let that in turn decide on the view, instead of the other way around? Though I just saw your comment on the question, and I guess you're correct. The problem is that the group assignment requires us to do it in JSF, so that's why I'm curious if you can somehow modify it's behavior. Sorry for the confusion.
erik.brannstrom
Make use of dynamic page includes. E.g. `<ui:include src="#{bean.viewId}.xhtml" />`.
BalusC