JSF2.0的一个简单Demo

学习javaeetutorial6.pdf 106页到113页 附件中javaeetutorial6.zip

环增需求:JDK1.6    服务器:tomcat6.0.29  web项目   框架:JSF2.0

简单描述:项目启动后,进入欢迎界面faces/greeting.xhtml

 <welcome-file-list>
        <welcome-file>faces/greeting.xhtml</welcome-file>
   </welcome-file-list>

 

Servlet

 <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

 

交给JSF处理

进入faces-config.xml的配置

/greeting.xhtml

<navigation-rule>
    <description>
        The decision rule used by the NavigationHandler to
        determine which view must be displayed after the
        current view, greeting.jsp is processed.
    </description>
    <from-view-id>/greeting.xhtml</from-view-id>
    <navigation-case>
        <description>
            Indicates to the NavigationHandler that the response.jsp
            view must be displayed if the Action referenced by a
            UICommand component on the greeting.jsp view returns
            the outcome "success".
        </description>
      <from-outcome>success</from-outcome>
      <to-view-id>/response.xhtml</to-view-id>
    </navigation-case>

  </navigation-rule>

 

 

由于请求的操作,应该action默认的不是success 即<from-outcome>success</from-outcome>
不符合,所以直接返回请求页面/greeting.xhtml

进入开始页面,

<managed-bean>
    <description>
      The backing bean that backs up the guessNumber Web application
    </description>
    <managed-bean-name>UserNumberBean</managed-bean-name>
    <managed-bean-class>guessNumber.UserNumberBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
      <property-name>minimum</property-name>
      <property-class>long</property-class>
      <value>0</value>
    </managed-property>
    <managed-property>
      <property-name>maximum</property-name>
      <property-class>long</property-class>
      <value>10</value>
    </managed-property>
  </managed-bean>

 

配的是Session作用域,所以每次请求的,bean只有第一次会被实例化(无参构造函数生成的随机数就被确定了,以后的请求这这个值是不会改变的),然后是返回结果页面response.xhtml只显示Bean中的UserNumberBean.response,如果用户输的value等于随机数就显示猜对了,否则显示猜错了。。

 

 

 

 

JSF的一个简单例子:

说明:

Web\WEB-INF\web.xml配置

<!--上下文参数--> 
<context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
<!--servlet路径拦截-->
    <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>
<!--session的生命周期-->
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
<!--欢迎页面-->
    <welcome-file-list>
        <welcome-file>faces/greeting.xhtml</welcome-file>
    </welcome-file-list>

 

Web\WEB-INF\faces-config.xml配置

 

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

   <application>
    <resource-bundle>
        <base-name>guessNumber.ApplicationMessages</base-name>
        <var>ErrMsg</var>
    </resource-bundle>
    <locale-config>
      <default-locale>en</default-locale>
    </locale-config>

    </application>

<managed-bean>
    <description>
      The backing bean that backs up the guessNumber Web application
    </description>
    <managed-bean-name>UserNumberBean</managed-bean-name>
    <managed-bean-class>guessNumber.UserNumberBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
      <property-name>minimum</property-name>
      <property-class>long</property-class>
      <value>0</value>
    </managed-property>
    <managed-property>
      <property-name>maximum</property-name>
      <property-class>long</property-class>
      <value>10</value>
    </managed-property>
  </managed-bean>
  <navigation-rule>
    <description>
        The decision rule used by the NavigationHandler to
        determine which view must be displayed after the
        current view, greeting.jsp is processed.
    </description>
    <from-view-id>/greeting.xhtml</from-view-id>
    <navigation-case>
        <description>
            Indicates to the NavigationHandler that the response.jsp
            view must be displayed if the Action referenced by a
            UICommand component on the greeting.jsp view returns
            the outcome "success".
        </description>
      <from-outcome>success</from-outcome>
      <to-view-id>/response.xhtml</to-view-id>
    </navigation-case>

  </navigation-rule>

  <navigation-rule>
   <description>
        The decision rules used by the NavigationHandler to
        determine which view must be displayed after the
        current view, response.jsp is processed.
    </description>
    <from-view-id>/response.xhtml</from-view-id>
    <navigation-case>
        <description>
            Indicates to the NavigationHandler that the greeting.jsp
            view must be displayed if the Action referenced by a
            UICommand component on the response.jsp view returns
            the outcome "success".
        </description>
        <from-outcome>success</from-outcome>
      <to-view-id>/greeting.xhtml</to-view-id>
    </navigation-case>
  </navigation-rule>

</faces-config>

 

 

guessNumber.UserNumberBean.java

/*
 * Copyright 2009 Sun Microsystems, Inc.
 * All rights reserved.  You may not modify, use,
 * reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * http://developer.sun.com/berkeley_license.html
 */


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guessNumber;

import java.util.Random;


public class UserNumberBean {
    Integer randomInt = null;
    Integer userNumber = null;
    String response = null;
    private boolean maximumSet = false;
    private boolean minimumSet = false;
    private long maximum = 0;
    private long minimum = 0;

    public UserNumberBean() {
        Random randomGR = new Random();
        randomInt = new Integer(randomGR.nextInt(10));
        System.out.println("Duke's number: " + randomInt);
    }

    public void setUserNumber(Integer user_number) {
        userNumber = user_number;
    }

    public Integer getUserNumber() {
        return userNumber;
    }

    public String getResponse() {
        if ((userNumber != null) && (userNumber.compareTo(randomInt) == 0)) {
            return "Yay! You got it!";
        } else {
            return "Sorry, " + userNumber + " is incorrect.";
        }
    }

    public long getMaximum() {
        return (this.maximum);
    }

    public void setMaximum(long maximum) {
        this.maximum = maximum;
        this.maximumSet = true;
    }

    public long getMinimum() {
        return (this.minimum);
    }

    public void setMinimum(long minimum) {
        this.minimum = minimum;
        this.minimumSet = true;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值