0

I am used to work in Struts Framework. The things there are pretty straight forward. The Action classes use the information received from Action Form (which represented a main entity in my project) classes and then they continue their work calling different methods from some Service Classes of some sort.

Now I'm trying to learn JSF but I can't understand the exact logic which sits behind this framework. I red some tutorials and followed some examples but they all focus on the request processing life cycle and they use managed beans and backing beans (one for each page) which handle validations, database updates and so on. No action handling of any sort.

I red hear in another post that struts is an Action Framework while JSF is a Component Framework but it seems to me that JSF is a little bit more messy than Struts.

Am I missing something in this whole JSF structure?

balazs630
  • 3,421
  • 29
  • 46
TGM
  • 1,659
  • 10
  • 30
  • 45
  • Other than validations, database updates, and so on, what *don't* you know how to handle? What's an "action" if not a combination of those things? – Dave Newton Dec 05 '11 at 23:56
  • @DaveNewton there isn't a problem of handling things, I want to be sure that this kind of approach is the correct one. Things are totally different than what I'm used to. – TGM Dec 06 '11 at 00:00

2 Answers2

4

they use managed beans and backing beans (one for each page) which handle validations, database updates and so on. No action handling of any sort.

It are those methods which are bound as action attribute of <h:commandLink> and <h:commandButton> which are the real action methods.

E.g.

<h:form>
    <h:inputText id="foo" value="#{bean.foo}" required="true" />
    <h:message for="foo" />
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

with

@ManagedBean
@RequestScoped
public class Bean {

    private String foo;

    public void submit() {
        // Here, you're inside the action method!
        // Save foo in DB or something: someService.save(foo);
        // Navigate to a different view if necessary.
    }

    // Getter+setter.
}

Note that validation is supposed to be done by Validator classes, not in action methods.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

You might think that JSF is messier than Struts since you just don't yet fully understand the inner workings of it.

With Struts, a request based framework, you have to deal with a lot of the lower level details yourself. You'll frequently find yourself tracking request parameters and interacting with the HttpServletRequest and HttpServletResponse objects.

With JSF, a component based framework, a lot of those lower level details have been removed. JSF takes care of these details for you by loading your beans, performing validations, etc. Only rarely would you need to interact with the request/response objects.

By abstracting a lot of the Servlet details away, JSF allows you to focus more on building your model and actions rather than the details of the requests. In this manner, I think JSF is a lot cleaner than Struts - not messier. The downside to this is that there is a decent learning curve to JSF. Developers using JSF exclusively might also remain oblivious of how the requests are actually handled, which is useful to know.

Andrew
  • 362
  • 3
  • 8
  • Thanks for your answer! And the correct way to manage those actions is through backing beans right? – TGM Dec 06 '11 at 00:24
  • Yes, most often where this action happens would be called a backing bean. However, you can also break your managed beans down further so you have separate backing beans and controller beans. There is a great article on different types of managed beans here: http://blog.icefaces.org/blojsom/blog/default/2009/04/23/Making-distinctions-between-different-kinds-of-JSF-managed-beans/ – Andrew Dec 06 '11 at 01:31