JSF tutorial
1. Getting Started
1) download the Download Area of the official JSF website
Http://java.sun.com/j2ee/javaserverfaces/download.html
Lib required by JSF
* Jsf-impl.jar
* Jsf-api.jar
* Commons-digester.jar
* Commons-collections.jar
* Commons-beanutils.jar
* Jstl. Jar
* Standard. Jar
Also required
Jstl. jar and standard. Jar files
1. JSF official website java.sun.com/j2ee/ererverfaces
Implementation of sourceforge.net/projects/myfaces open source edition.
Reference http://java.sun.com/webservices/downloads/webservicespack.html
Sample Application and code list www.brainysoftware.com (to copy the package)
2. The jsf-api.jar contains the related classes in the faces Servlet and javax. Faces packages.
Reference implementation of jfs-ri.jar JSF.
Jstl. jar and standard. Jar use the JSP standard to mark the files in the library.
A commons-beanutils.jar is a tool class that defines and accesses attributes of a JavaBeans component.
Commons-digester.jar this library file contains the Apache common digester class.
Commons-logging.jar is a universal, flexible logging tool.
2) ing web. xml
<Servlet-mapping>
<Servlet-Name> faces servlet </servlet-Name>
<URL-pattern> *. Faces </url-pattern> and/faces
</Servlet-mapping>
If the request is/index. faces, the/index. jsp page will actually be invoked.
3) faces-config.xml
<Faces-config>
<Navigation-Rule>
<From-View-ID>/pages/index. jsp </from-View-ID>
<Navigation-case>
<From-outcome> login </from-outcome>
<To-View-ID>/pages/welcome. jsp </to-View-ID>
</Navigation-case>
</Navigation-Rule>
You can also add <from-Action> in <navigation-case> to further standardize the action method that the form result must follow.
(Action Method)
<Managed-bean>
<Managed-bean-Name> User </managed-bean-Name>
<Managed-bean-class>
Onlyfun. Caterpillar. userbean
</Managed-bean-class>
<Managed-bean-scope> session </managed-bean-scope>
</Managed-bean>
</Faces-config>
In <navigation-Rule>, we define the page flow. When the request comes from the page specified in <from-View-ID>
When <from-outcome> in <navigation-case> is login, the request is directed to the page specified by <to-View-ID>.
<From-View-ID> if this parameter is not set, the source webpage is not restricted. You can also explicitly use * to indicate in the definition file, for example:
<From-View-ID>/* or * </from-View-ID>
4) error message attributes
<H: outputtext value = "# {user. errmessage}"/>
Errmessage = "incorrect name or password ";
JSF Expression Language #{}
5) Param
Param is an implicit object variable preset by JSF El. It represents a set of all request parameters. It is actually
Java. util. Map type objects. The implicit objects provided by JSF correspond to JSP implicit objects.
The JSF implicit object removes pagination and pagecontext, and adds facescontext and view,
They correspond to javax. Faces. Context. facescontext and
Javax. Faces. component. uiviewroot.
For map objects, we can use the '.' operation to specify the key value to retrieve the corresponding value.
Use [and] to specify, for example:
<F: View>
<B> hello, </F: View>
6) map, list
<F: View>
<H: outputtext value = "# {somebean. somemap [user. Name]}"/>
</F: View>
If the variable is a list type or array, you can specify the index in [], for example
<F: View>
<H: outputtext value = "# {somebean. somelist [0]}"/>
</F: View>
7) International messages
Messages. properties default
<F: loadbundle basename = "messages" Var = "msgs"/>
<F: View> you can set the locale attribute to directly specify the language to be used. For example:
If set to the following
Messages_en.properties:
<F: View locale = "en">
<F: loadbundle basename = "messages" Var = "msgs"/>
You can also set the language family in faces-config.xml, for example:
<Faces-config>
<Application>
<Local-config>
<Default-locale> en </default-locale> default language
Languages supported by <supported-locale> zh_tw </supported-locale>
</Local-config>
</Application>
.....
</Faces-config>
There must be a <default-locale> in <local-config>, and <supported-locale>
Which tells JSF which languages your application supports.
Of course, it would be better if you could provide an option for users to select their own language. For example
The Locale attribute of the user bean determines the page language family:
<F: View locale = "# {user. locale}">
<F: loadbundle basename = "messages" Var = "msgs"/>
Set a form on the page to allow users to select a language family, for example, set a single button:
<H: selectoneradio value = "# {user. locale}">
<F: selectitem itemvalue = "zh_tw"
Itemlabel = "# {msgs. zh_twtext}"/>
<F: selectitem itemvalue = "en"
Itemlabel = "# {msgs. entext}"/>
</H: selectoneradio>
* <F: selectitem itemvalue = "" itemlabel = ""/>