JSF development practices (1)

Source: Internet
Author: User
Tags tld
JSF development practices (1)

Bromon originality, please respect copyright

JSF will be the web development framework included in j2ee5.0. This should be the first web framework to become the JCP standard and released along with j2eesdk. We can see that sun has high expectations for it. The biggest competitor of JSF is tapestry, which is an Apache product. However, Apache has developed a MyFaces, which is an implementation of the JSF standard. Maybe you have been hesitating for a long time between JSF and Tapestry like me. In the future, we should be able to see the trend of the two from Apache's attitude. In TSS there is a comparison of JSF 1.0 and Tapestry 3.0 Article, the content is very solid in place: http://www.theserverside.com/articles/article.tss? L = jsftapestry
The competitor of JSF is not the stream of struts/webwork. They are basically at different stages. It is of little significance to put them together.

The development process of JSF is very similar to the code behind method proposed by Asp.net. The core is event-driven, and components and labels are encapsulated in a high degree. Many typical applications do not need developers to process HTTP. Page operations are automatically mapped to the corresponding Java Bean, and the background logic only needs to interact with the Java Bean. The entire process is implemented through "dependency injection (DI)". It seems that this is the best way to solve the coupling problem at present. Spring has a profound influence. However, due to the fact that JSF adopts this method, the development work is very different from the previous JSP/struts, and it takes some time to learn. Before learning, we recommend that you have a clear understanding of dependency injection. For more information, see the first article in my learn spring in Spring Series.

This series uses two examples to illustrate the basic development of JSF. The first example is Hello world. Currently, only a few JSF ides are available. IBM will release the WTP version supporting JSF in. Therefore, our example is based on handwriting, which can give us a clearer understanding and recommend the best JSF development tool: myeclipse 4.0 ga. In the following example, the content of JSF and hibernate will be available, which can be well supported. Because myeclipse is not free of charge, in addition to explaining how to operate in IDE, we will also describe the specific content of manual operations to avoid relying too much on development tools. Any server can be used. JBoss 4.0.2 is used here. If your server is a later version of Tomcat (5.5 +), you must delete some of its built-in packages to support JSF. For details, see its documentation.

Download the jsf ri and jstl 1.1 on your own.

Start.

Create a new web project named "hello" in myeclipse 4.0ga and add support for jstl for the project:


 
In jstl, select 1.1.
This operation adds jstl. jar and standard. jar to the project.

Add JSF support for the project using similar operations: myeclipse-add JSF capabilities

 

The JSF implementation option is to select which JSF implementation to use. We use sun's JSF Ri.
The JSF config path is the location of the configuration file and remains unchanged.
URL pattern is the JSF ing method of JSF servlet. There are two options. The details are described later.
The above operation adds the jar and TLD files required by JSF for the project and creates a configuration file for the faces-config.xml. Involved jar has: commons-beanutils.jar commons-collections.jar commons-digester.jar commons-logging.jar jsf-api.jar jsf-impl.jar
All TLD files in JSF are involved.

The current faces-config.xml file content is:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype faces-config public "-// Sun Microsystems, Inc. // DTD JavaServer faces config 1.1 // en" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<Faces-config>

</Faces-config>

The environment has been set up. Now we need to create a program. Its function is to allow users to enter a name in the form. After submission, the system will return a greeting. The advantage of using JSF is that developers naturally separate the MVC layers and won't be as awkward as using strtus.

First, develop the model layer, which is a very simple Bean:
Package org. bromon. JSF. model. Hello;

Public class sayhello {
Public String say (string name)
{
Return "hello," + name;
}
}

In the model layer, You can implement business data logic without having to have any relationship with the web layer.

The following development control layer is used to access data on the web layer and call the logic of the model layer:
/**
* JSF Control Layer Method
* @ Author bromon
*/
Package org. bromon. JSF. Control. Hello;

Import org. bromon. JSF. model. Hello .*;

Public class hellodelegater {

// ------ Property ---------
Private string name; // text box data in the form will be uploaded here
Private string result; // The running result is obtained from the web page.
Private sayhello; // object at the model layer, which is not case-based and injected by the System

// ----- Set/get --------
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public sayhello getsayhello (){
Return sayhello;
}
Public void setsayhello (sayhello ){
This. sayhello = sayhello;
}
Public String getresult (){
Return result;
}
Public void setresult (string result ){
This. Result = result;
}

// ----- Logical method ---------
Public String say ()
{
This. setresult (sayhello. Say (this. getname ()));
Return "OK ";
}
}

Note that the attribute names and the Set/get method names must be written in strict accordance with Java Bean specifications because they are used for injection dependencies. The sayhello object is not instantiated and will be injected by the system at runtime.

Of course, these two beans must be declared in the system; otherwise, di cannot be implemented. Add content to the faces-config.xml file:
<Managed-bean>
<Managed-bean-Name> sayhello </managed-bean-Name>
<Managed-bean-class>
Org. bromon. JSF. model. Hello. sayhello
</Managed-bean-class>
<Managed-bean-scope> request </managed-bean-scope>
</Managed-bean>
<Managed-bean>
<Managed-bean-Name> hellodelegater </managed-bean-Name>
<Managed-bean-class>
Org. bromon. JSF. Control. Hello. hellodelegater
</Managed-bean-class>
<Managed-bean-scope> request </managed-bean-scope>
<Managed-property>
<Property-Name> sayhello </property-Name>
<Value >#{ sayhello} </value>
</Managed-property>
</Managed-bean>

In the next bean, its sayhello attribute is specified to inject an org. bromon. JSF. model. Hello. sayhello instance at runtime.

The presentation layer page has only one index. jsp:
Two tag libraries need to be introduced:

<% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "H" %>
<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "F" %>
The following describes how to construct a JSF Tag:

<Body>
<F: View>
<H: Form>
<H: panelgrid columns = "3">
<H: outputlabel for = "name" value = "name:"/>
<H: inputtext id = "name" value = "# {hellodelegater. name}" required = "true"/>
<H: Message for = "name"/>

<H: outputlabel value = "# {hellodelegater. Result}"/>
</H: panelgrid>
<H: panelgroup>
<H: commandbutton action = "# {hellodelegater. Say}" value = "Submit"/>
</H: panelgroup>
</H: Form>
</F: View>
</Body>

The page contains a text box and a label, which are respectively bound to two attributes of the hellodelegater class, the specific binding work is implemented by calling the corresponding set/get method through flip control. The submit button is bound to the hellodelegater. Say method, which assigns the calculation result to the result attribute and is displayed on the page.
The program execution result is as follows:


 

In the next article, we will introduce a more complex user management program. We can see more clearly the features of JSF and the use of some components of JSF Model objects.

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.