JSF 页面导航

Sang Shin, sang.shin@sun.com, Sun Microsystems, www.javapassion.cn



在本实验室中,您将实践 JSF 的导航功能。本实验室使用的一些示例应用程序来自 Core JSF 一书(David Geary 和 Cay Horstmann 著) 发布的源代码



预计时间:90 分钟(不包括课外练习)


前提条件

本动手实验室假定您拥有以下技术的基本知识或者具备相关编程经验:


软件需求

开始之前,需要在您的计算机中安装以下软件。


可以使用的操作系统平台

变更记录



实验室练习


练习 1:编译和运行 "core-javaquiz" 示例应用程序

在此练习中,您将了解如何编译和运行 "core-javaquiz" 示例应用程序。


(1.1)打开、编译和运行 "core-javaquiz" 示例应用程序

0. 启动 NetBeans IDE。
1. 打开 core-javaquiz NetBeans 项目。

2. 编译和运行 core-javaquiz 项目。




(1.2) 深入了解  "core-javaquiz" 示例应用程序


1. 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><h:outputText value="#{msgs.title}"/></title>
        </head>
        <body>
            <h:form>
                <p>
                    <h:outputText value="#{quiz.question}"/>
                </p>
                <p>
                    <h:inputText value="#{quiz.response}"/>
                </p>
                <p>
                    <h:commandButton value="#{msgs.answerButton}"
                                     action="#{quiz.answerAction}"/>
                </p>
            </h:form>
        </body>
    </f:view>
</html>

2. faces-config.xml

<?xml version="1.0"?>
<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_1_2.xsd"
    version="1.2">
   <navigation-rule>
      <navigation-case>
         <from-outcome>success</from-outcome>
         <to-view-id>/success.jsp</to-view-id>        
         <redirect/>
      </navigation-case>
      <navigation-case>
         <from-outcome>again</from-outcome>
         <to-view-id>/again.jsp</to-view-id>
      </navigation-case>
      <navigation-case>
         <from-outcome>failure</from-outcome>
         <to-view-id>/failure.jsp</to-view-id>
      </navigation-case>
      <navigation-case>
         <from-outcome>done</from-outcome>
         <to-view-id>/done.jsp</to-view-id>
      </navigation-case>
      <navigation-case>
         <from-outcome>startOver</from-outcome>
         <to-view-id>/index.jsp</to-view-id>
      </navigation-case>
   </navigation-rule>

   <managed-bean>
      <managed-bean-name>quiz</managed-bean-name>
      <managed-bean-class>com.corejsf.QuizBean</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>

   <application>
      <resource-bundle>
         <base-name>com.corejsf.messages</base-name>
         <var>msgs</var>
      </resource-bundle>
   </application>
</faces-config>

3. QuizBean.java

package com.corejsf;

public class QuizBean {
    private int currentProblem;
    private int tries;
    private int score;
    private String response;
    private String correctAnswer;
   
    // here, we hardwire the problems. In a real application,
    // they would come from a database
    private Problem[] problems = {
        new Problem(
                "What trademarked slogan describes Java development? Write once, ...",
                "run anywhere"),
        new Problem(
                "What are the first 4 bytes of every class file (in hexadecimal)?",
                "CAFEBABE"),
        new Problem(
                "What does this statement print? System.out.println(1+\"2\");",
                "12"),
        new Problem(
                "Which Java keyword is used to define a subclass?",
                "extends"),
        new Problem(
                "What was the original name of the Java programming language?",
                "Oak"),
        new Problem(
                "Which java.util class describes a point in time?",
                "Date")
    };
   
    public QuizBean() { startOver(); }
   
    // PROPERTY: question
    public String getQuestion() {
        return problems[currentProblem].getQuestion();
    }
   
    // PROPERTY: answer
    public String getAnswer() { return correctAnswer; }
   
    // PROPERTY: score
    public int getScore() { return score; }
   
    // PROPERTY: response
    public String getResponse() { return response; }
    public void setResponse(String newValue) { response = newValue; }
   
    public String answerAction() {
        tries++;
        if (problems[currentProblem].isCorrect(response)) {
            score++;
            nextProblem();
            if (currentProblem == problems.length) return "done";
            else return "success";
        } else if (tries == 1) {
            return "again";
        } else {
            nextProblem();
            if (currentProblem == problems.length) return "done";
            else return "failure";
        }
    }
   
    public String startOverAction() {
        startOver();
        return "startOver";
    }
   
    private void startOver() {
        currentProblem = 0;
        score = 0;
        tries = 0;
        response = "";
    }
   
    private void nextProblem() {
        correctAnswer = problems[currentProblem].getAnswer();
        currentProblem++;
        tries = 0;
        response = "";
    }
}


结束语

在此练习中,您编译和运行了一个现成的 "core-javaquiz" 示例应用程序,从而对该应用程序的运行机制有了一定的了解。

                                                                                                                        返回顶部




课外练习(针对 Sang Shin“Java EE 编程在线课程”的学习者)


1.课外练习是根据以下要求修改 core-javaquiz 项目。如果没有 core-javaquiz 项目,可以使用本动手实验室压缩文件中的 <LAB_UNZIPPED_DIRECTORY>/jsfnavigation/samples/core-javaquiz(可以通过复制 core-javaquiz 项目创建一个新项目。您可以为课外练习项目指定任意名称,但此处我将其称为 Mycore-javaquiz。)
2. 将以下文件 J2EEHomework-jsfnavigation 作为 发送主题 发送至 j2eehomeworks@sun.com


                                                                                                                    返回顶部





原文链接: http://www.javapassion.com/handsonlabs/jsfnavigation/