Similarities and differences between JSF and struts of Java EE MVC pattern

Source: Internet
Author: User
J2ee|js

Struts and Jsf/tapestry both belong to the presentation layer framework, which is a different kind of framework, the latter is an event-driven component model, and struts is just a simple MVC pattern framework, the foreigner is always yelling that the event-driven model is better than the MVC framework, why, Let's do a detailed analysis to compare what's going on here?

The first event is an event that is triggered from a client page (browser) by a user action, and struts uses the action to accept events from the browser form submission, where command mode is used, and the subclass of each inherited action must implement a method execute.

In struts, it's actually a form form that corresponds to an action class (or Dispatchaction), in other words: a form in struts is actually a single event, and struts is called Application event, Application event and component events are a coarse-grained occurrence.

Struts important Form Object Actionform is an object that represents an application that contains at least several fields that are input fields in the JSP page form because one form corresponds to an event, so When we need to detail the event granularity to these fields in the form, that is, when a field corresponds to an event, it is not possible to simply use struts, and of course it can be done by combining JavaScript.

This is a situation that can be easily implemented using JSF,

valuechangelistener type= "Logindemo. Userloginchanged "/>

#{login.userid} represents the results obtained from the GetUserID of JavaBean named Login, which can also be implemented using struts, name= "login" property= "UserId"

The key is the second line, which means that if the value of the UserID changes and the commit is determined, the invocation of the processvaluechanged (...) of the class userloginchanged is triggered. Method.

JSF can provide two kinds of events for a component: Value changed and Action. The former, which we have seen in the last section, is equivalent to the form submission action mechanism in struts, and its JSF is written as follows:

actionlistener type= "Logindemo. Loginactionlistener "/>

As you can see from the code, these two events are posted on a specific component field by Listerner, and struts such an event is a primitive form submission submit trigger mechanism. If the former is more linguistically (programming language idioms are like swing programming), the latter is web based because it comes from an HTML form, and if you start with perl/php, it's easy to accept the style of struts.

Basic Configuration

Both struts and JSF are a framework, JSF must require two packages of JSF core packages, JSTL packages (tag libraries), and JSF will also use some commons packages to the Apache project, which can be deployed only on your server.

JSF package Download Address: http://java.sun.com/j2ee/javaserverfaces/download.html Select which Reference implementation.

Jstl package Download in http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi

So, in terms of the JSF driver package, the Open-source gene is also a big part of the mix, and JSF is a half-breed between Sun partners ' industrial standards and open source.

These two addresses downloaded jars together are all the driver packages that JSF needs. Like struts's driver packages, these drivers must be in the web-inf/lib of the Web project, and as with struts, they must be configured in Web.xml as follows:

<web-app>
<servlet>
<servlet-name> Faces servlet </servlet-name>
<servlet-class> Javax.faces.webapp.FacesServlet </servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>


Faces servlet
*.faces

This is exactly the same as the web.xml configuration of struts.

Like the struts-config.xml of struts, JSF has a similar faces-config.xml configuration file:


<faces-config>
<navigation-rule>
<from-view-id>/index.jsp </from-view-id>
<navigation-case>
<from-outcome> Login </from-outcome>
<to-view-id>/welcome.jsp </to-view-id>
</navigation-case>
</navigation-rule>


User
Com.corejsf.UserBean
Session

In the struts-config.xml there are actionform action and the process relationship between the JSP, in Faces-config.xml, there is also such a process, we specifically explain navigation:

There is an event in the index.jsp:

The value of the action must match the Form-outcome value, and the above navigation configuration indicates that if there is a login event in index.jsp, the next page after the event is triggered will be welcome.jsp

JSF has a separate event occurrence and page navigation flow arrangement, which is much clearer than struts.

Managed-bean is similar to Struts actionform, as can be defined in Struts-config.xml as the scope of Actionform, here also defines the Managed-bean scope for session.

But if you only think JSF's Managed-bean is wrong about this, JSF incorporates the new IOC model/Dependency injection technology.

IOC model

For a managed-bean such as Userbean, the code is as follows:

public class UserBean {
private String name;
private String password;

Property:name
Public String GetName () {return name;}
public void SetName (String newvalue) {name = NewValue;}

Property:password
Public String GetPassword () {return password;}
public void SetPassword (String newvalue) {password = newvalue;}
}


User
Com.corejsf.UserBean
Session


name
Me



Password
Secret

Faces-config.xml This configuration is actually assigning "me" to name, assigning secret to password, which is the setter injection method in the IOC model.

backing Beans

For a Web form, we can use a bean that contains all of the components it involves, which is called the backing Bean, and the advantage of the backing Bean is that a single class can encapsulate the data and logic of a series of related functions.

To be blunt, a JavaBean contains other JavaBean that are called to each other and belong to the façade mode or adapter mode.


For a backing beans, which contains several managed-bean,managed-bean that must have scope, how many of these Managed-beans configure their scope?

<managed-bean>
...
<managed-property>
<property-name> Visit </property-name>
<value> #{sessionscope.visit} </value>
</managed-property>

This is configured with a Setvisit method in the backing beans that assigns this visit to the visit in session, so that later in the program we'll just access the visit object and get the data we want (such as user login registration information), And visit is saved in session or application or request only need to configure both.

UI interface

JSF and struts, in addition to the JavaBeans class, there are page performance elements, are done using tags, struts also provides a STRUTS-FACES.TLD tag library to the JSF transition.

One of the biggest problems with using the Struts tag library for programming complex pages is that the logic tag is used in large numbers, and this logic is like an if statement, once written, the JSP page is like a Tetris, but using JSF tags is simple and graceful:

icon= "/images/inbox.gif"
action= "Inbox"
Disabled= "#{!authenticationbean.inboxauthorized}"/>

If the Authenticationbean inboxauthorized return is false, then this line of labels do not have to show, how clean!

First of all, I'm going to go deeper into JSF, and if people who have studied the Jdon framework might find that The Jdonframework.xml service configuration in the Jdon framework, like Managed-bean, uses a dependency injection, and it seems that the reliance injection on JavaBean has quickly become a symbol of new technology, and if you don't know the IOC model, hurry up and make up a lesson.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.