First, let's take a look at what application developers are going to do. Let's write a simple JavaBean:
Userbean. Java
Package onlyfun. Caterpillar;
Public class userbean {
Private string name;
Public void setname (string name ){
This. Name = Name;
} Public String getname (){
Return name;
}
}
This bean stores the user name, compiled and placed under/WEB-INF/classes.
Next, design the page process. We will first display a login page/pages/index. JSP. Enter the user name and send out the form, and then enter the name in/pages/welcome. the user name and welcome message in bean are displayed in JSP.
To let JSF know the bean we designed and the page flow, we define a/WEB-INF/faces-config.xml:
Faces-config.xml
<?xml version="1.0"?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd"> <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> <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 a page flow. When a request comes from the page specified in <from-View-ID>, when <from-outcome> in <navigation-case> is specified as login, the request is directed to the page specified by <to-View-ID>.
In <managed-bean>, we can manage our beans in a unified manner. We set the survival range of bean objects to session, that is, the user can survive during the interaction between the browser and the program.
The following information is the bean name they can use, that is, the name set in <managed-bean-Name> and the page flow defined above.
First, the webpage designer writes the index. jsp webpage:
Index. jsp
We use the JSF core and HTML standard library. The core is about the processing of UI components, while the HTML is about the advanced standard of HTML.
<F: View> is similar to <HTML>. When you want to start using JSF components, these components must be <F: View> and </F: view>, just like when HTML is used, all labels must be between <HTML> and
Almost all of the HTML Tag libraries are advanced tags related to HTML tags.
Webpage designers don't have to worry about what to do after form transfer. They just need to design the welcome page:
Welcome. jsp
<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "F" %>
<% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "H" %>
<% @ Page contenttype = "text/html; charset = big5" %>
<HTML>
<Head>
<Title> first JSF Program </title>
</Head>
<Body>
<F: View>
<H: outputtext value = "# {user. name}"/> hello!
<H3> welcome to assumerver faces! </H3>
</F: View>
</Body>
</Html>
There is nothing to explain about this page. As you can see, there is no program logic on the webpage. What the webpage designer does is to follow the page process and use the relevant name to retrieve information, instead of worrying about how the program actually works.
Start container and connect to your application URL, for example, http: // localhost: 8080/jsfdemo/pages/index. faces: enter the name and send out the form. The welcome page is displayed.
<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "F" %>
<% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "H" %>
<% @ Page contenttype = "text/html; charset = big5" %>
<HTML>
<Head>
<Title> first JSF Program </title>
</Head>
<Body>
<F: View>
<H: Form>
<H3> enter your name Name: <H: commandbutton value = "send" Action = "login"/>
</H: Form>
</F: View>
</Body>
</Html>