jsf 的導航演示(navigation)

注:jsf演示例子都在同一個workspace中完成的,所以你會看到一些與本演示例子無關的代碼!

UserBean.java

package org.baicai.jsf_document.simple_navigation;

public class UserBean
{
    private String name;
    private String password;
    private String errMessge;
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
 public String getPassword()
 {
  return password;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }
 public String getErrMessge()
 {
  return errMessge;
 }
 public void setErrMessge(String errMessge)
 {
  this.errMessge = errMessge;
 }
    /**
     * 驗證登錄用戶
     * @return
     */
 public String verify()
 {
  System.out.println("----調用UserBean的verify()-----");
  if(!name.equals("admin")||!password.equals("admin"))
  {
   errMessge="名稱或密碼錯誤";
   return "failure";
  }else
  {
   return "success";
  }
 }
   
}

WebRoot/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!--第一步: 添加jsf的基本(html)標籤標籤庫,和jsf的核心(cord)標籤標籤庫 -->
     <%@ taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>
     <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- f:view 相當於超文本標記語言的html,是必須要添加的。否則會出錯 -->
<!-- Component javax.faces.component.UIViewRoot@92b535 not expected type.
 Expected: javax.faces.component.UIForm.  Perhaps you're missing a tag?  -->
 <f:view>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jsf Menu</title>
</head>
<body>
 <!-- jsf的表單標籤,當頁面有h:commandLink,h:commandButton標籤時該標籤是必須要添加的標籤 否則這些標籤沒有用 -->
 <!--This link is disabled as it is not nested within a JSF form. -->
<h:form>
   <!-- h:commandLink 鏈接標籤,相當於html中的a元素 . action屬性的值有兩種形式 ,本形式會直接根據導航進到下一個頁面,value屬性的值爲頁面的顯示值 -->
  <h:commandLink action="simpleDemo" value="第一個jsf演示"></h:commandLink><br>
  <h:commandLink action="simplenavigation" value="jsf導航演示"></h:commandLink><br>
</h:form>
</body>
</f:view>
</html>

WebRoot/simplenavigation/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>
     <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<f:view>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>smplenavigation</title>
</head>
<body >
   <h:form>
        <h:outputText value="請輸入用戶名"></h:outputText>
        <h:inputText value="#{navigationUser.name}"></h:inputText>
        <h:outputText value="請輸入用戶名"></h:outputText>
        <h:inputSecret value="#{navigationUser.password}"></h:inputSecret>
        <!--commandButton提交標籤 。action屬性的值的另外一種形式   -->
        <h:commandButton action="#{navigationUser.verify}" value="登錄" ></h:commandButton>
       
        <h:outputText value="#{navigationUser.errMessge}" style="color: red"></h:outputText>
   </h:form>
</body>
</f:view>
</html>

WebRoot/simplenavigation/welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>
     <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<f:view>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>smplenavigation</title>
</head>
<body>
    <h:form>
       <h3>
        <h:outputText value="歡迎#{navigationUser.name}登錄!"/>
       </h3>
    </h:form>
</body>
</f:view>
</html>

 

WebRoot/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
   <!-- 指定加載多個faces-config.xml文件,文件之間用逗號分開。  默認會加載faces-config.xml文件, -->
  <context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>
                 /WEB-INF/faces-config/faces-config.xml,
                 /WEB-INF/faces-config/simpledemo-faces-config.xml,
                 /WEB-INF/faces-config/simplenavigation-faces-config.xml
   </param-value>
  </context-param>
 
  <!-- faces 的核心配置 -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>


 

WebRoot/WEB-INF/faces-config/faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
  <faces-config>
        <!-- 頁面導航 -->
        <navigation-rule>
          <!-- 事件觸發頁面 -->
               <from-view-id>/index.jsp</from-view-id>
               <navigation-case>
               <!-- 返回結果的標記 -->
                  <from-outcome>simpleDemo</from-outcome>
               <!--觸發事件後陳顯結果的頁面  -->
                  <to-view-id>/simpleDemo/index.jsp</to-view-id>
               </navigation-case>
              
              
               <navigation-case>
                      <from-action>simplenavigation</from-action>
          <to-view-id>/simplenavigation/index.jsp</to-view-id>
    </navigation-case>

  </navigation-rule>
  </faces-config>

WebRoot/WEB-INF/faces-config/simplenavigation-faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
  <faces-config>
   <navigation-rule>
        <from-view-id>/simplenavigation/index.jsp</from-view-id>
          <!-- 根據頁面上調用的managerBean中的方法得到返回字符串來判斷結果頁面 -->
        <navigation-case>
      <!-- 登錄成功後 根據返回字符串success進入sg/welecom.jsp頁面 -->
           <from-outcome>success</from-outcome>
           <to-view-id>/simplenavigation/welcome.jsp</to-view-id>
           <!-- 重定向  瀏覽器主動要求新網頁 地址欄 發生明顯的變化-->
           <redirect/>
        </navigation-case>
       
        <navigation-case>
        <!--登錄失敗後的處理:根據返回字符串failure進入到sg/index.jsp頁面 -->
           <from-outcome>failure</from-outcome>
           <to-view-id>/simplenavigation/index.jsp</to-view-id>
        </navigation-case>
   </navigation-rule>
  
   <managed-bean>
         <managed-bean-name>navigationUser</managed-bean-name>
         <managed-bean-class>org.baicai.jsf_document.simple_navigation.UserBean</managed-bean-class>
         <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>
  </faces-config>

 

發佈了39 篇原創文章 · 獲贊 7 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章