JSF2应用笔记(一)

最近打算做一个WEB应用系统,以前对WEB开发虽然有一些了解,但是一直没有深入,对Struts、Tapestry等框架有一点了解,就在网上了解最新的WEB开发技术,发现JSF似乎有比较好的发展前景,最新的JSF2具有更多的功能,决定使用JSF2开始此WEB应用系统,在此记录开发中的心得,遇到的问题,希望能够坚持下来!

使用Eclipse(WTP)开发工具,安装了JBoss Tools RichFaces。

开始:

  1. 下载JSF2,目前最新实现版本是mojarra-2.0.2,从此处下载
  2. 新建web应用,将mojarra-2.0.2中lib目录下的jsf-api.jar和jsf-impl.jar两个文件复制到WEB-INF/lib目录下;
  3. 编辑web.xml
    <?xml version='1.0' encoding='UTF-8'?>
    <!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>
        <display-name>Facelets Tutorial</display-name>
        <description>Number Guess Game</description>
        <!-- Change to "Production" when you are ready to deploy -->
        <context-param>
            <param-name>javax.faces.PROJECT_STAGE</param-name>
            <param-value>Development</param-value>
        </context-param>
        <!-- Faces Servlet -->
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Faces Servlet Mapping -->
        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.jsf</url-pattern>
        </servlet-mapping>
        <!-- welcome file mapping -->
        <welcome-file-list>
            <welcome-file>faces/hello.xhtml</welcome-file>
        </welcome-file-list>
    </web-app>
  4. 增加 Messages.properties 文件;
  5. 编辑布局文件 layout.xhtml
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">
          
      <f:loadBundle basename="com.gzkj.cdms.Messages" var="msg" />
      <h:head>
        <title><ui:insert name="pageTitle">综合灾害监测系统</ui:insert></title>
      </h:head>
      <body>
        <h:outputStylesheet library="style" name="001.css" target="body"/>
        <div id="header">注销</div>
        <div id="contain">
          <div id="mainbg">
            <div id="right">
              <div class="text">
                <ui:insert name="body">Page Body</ui:insert>
              </div>
            </div>
            <div id="left">
              <div class="text">left</div>
            </div>
          </div>    
        </div>
        <div id="footer">
            <a href="http://www..com"> 发展有限公司</a>  版权所有<br/>
            联系电话:<br/>
        </div>
      </body>
    		
    </html>
  6. 编辑文件 inputname.xhtml文件
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:c="http://java.sun.com/jstl/core">
    
      		<f:loadBundle basename="com.gzkj.cdms.Messages" var="msg" />
    
       		<ui:composition template="/templates/layout.xhtml">
    						
    			<ui:define name="pageTitle">Input User Name</ui:define>
      
    			<!-- ui:define name="pageHeader">Facelets Hello Application</ui:define-->
    
    			<ui:define name="body">
    				<h:message showSummary="true" showDetail="false" style="color: red; font-weight: bold;" for="name" />
    				<h:form id="helloForm">
    					${msg.prompt}
    					<h:inputText required="true" id="name" value="#{helloBean.name}" />
    					<h:commandButton id="submit" action="greeting" value="Say Hello" />
    				</h:form>
    				
    			</ui:define>
    		</ui:composition>
    </html>
  7. helloBean.java
    package com.gzkj.cdms;
    
    import java.io.Serializable;
    
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    
    @ManagedBean
    @SessionScoped
    public class HelloBean implements Serializable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -4860646105856875593L;
    
    	   private String name;
    
    	   public String getName() { return name;}
    	   
    	   public void setName(String name) { this.name = name; }
    
    }
  8. 文件目录:
    JSF2-1
posted @ 2010-01-26 15:11  海畅  阅读(1636)  评论(0)    收藏  举报