Similarities and differences between JSF and Struts

xiaoxiao2021-04-09  348

Both Struts and JSF / Tapestry are a framework framework. These two frameworks that are subordinate, the latter is an event-driven component model, while Struts is only a simple MVC mode framework, and foreigners always say an extra. The drive type is better than the MVC mode frame, why see, let's take a detailed analysis, what is going on?

First event refers to the event from the client page (browser), which is triggered by the user, Struts uses action to accept the event submitted by the browser form, where the Command mode is used, and each inherited Action must implement a method Execute .

In Struts, it is actually a form form corresponding to an Action class (or dispatchction), in order to say: In Struts, it is actually a form that can only correspond to an event, and the Struts is called Application Event, Application Event and Component Event. Compared to a coarse granular event.

Struts Important Form Object ActionForm is an object that represents an application. At least a few fields in this object, these fields are the input field in the JSP page form, because a form corresponds to an event, so when we need When the event granularity is refined into these fields in the form, that is, when a field corresponds to an event, it is unlikely to use Struts, and of course, it is also possible to turn it through javascript.

And this situation can be easily implemented using JSF.

# {login.userid} indicates the result obtained from the GetUserID named login's JavaBean, which can be implemented using Struts, name = "login" property = "userid"

The key is the second line, which means that if the value of the user ID changes and determines submissions, the processValueChanged (...) method of the class userLoginChanged will be triggered.

JSF can provide two events for components: Value Changed and Action. We have already visited the same place in the previous section, and the latter is equivalent to Struts' form submitting an action mechanism, its JSF Writing is as follows:

As can be seen from the code, these two events are attached to the specific component field through the Listerner, while the Struts such event is the original form submits the Submit trigger mechanism. If the former is relatively language (programming language habits Similar to swing programming); the latter is Web because it is from the HTML form, if you start from Perl / PHP, it is easy to accept the style of Struts.

basic configuration

Struts and JSF are a framework. JSF must require two packets JSF core packages, JSTL packages (tag libraries), and JSF will also use some of the Apache project, these Apache packages as long as deployed in your server Either. JSF package download address: http://java.sun.com/j2ee/javaserverfaces/download.html Select where REFERENCE IMPLEMENTATION.

The JSTL package is downloaded at http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi, so from the JSF driver package, its open source gene also occupies a big proportion, JSF is a Sun partner industry A mixed product between standards and open sources.

The JAR merged by the two addresses downloaded is the full driver package required by JSF. Like the Struts driver package, these drivers must be located on the web-inf / lib of the web project, and the Struts must also be configured in Web.xml:

faces servlet javax.faces.Webapp.Facesservlet 1 Faces servlet *. Faces

Here and Struts's web.xml configuration is similar, simply exactly the same.

As Struts Struts-Config.xml, JSF also has similar Faces-Config.xml configuration files:

/index.jsp Login /welcome.jsp user com.corejsf.userbean session

There is a process relationship between Actionform Action and JSP in Struts-Config.xml. In Faces-Config.xml, there is such a process, and we explain the navigation:

There is an event in Index.jsp:

The value of the Action must match the Form-Outcome value, the above Navigation configuration representation: If there is a login event in Index.jsp, then the next page will be Welcome.jsp after the event is triggered.

JSF has an independent event and the process arrangement of page navigation. This idea is very clear than Struts.

Managed-bean is similar to Struts, as you can define the ActionForm's Scope in Struts-Config.xml, which also defines the management-bean scope as session.

But if you only think that JSF's Managed-Bean is wrong, JSF integrates new IOC mode / dependent injection.

IOC mode

For UserBean such a managed-bean, its code is as follows:

Public class userbean {private string name; private string password ;. 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 < / managed-bean-scope> name me Password < / Property-Name> Secret

Faces-config.xml This configuration is actually assigned "ME" to NAME, assigns the SecRET to Password, which is the setter injection in IOC mode.

BACKING Beans

For a web form, we can use a bean that contains all the components involved. This bean is called the backing bean, and the BACKING BEAN has the advantage of: a single class can encapsulate data and logic of the related functions.

To put it bluntly, it is a JavaBean containing other JavaBeans, mutual calls, which belong to Facade mode or Adapter mode.

For a Backing Beans, it contains several managed-beans. Managed-bean must have scope, then how many managed-beans are configured to configure their Scope? ... Visit # {sessionscope.visit}

It is configured with a setvisit method in a backing beans, assigning this Visit to the Visit in the session, so that we only access the Visit object in the program, get the data we want (such as the user login registration information), and Visit is Save in Session or Application or Request only needs to be configured.

UI interface

JSF and Struts, in addition to the JavaBeans class, there is also a page performance element, which is done using the label, and Struts also provides the Struts-Faces.TLD tag library transition to the JSF.

When using the Struts tag library programming complex pages, a biggest problem is to use the Logic tab in a large number, this Logic is like the IF statement, once written, the JSP page is like a Russian square, but the use of JSF tags is simple and beautiful:

If INBOXAUTHORIZED is fake in AuthenticationBean, then this line of tags do not have to be displayed, clean and benefit!

First, I will continue to be in-depth in JSF, if you have studied the JDON framework, you may find that the service configuration in JDonframework.xml in the JDON framework uses the dependence in JavaBean. Dependency injections have quickly become a new technology symbol, if you still don't understand the IOC mode, quickly make up less

转载请注明原文地址:https://www.9cbs.com/read-133012.html

New Post(0)