TGM
TGM

Reputation: 1679

JSF application structure

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?

Upvotes: 0

Views: 302

Answers (2)

BalusC
BalusC

Reputation: 1108567

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:

Upvotes: 4

Andrew
Andrew

Reputation: 362

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.

Upvotes: 2

Related Questions