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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章