Similarities and differences between JSF and struts in J2EE MVC Mode

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, by combiningJavascriptTurns can also be achieved.

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

<H: inputtext id = "userid" value = "# {login. userid}">
<F:ValuechangelistenerType = "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:ActionlistenerType = "logindemo. loginactionlistener"/>
</H: commandbutton>

From the code, we can see that these two events are pasted on specific component fields in the Observer mode like listerner, and Struts is a 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, if these Apache packages are deployed on yourServer.

JSF package:Http://java.sun.com/j2ee/javaserverfaces/download.htmlSelectReference implementation.

Download the jstl package inHttp://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 will not be displayed. How clean is it!

Write it here first, and 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.

 

This article from: http://dev.yesky.com/322/2162322_1.shtml

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.