
本动手实验室假定您拥有以下技术的基本知识或者具备相关编程经验:
开始之前,需要在您的计算机中安装以下软件。
在此练习中,您将了解如何编译和运行 "core-javaquiz" 示例应用程序。
0. 启动 NetBeans IDE。
1. 打开 core-javaquiz NetBeans 项目。
2. 编译和运行 core-javaquiz 项目。
| <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> |
| <?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> |
| 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" 示例应用程序,从而对该应用程序的运行机制有了一定的了解。