風(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)航:
           
          主站蜘蛛池模板: 武宁县| 黑山县| 句容市| 沾化县| 大同市| 阳朔县| 特克斯县| 两当县| 全椒县| 宜宾市| 黔西| 永嘉县| 高安市| 金溪县| 五指山市| 攀枝花市| 平陆县| 青铜峡市| 呼伦贝尔市| 安阳市| 诸城市| 漳平市| 郸城县| 晋城| 贵南县| 南漳县| 大兴区| 陇西县| 乐平市| 武邑县| 黔江区| 炎陵县| 报价| 株洲市| 独山县| 鸡东县| 建始县| 南雄市| 玛纳斯县| 冀州市| 林西县|