風(fēng)人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
          數(shù)據(jù)加載中……

          JSF-- 入門 First Example

          一、JSF開發(fā)環(huán)境
             使用myeclipse的功能,添加相關(guān)lib到classpath。 或者從sun的官方網(wǎng)站下載相關(guān)lib。
          http://java.sun.com/javaee/javaserverfaces/download.html
          二、JSP頁面
                index.jsp

          <html>
              
          <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
              
          <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

              
          <f:view>
                  
          <head>
                      
          <title>A Simple JavaServer Faces Application</title>
                  
          </head>
                  
          <body>
                      
          <h:form>
                          
          <h3>Please enter your name and password.</h3>
                          
          <table>
                              
          <tr>
                                  
          <td>Name:</td>
                                  
          <td><h:inputText value="#{user.name}" /></td>
                              
          </tr>
                              
          <tr>
                                  
          <td>Password:</td>
                                  
          <td><h:inputSecret value="#{user.password}" /></td>
                              
          </tr>
                          
          </table>
                          
          <p><h:commandButton value="Login" action="login" /></p>
                      
          </h:form>
                  
          </body>
              
          </f:view>
          </html>


                welcome.jsp

          <html>
              
          <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
              
          <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

              
          <f:view>
                  
          <head>
                      
          <title>A Simple JavaServer Faces Application</title>
                  
          </head>
                  
          <body>
                      
          <h3> Welcome to JavaServer Faces,
                          
          <h:outputText value="#{user.name}" /> !
                      
          </h3>
                  
          </body>
              
          </f:view>
          </html>


          三、Bean
                UserBean .java
             package com.test.jsf.bean;

          public class UserBean {

           private String name;

           private String password;

           // PROPERTY: name
           public String getName() {
            return name;
           }

           public void setName(String newValue) {
            name = newValue;
           }

           // PROPERTY: password
           public String getPassword() {
            return password;
           }

           public void setPassword(String newValue) {
            password = newValue;
           }
           
           public String check() {
            name = "wxf";
            return "success";
            //return SUCCESS;
           }
          }

          四、faces-config
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "<faces-config >
           <navigation-rule>
           
              <from-view-id>/login/index.jsp</from-view-id>
              <navigation-case>
                 <from-outcome>login</from-outcome>
                 <to-view-id>/login/welcome.jsp</to-view-id>
              </navigation-case>
           </navigation-rule>
           
           <managed-bean>
              <managed-bean-name>user</managed-bean-name>
              <managed-bean-class>
                 com.test.jsf.bean.UserBean
              </managed-bean-class>
              <managed-bean-scope>session</managed-bean-scope>
           </managed-bean>

          </faces-config>
          五、web.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns="
           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    <context-param>
            <param-name>javax.faces.CONFIG_FILES</param-name>
            <param-value>/WEB-INF/faces-config.xml</param-value>
           </context-param>

           <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>0</load-on-startup>
           </servlet>
           <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.faces</url-pattern>
           </servlet-mapping>
          </web-app>


          六、測試

             http://localhost:8080/jsf/login/index.faces
          注意,調(diào)用jsp頁面時(shí)一定要使用faces,結(jié)尾,否則jsf的標(biāo)簽將無法識別

          posted on 2007-04-26 12:56 風(fēng)人園 閱讀(608) 評論(0)  編輯  收藏 所屬分類: JSF


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 静安区| 陆丰市| 阳江市| 石嘴山市| 松原市| 阿克苏市| 宜宾县| 林甸县| 正宁县| 侯马市| 铁力市| 昆山市| 石城县| 永康市| 新泰市| 台南县| 基隆市| 蓝田县| 镇康县| 西吉县| 白城市| 碌曲县| 旺苍县| 乐安县| 台东县| 陕西省| 田阳县| 清徐县| 仁化县| 东乌珠穆沁旗| 平潭县| 阿勒泰市| 齐河县| 新竹市| 夹江县| 永宁县| 乌鲁木齐县| 平原县| 常德市| 达孜县| 尚义县|