构建你自己的应用程序 
  
这一部分具体讲述如何一步一步地创建你自己的JavaServer Faces应用。我所使用的例子很简单,它要求用户输入他(她)的名字,然后点击Submit按钮,然后应用程序会向用户显示一个欢迎的信息。  
- 创建如下目录结构: 
  
  c:\tomcat4.1\webapps 
                 hello 
                   src 
                   web 
                     WEB-INF 
                       web.xml 
                       lib 
                       classes 
  
这个目录结构的基本意思是说,我想创建一个叫做hello的新应用程序。在hello子目录下,有一个src子目录,里面放所有的Java 源文件;还有一个web子目录,该目录下有一个WEB-INF目录,里面包含web.xml文件及另外两个子目录,分别是lib和classes。  
- 把c:\jsf-ea3\lib 目录下所有的jar文件拷贝到我们上面创建的lib子目录中。 
 - 创建web.xml 文件,用来配置我们的这个Web应用。在使用JavaServer Faces的时候,必须指定几个配置,诸如:(1) servlet context listener、(2) 用来处理JavaServer Faces 请求的servlet以及 (3) 上述servlet的servlet mapping。 下面的代码是这个应用程序的一个配置文件。 
  
代码1: web.xml  
  
<?xml version="1.0"?> 
<!DOCTYPE web-app PUBLIC 
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
  "http://java.sun.com/dtd/web-app_2_3.dtd"> 
  
<web-app> 
    <listener> 
        <!-- Used to initialize and destroy the application helper and register a Renderer to a RenderKit --> 
        <listener-class>BasicServletContextListener</listener-class> 
    </listener> 
  
    <!-- Faces Servlet --> 
    <servlet> 
        <servlet-name>Faces Servlet</servlet-name> 
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
        <!-- FaceServlet should be loaded when the application starts up --> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
  
    <!-- Faces Servlet Mapping. Map the path to the servlet when a request for 
            the servlet is received --> 
    <servlet-mapping> 
        <servlet-name>Faces Servlet</servlet-name> 
        <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
</web-app> 
- 使用JavaServer Faces 标记创建HTML页面。 
  
首先我们写一个 index.html 页面,这是用户进入这个应用程序的第一个页面。这个页面里面有一个超级连接,点击它可以启动应用程序。代码如下: 
  
代码2: index.html  
  
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> 
<html> 
<head> 
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
   <meta name="GENERATOR" content="Mozilla/4.75 [en] (WinNT; U) [Netscape]"> 
   <title>index</title> 
</head> 
<body> 
  
<P>Click <a href="faces/index.jsp">here</a> to start the application.</P> 
  
<br> 
<hr WIDTH="100%"> 
</body> 
</html> 
  
当用户点击了“here”,系统就会装载“index.jsp”,代码如下: 
  
代码3: index.jsp  
  
<HTML> 
    <HEAD> <title>Hello</title> </HEAD> 
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 
    <body bgcolor="white"> 
    <h2>What is your name?</h2> 
    <jsp:useBean id="UserNameBean" class="UserNameBean" scope="session" /> 
    <f:use_faces> 
    <h:form id="helloForm" formName="helloForm" > 
          <h:input_text id="username" 
                 modelReference="UserNameBean.userName"/> 
          <h:command_button id="submit" label="Submit" commandName="submit" /> 
    </h:form> 
    </f:use_faces> 
</HTML> 
  
这个JSP页面有几个值得注意的地方:  
- 定制标记库。组件标记库不需要硬编码HTML来构成UI组件,从而使得组件可以复用,而且core tag library可以让向组件注册事件和其它行为更为简单。 
 - <jsp:useBean> 这个标记用来创建一个JavaBean的实例,它的类为UserNameBean,名字也是UserNameBean。当然,这个是在服务器上实现的。 
 - form 标记用来表示一个输入窗体。Input_text和command_button用来表示该窗体中的组件,嵌套在form标记中。 
 - input_text标记表示一个文本框,可以供用户输入一个字符串。这个标记有两个属性,分别为id和modelReference。id属性对应这个组件,是可选的。modelReference代表模型对象的属性,这个属性保存了我们输入文本框的值。 
 - command_button 代表了一个提交按钮。 
   
  
- 如有必要,编写模型对象(JavaBean组件)。 
  
模型对象bean 就像其它JavaBean组件一样:它有一组访问方法。下面的代码段显示了我们这个应用中要使用的JavaBean组件。 
  
代码4: UserNameBean.java 
  
public class UserNameBean { 
   String userName = null; 
  
   public UserNameBean () { 
   } 
  
   public void setUserName(String user_name) { 
      userName = user_name; 
   } 
  
   public String getUserName() { 
      return userName; 
   } 
} 
  
<未完待续> 
其余部分请参考: 
http://www.csdn.net/develop/read_article.asp?id=18705 用JavaServer Faces开发Web应用(1) 
http://www.csdn.net/develop/read_article.asp?id=18707 用JavaServer Faces开发Web应用(2) 
http://www.csdn.net/develop/read_article.asp?id=18710 用JavaServer Faces开发Web应用(4) 
http://www.csdn.net/develop/read_article.asp?id=18712 用JavaServer Faces开发Web应用(5)  
 
  |