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>
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, 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 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.