当前位置:编程学习 > JSP >>

JavaServer Faces (JSF) vs Struts

答案:A brief comparison
October 19, 2004
Summary
My JSF article series and Meet the Experts appearance on IBM developerWorks received a lot of feedback. I would have to say, the most common question or feedback came along the lines of comparing Struts to JSF.


My JSF article series and Meet the Experts appearance on IBM developerWorks received a lot of feedback.

I would have to say, the most common question or feedback came along the lines of comparing Struts to JSF. I thought it would be a good idea to compare JSF to Struts by evaluating various features that an application architect would look for in a Web application framework. This article will compare specific features. Those on which I will focus include:

Maturity
Controller Flexibility/Event Handling
Navigation
Page development
Integration
Extensibility
Certainly, there are other places in which you might want to do a comparison, such as performance, but I'll focus on the set I just mentioned. I'll also spend more time on the Controller and Navigation sections because they are the heart of the frameworks. Performance of JSF is specific to the vendor implementation, and I always encourage people to perform their own performance tests against their own set of requirements because there are too many factors that can affect performance. A performance evaluation would be unfair. Other areas such as page layout, validation, and exception handling were also left out in the interest of saving space.
Maturity
Struts has been around for a few years and has the edge on maturity. I know of several successful production systems that were built using the Struts framework. One example is the WebSphere Application Server Web-based administrative console. JavaServer Faces(JSF), however, has been in draft for 2 years. Several companies, including IBM as well as the creator of Struts, Craig McClanahan, have contributed to the creation of JSF during that time. Nonetheless, it will take some time to see a few systems deployed.
Struts definitely has the edge in this category. With JSF, however, you can rely on different levels of support depending on which implementation you choose. For example, the JSF framework inside WebSphere Studio comes with IBM support.



Controller Flexibility/Event Handling
One of the major goals of Struts was to implement a framework that utilized Sun's Model 2 framework and reduced the common and often repetitive tasks in Servlet and JSP development. The heart of Struts is the Controller. Struts uses the Front Controller Pattern and Command Pattern. A single servlet takes a request, translates HTTP parameters into a Java ActionForm, and passes the ActionForm into a Struts Action class, which is a command. The URI denotes which Action class to go to. The Struts framework has one single event handler for the HTTP request. Once the request is met, the Action returns the result back to the front controller, which then uses it to choose where to navigate next. The interaction is demonstrated in Figure 1.

JSF uses the Page Controller Pattern. Although there is a single servlet every faces request goes through, the job of the servlet is to receive a faces page with components. It will then fire off events for each component and render the components using a render toolkit. The components can also be bound to data from the model. The faces life-cycle is illustrated in Figure 2.

JSF is the winner in this area, because it adds many benefits of a front controller, but at the same time gives you the flexibility of the Page Controller. JSF can have several event handlers on a page while Struts is geared to one event per request. In addition, with Struts, your ActionForms have to extend Struts classes, creating another layer of tedious coding or bad design by forcing your model to be ActionForms. JSF, on the other hand, gives developers the ability to hook into the model without breaking layering. In other words, the model is still unaware of JSF.

Navigation
Navigation is a key feature of both Struts and JSF. Both frameworks have a declarative navigation model and define navigation using rules inside their XML configuration file. There are 2 types of navigation: static navigation - when one page flows directly to the next; and dynamic navigation - when some action or logic determines which page to go to.

Both JSF and Struts currently support both types of navigation.

Struts
Struts uses the notion of forwards to define navigation. Based on some string, the Struts framework decides which JSP to forward to and render. You can define a forward by creating an Action as shown in the snippet below.

<action path="/myForward" forward="/target.jsp"> </action>

Struts supports dynamic forwarding by defining a forward specifically on an Action definition. Struts allows an Action to have multiple forwards.



<action-mappings>
<action name="myForm" path="/myACtion" scope="request"
type="strutsnav.actions.MyAction">
<forward name="success" path="./target.jsp">
</forward>
<forward name="error" path="./error.jsp">
</forward>

</action>
</action-mappings>

Developers can then programmatically choose which forward to return.



public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
MyForm myForm = (MyForm) form;

try {

// do something here

} catch (Exception e) {

// Report the error using the appropriate name and ID.
errors.add("name", new ActionError("id"));
forward = mapping.findForward("success");
return (forward);
}

forward = mapping.findForward("success");
return (forward);

}

JSF Static Navigation
JSF supports navigation by defining navigation rules in the faces configuration file. The example below shows a navigation rule defining how one page goes to the next.



<navigation-rule>
<from-view-id>/FromPage.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/ToPage.jsp</to-view-id>
</navigation-case>
</navigation-rule>

However, unlike Struts, JSF navigation is applied on the page level and can be action-independent. The action is hard coded into the component allowing for finer grain control on the page. You can have various components on the page define different actions sharing the same navigation rule.

<hx:commandExButton type="submit" value="Submit"
styleClass="commandExButton" id="button1" action="success" />

JSF also supports dynamic navigation by allowing components go to an action handler.

<hx:commandExButton type="submit" value="Submit"
styleClass="commandExButton" id="button1" action="#
{pc_FromPage.doButton1Action}" />

Developers can then code action handlers on any class to make the dynamic navigation decision.



public String doButton1Action() {
return "success";
}

Even though navigation rules don't need to specify the action in order to support dynamic navigation, JSF allows you to define the action on the navigation rule if you so choose. This allows you to force a specific navigation rule to go through an action.



<navigation-rule>
<from-view-id>/FromPage.jsp</from-view

上一个:初探Java类加载机制
下一个:Java Threading中的final变量

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,