开发者

Can JSF2 use beans as request-based controllers without a framework?

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 开发者_StackOverflow社区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


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:

  • JSF2 bookmarkability and view parameters
  • JSF2 page parameters and page actions
  • What's new in JSF2? - GET support
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