JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Web Framework » JSF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Your First JavaServer Faces. [精華]
caterpillar

良葛格

版主

發文: 2613
積分: 70
於 2005-02-07 18:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
沒事。。。太久沒有寫英文了。。。最近在看JSF。。。找個主題來練練英文。。。。Big SmileBig SmileBig Smile

In this article, I’d like to introduce an easy application written by using JSF pages. This application would show a simple form with one text field and submit button. Users enter their name in the text field and submit the form and then they would see a welcome page with their name on it.

First, you should download the “JavaServer Faces Reference Implementation” from http://java.sun.com/j2ee/javaserverfaces/download.html. It’s a zipped file. After you download it and decompress it, look into the directory “lib”. Copy all the jar files into your web application’s “WEB-INF/lib” directory. You also need jstl.jar and standard.jar files. You could easily get them from the war file in the “samples” directory. Unzip one war file and look for jstl.jar and standard.jar in the “WEB-INF/lib” directory. Copy them into your web application’s “WEB-INF/lib” directory too. In Summary, You need the jar files.
1
2
3
4
5
6
7
jsf-impl.jar
jsf-api.jar
commons-digester.jar
commons-collections.jar
commons-beanutils.jar
jstl.jar
standard.jar


Now you need to define your web application in the “WEB-INF/web.xml” file. All JSF pages requested should be processed by a special servlet “FacesServlet”. You should define it in the “web.xml”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="ISO-8859-1"?>
 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
    version="2.4"> 
 
    <description> 
        JSF Demo
    </description> 
    <display-name>JSF Demo</display-name> 
    <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>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list> 
</web-app>


In the “web.xml”, we define all requested URL with suffix “.faces” should be processed by FacesServlet. In this first JSF example, I’d like to use JSP for the view technology. The FacesServlet would strip off the suffix “.faces” and load the pages with suffix “.jsp”. For example, if you request “/index.faces”, the “/index.jsp” would be processed in the end.

Now lets’ come out with the first JSF page, named with “pages/index.jsp”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<title>JSF Demo</title>
</head>
<body>
    <f:view>
        <h:form>
            <h3>Please enter your name.</h3>
             name: <h:inputText value="#{user.name}"/><p>
            <h:commandButton value="Login" action="login"/>
        </h:form>
    </f:view>
</body>
</html>


If this page, I use two JSF’s standard tag libraries, “core” and html“. The <f:view> tag creates the top-level view. Currently, just know its function is a little like the HTML’s <html> tag. All JSF’s tags are begun with it.
<h:form> creates a HTML form and <h:inputText> provide a text field. Take a look at the “#{user.name}”. It tells the JSF implementation to link the text field with the name property of a user object. We’ll see how to link them soon.

<h:commandButton> provide an submit button. What you should note is the action attribute. It’s used to specify the navigation rule. The navigation rules are defined in the faces-config.xml file. It should be located in “WEB-INF” directory. This application’s faces-config.xml file is shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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>


See the <from-view-id>, it’s defined as “/pages/index.jsp”. It tells FacesServlet that while there’s a request from “/pages/index.jsp” with an action attribute “login”, defined in <from-outcome>, it should be navigated to “/pages/welcome.jsp”, defined in <to-view-id>.

You also see the <managed-bean>. It defines the bean used by the JSF pages. The bean is in session scope, defined in <managed-bean-scope>. The UserBean is defined like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
package onlyfun.caterpillar;
 
public class UserBean {
    private String name;
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
}


The bean’s name “user” is defined in <managed-bean-name>. You could use #{user.name} to get the bean’s name property in JSF pages. Just as you see in the “pages/index.jsp”.

OK! We still have one page “pages/welcome.jsp” not written. It’s shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<title>JSF Demo</title>
</head>
<body>
    <f:view>
        <h3>Hello! <h:outputText value="#{user.name}"/>!</h3>
        <h3>Welcome your JavaServer Faces!</h3>
    </f:view>
</body>
</html>


The <h:outputText> is used to output the name property of user bean.

