Comparison between JSF and struts

Source: Internet
Author: User
Struts and JSF/tapestry both belong to the presentation layer framework. These two frameworks belong to different properties. The latter is an event-driven component model, while struts is only a simple MVC model framework, foreigners are always eager to say that the event-driven architecture is better than the MVC Framework. Why? Let's make a detailed analysis and compare what is going on?
First, events are triggered by user operations on the client page (browser). Struts uses action to accept browser form submission events. The command mode is used here, each subclass that inherits action must implement a method execute.

In struts, a form corresponds to an action class (or dispatchaction). In other words, in struts, a form corresponds to only one event, struts is called Application Event. Application event is a coarse-grained event compared with component event.

An important form object of struts, actionform, is an object that represents an application. This object contains at least several fields, which are input fields in the JSP page form, because a form corresponds to an event, when we need to refine the event granularity to the fields in the form, that is, when a field corresponds to an event, it is impossible to simply use struts, of course, it can be achieved by combining JavaScript.

In this case, JSF can be used for convenient implementation,

< H: inputtext ID = "Userid" Value = "# {Login. userid }" >
< F: valuechangelistener Type = "Logindemo. userloginchanged"   />
</ H: inputtext >  

# {Login. userid} indicates the result obtained from getuserid of Logan named login. This function can also be implemented using struts, name = "login" property = "userid"

The key is the second line, which indicates that if the value of userid changes and is determined to be submitted, the processvaluechanged () method of userloginchanged will be called.

JSF provides two types of events for components: value changed and action. The former has been used in the previous section, and the latter is equivalent to the form submission action mechanism in struts. Its JSF syntax is as follows:

< H: commandbutton ID = "Login" Commandname = "Login" >
< F: actionlistener Type = "Logindemo. loginactionlistener" />
</ H: commandbutton >  

SlaveCodeWe can see that these two events are pasted on specific component fields in the Observer mode like listerner, and Struts events are the original form submission submit trigger mechanism. If the former is more linguistic (the conventional programming language is similar to swing programming); the latter is web-based because it comes from HTML forms. If you start from Perl/PHP, instead, it is easy to accept the struts style.

Basic Configuration

Struts and JSF are both a framework. JSF must have two packages: JSF core package and jstl package (TAG library). In addition, JSF will also use some commons packages of the Apache project, you can deploy these Apache packages on your server.

JSF package: http://java.sun.com/j2ee/javaserverfaces/download.htmlselect the reference implementation.

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

Therefore, from the perspective of the composition of the JSF driver package, the Open Source Gene also occupies a large proportion. JSF is a mix of industrial standards of Sun's partners and open source.

The jar files downloaded from the preceding two addresses are merged into all the driver packages required by JSF. Like struts driver packages, these driver packages must be located in the WEB-INF/lib of the web Project, and like struts, they must also have the following configuration in Web. xml:

< 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 >  
< Servlet-Mapping >
< Servlet-name > Faces Servlet </ Servlet-name >
< URL-Pattern > *. Faces </ URL-Pattern >
</ Servlet-Mapping >
</ Web-app >
 

It is similar to struts's web. xml configuration.

Like struts's struts-config.xml, JSF also 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 >  
< Managed-bean >
< Managed-bean-name > User </ Managed-bean-name >
< Managed-bean-class > Com. corejsf. userbean </ Managed-bean-class >
< Managed-bean-Scope > Session </ Managed-bean-Scope >
</ Managed-bean >
</ Faces-config >

 
 

In the Struts-config.xml there are actionform action and JSP between the process relationship, in the faces-config.xml, there is also such a process, we have a specific explanation of Navigation:

There is an event in index. jsp:

< H: commandbutton Label = "Login" Action = "Login"   />

The value of action must match the form-outcome value. The navigation configuration above 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 an independent event occurrence and page navigation process arrangement, which is clearer than struts.

Managed-bean is similar to the struts actionform, just as you can define the scope of the actionform in the struts-config.xml, here also defines the scope of the managed-bean as the session.

However, if you only think that JSF's managed-bean is wrong about this function, JSF is integrated with new IOC mode/dependency injection and other technologies.

IOC Mode

For a managed-bean like 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 ;}
}


< Managed-bean >
< Managed-bean-name > User </ Managed-bean-name >
< Managed-bean-class > Com. corejsf. userbean </ Managed-bean-class >
< Managed-bean-Scope > Session </ Managed-bean-Scope >

< Managed-Property >
< Property-name > Name </ Property-name >
< Value > Me </ Value >
</ Managed-Property >

< Managed-Property >
< Property-name > Password </ Property-name >
< Value > Secret </ Value >
</ Managed-Property >
</ Managed-bean >
 

Faces-config.xml this configuration is actually to assign "me" to name, assign secret to password, which is to take the setter injection method in IOC mode.

Backing beans

For a web form, we can use a bean to contain all the components involved. This bean is called a backing bean. The advantages of backing bean are: A single class can encapsulate data and logic related to a series of functions.

To put it bluntly, a JavaBean contains other JavaBean and calls each other. It belongs to the facade mode or adapter mode.


For a backing beans, there are several managed-beans, and the managed-bean must have a scope. So how do some managed-beans configure their scope?

< Managed-bean >

< Managed-Property >
< Property-name > Visit </ Property-name >
< Value > # {Sessionscope. Visit} </ Value >
</ Managed-Property >
 

Here, a backing beans is configured with a setvisit method. Assign this visit value to visit in the session so that we can access the visit object in the program, obtain the expected data (for example, the user login registration information), and the visit is saved in the session, application, or request.

UI

Like JSF and struts, in addition to the JavaBeans class, there are also page representation elements, are completed using tags, Struts also provides the struts-faces.tld tag Library to the JSF transition.

When you use the struts tag library to program complex pages, one of the biggest problems is that logic tags are used in large quantities. This logic is like the if statement. Once written, the JSP page is like the Russian square, however, using the JSF tag is concise and elegant:

< Jia: navigatoritem Name = "Inbox" Label = "Inbox"
Icon = "/Images/inbox.gif"
Action = "Inbox"
Disabled = "#{! Authenticationbean. inboxauthorized }" />
If the inboxauthorized returned by authenticationbean is false, this line of labels is not displayed, how clean and easy!
write it here first. I will continue to make in-depth comparisons on JSF. If you have studied the jdon framework, you may find that the jdon framework of the jdon framework. in XML, service configuration and managed-bean both use dependency injection. It seems that the dependency injection for JavaBean has quickly become a new symbol. If you do not know the IOC mode, make up the class.
Add JSF core tutorial a JSF case: login.rar

http://blog.sina.com.cn/s/blog_4e88c02701000cl0.html

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.