風(fēng)人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評(píng)論 - 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>


          六、測(cè)試

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

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 胶州市| 晋中市| 固镇县| 武鸣县| 宣化县| 金华市| 扎囊县| 白玉县| 兴文县| 宁化县| 马边| 淮滨县| 乌兰察布市| 河源市| 宜兰县| 平阳县| 益阳市| 台南市| 时尚| 山阴县| 北京市| 湾仔区| 车险| 伽师县| 浦北县| 老河口市| 砀山县| 萝北县| 阿图什市| 鲁甸县| 汉沽区| 仙居县| 大兴区| 永州市| 凤翔县| 沾益县| 大港区| 泰兴市| 阿城市| 井冈山市| 渭源县|