Now startup your Servlet container and point your browser to the application’s URL, such as http://localhost:8080/jsfDemo/pages/index.faces. The “pages/index.jsp” would be loaded first. After you enter your name in the text field of “pages/index.jsp” and click the submit button, your name would be stored in the user bean’s name property. Then the FacesServlet navigate from “pages/index.jsp” to “pages/welcome.jsp” according to navigation rules in the faces-config.xml. The “pages/welcome.jsp” would get the name property from the user bean and show it in the page.


caterpillar edited on 2005-02-08 00:17
reply to postreply to post
良葛格學習筆記
作者 Re:Your First JavaServer Faces. [Re:caterpillar]
dingwen





發文: 14
積分: 0
於 2005-02-25 12:21 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
A very good article for me. I am a newbie in Java world. Now, I am tortured by J2EE and WebLogic assignmnet which is accompanied with ASP.NET assignment. Hope get more involved in this forum.
Sorry not talking anything about the JSF technology here. Just a few suggestions on writing style.

If you want the reader/user to do something, you should use "need to" or "have to" .
For example,

"First, you should download the “JavaServer Faces Reference
Implementation..."

Change to:

"First, you need to download the “JavaServer Faces Reference
Implementation..."

"Should" is used when you want to alert reader/user to do something. However, the following paragraph is a good use of "should" and "need to".

"Now, you need to define your web application in the “WEB-INF/web.xml”
file. All JSF pages requested should be processed by a special servlet
“FacesServlet”. You should define it in the “web.xml”...."

Try not to use "it" to replace an important step, a concept, or an element. People will get confused after many "they", "it", "this", or "that".

For example:

"First, you should download the “JavaServer Faces Reference
Implementation” from
http://java.sun.com/j2ee/javaserverfaces/download.html. It’s a zipped file.
After you download it and decompress it, look into the directory “lib”...."

Change to:

"First, you need to download the “JavaServer Faces Reference
Implementation” from
http://java.sun.com/j2ee/javaserverfaces/download.html. It’s a zip file.
After you download and decompress the file, look into the decompressed
file directory “lib”...."

Also, in the article, there are a few missing commas and plural and singular errors.


reply to postreply to post
作者 Re:Your First JavaServer Faces. [Re:caterpillar]
caterpillar

良葛格

版主

發文: 2613
積分: 70
於 2005-02-25 12:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝感謝。。。很高興有您的指正。。。

我想我寫的雖然人家看得懂,但是一定有很多地方在懂英文的人眼中怪怪的。。。。趕將這篇收藏起來。。。。英文功力增加100倍。。。。Big SmileBig SmileBig Smile


reply to postreply to post
良葛格學習筆記
作者 Re:Your First JavaServer Faces. [Re:caterpillar]
gowingo





發文: 1
積分: 0
於 2005-03-04 16:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Smile It's a good!

reply to postreply to post
作者 Re:Your First JavaServer Faces. [Re:caterpillar]
noellaw





發文: 7
積分: 0
於 2005-03-12 20:15 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
thanks ,a very good introduction to JSF

reply to postreply to post
作者 Re:Your First JavaServer Faces. [Re:caterpillar]
mangoxiang





發文: 1
積分: 0
於 2005-04-20 12:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
写得挺好,给了我很多指导!!!!
谢谢!!
我是刚学的,所以希望有高人指导一下 。。。


reply to postreply to post
作者 Re:Your First JavaServer Faces. [Re:caterpillar]
kjavaer





發文: 1
積分: 0
於 2005-05-10 09:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
毕竟现在介绍JSF的文章不多呀!尤其是搂主这样敢大胆持外国螃蟹的Smile

reply to postreply to post
作者 Re:Your First JavaServer Faces. [Re:caterpillar]
smartcdrw





發文: 39
積分: 0
於 2005-06-07 09:49 user profilesend a private message to usersend email to smartcdrwreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
從這學起JSP的第一步
最近不知怎麼了忽然想試試不同的東西
感謝讓我有學習的機會


reply to postreply to post
» JWorld@TW »  Web Framework » JSF

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8