`
thinktothings
  • 浏览: 766715 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JSF2.0的一个简单Demo

    博客分类:
  • JSF
阅读更多

学习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;
    }
}

 

  • 大小: 21.4 KB
分享到:
评论
1 楼 beykery 2015-02-15  
你这也太复杂了。。。。jsf2不应该是这样的。。。。

相关推荐

    jsf2.0 hibernate3.2 spring2.5整合 jar包(所有)

    包括richfaces mysql log4j 在eclipse里手动配置环境经常会因jar出现种种问题,我把完整的jsf2.0 spring2.5 hibernate3整合所有jar传上来希望有用 如果有需要我会传上一个jsf2.0 spring hibernate的小demo上来

    JSF2.0-hello-world-example-2.1.7.zip

    引用别人的Demo,自己运行。做个备份。 http://www.mkyong.com/jsf2/jsf-2-0-hello-world-example/

    richfaces jsf2.0

    学习的好助手richfaces-demo-jsf2-3.3.3.CR1-tomcat6.war

    jsf+spring+hibernate整合demo

    jsf1.2+spring2.0+hibernate3.2整合demo part1

    jsf2.1demo

    JavaServer Faces+eclipse,入门教程。本人觉得挺好用,和jsp细节不同。需要自己体会

    JSF2 + WELD 1.0 + ibatis 例子(个人日志系统)

    本程序使用JSF2.0,WELD(CDI)1.0 ,ibatis开发的一个个人日志系统,系统功能很简单. 本demo谨献给热爱java,学习weld(CDI)的朋友们.

    ICEFaces 2.0

    根据以往做过的ICEface的经验, 初次尝试了ICEFaces 2.0Beta. 极力推荐这个便捷的AJAX框架.开发工具是Intellij Idea 9.0.3. 废话少说,各自体验吧.

    demo:Java EE 7 演示

    此项目设置为允许您使用 JSF 2.0、CDI 1.0、EJB 3.1、JPA 2.0 和 Bean Validation 1.0 创建兼容的 Java EE 7 应用程序。 它包括一个持久性单元和一些示例持久性和事务代码,向您介绍企业 Java 中的数据库访问。系统...

    spring web flow demo

    本教程分析了 Spring Web Flow 2.0 的技术要点,并且通过创建一个示例应用程序,展示了 Spring Web Flow 2.0 的基本知识。 开始之前 关于本教程 本教程通过一个简化的购物车应用,介绍了如何使用 Spring Web Flow ...

    iuhyiuhkjh908u0980

    PrettyFaces: EL API访问PrettyContext 支持JSF 1.1 增强了错误页面和servlet重定向 PrettyFaces是一个JSF1.2和JSF2.0的扩展,用来创建便于书签收藏、漂亮的网址。 PrettyFaces优雅的解决了这个问题,包括诸如功能:...

    struts-2.3.30-all所有jar包

    commons-digester-2.0.jar, commons-fileupload-1.3.2.jar, commons-io-2.2.jar, commons-lang-2.4.jar, commons-lang3-3.2.jar, commons-logging-1.1.3.jar, commons-validator-1.3.1.jar, core-0.6.2.jar, dwr-...

    271个java需要用的jar包

    struts2-osgi-demo-bundle-2.3.15.3.jar struts2-osgi-plugin-2.3.15.3.jar struts2-oval-plugin-2.3.15.3.jar struts2-pell-multipart-plugin-2.3.15.3.jar struts2-plexus-plugin-2.3.15.3.jar struts2-portlet-...

Global site tag (gtag.js) - Google Analytics