首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

JSF 向导 用 JSF 2 和 CDI 实现一个向导-2

JSF 向导 用 JSF 2 和 CDI 实现一个向导-2

测验应用程序这个测验应用程序的文件如图 3 所示:
图 3. 此应用程序的文件我用一个 JSF 2 模板(/templates/wizardTemplate.xhtml)实现此测验向导,向导视图(/quizWizard/wizard.xhtml)使用了这个模板。
除了上述模板和视图外,我还有针对向导的每个组成块的 facelets — 全部处于 quizWizard    目录:
  • 头部(/quizWizard/heading.xhtml)
  • 问题(/quizWizard/question.xhtml)
  • 单选按钮(quizWizard/choices.xhtml)
  • Next、Previous 以及 Finish 按钮(quizWizard/controls.xhtml)
index.xhtml facelet 用 Start the wizard 链接启动这个应用程序,而 done.xhtml facelet 则显示了问题和答案的总结。
对于客户机,就介绍这么多。在服务器上,应用程序有三个 bean,我们接下来将讨论其中的两个。
此应用程序的两个 question beanQuestion bean,如清单 1 所示,实际上是一个问题、一组答案选项以及一个答案:
清单 1. Question bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.clarity;

import java.io.Serializable;

public class Question implements Serializable {
  private static final long serialVersionUID = 1284490087332362658L;

  private String question, answer;
  private String[] choices;
  private boolean answered = false; // next button is enabled when answered is true
   
  public Question(String question, String[] choices) {
    this.question = question;
    this.choices = choices;
  }

  public void setAnswer(String answer) {
    this.answer = answer;
    answered = true;
  }

  public String getAnswer()    { return answer;   }
  public String getQuestion()  { return question; }
  public String[] getChoices() { return choices;  }
  public boolean isAnswered()  { return answered; }

  public void setAnswered(boolean answered) { this.answered = answered; }  
}




此应用程序在 Questions 类内还包含了一个问题数组,如清单 2 所示:
清单 2. Questions bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.clarity;

import java.io.Serializable;

import com.corejsf.util.Messages;

public class Questions implements Serializable {
  private static final long serialVersionUID = -7148843668107920897L;

  private String question;
  private Question[] questions = {      
    new Question(
       Messages.getString("com.clarity.messages", "expandQuestion", null),
       new String[] {
         Messages.getString("com.clarity.messages", "hydrogen", null),
         Messages.getString("com.clarity.messages", "helium", null),
         Messages.getString("com.clarity.messages", "water", null),
         Messages.getString("com.clarity.messages", "asphalt", null)
       }),
        
   new Question(
       Messages.getString("com.clarity.messages", "waterSGQuestion", null),
       new String[] {
         Messages.getString("com.clarity.messages", "onedotoh", null),
         Messages.getString("com.clarity.messages", "twodotoh", null),
         Messages.getString("com.clarity.messages", "onehundred", null),
         Messages.getString("com.clarity.messages", "onethousand", null)
       }),
        
   new Question(
       Messages.getString("com.clarity.messages", "numThermoLawsQuestion", null),
       new String[] {
         Messages.getString("com.clarity.messages", "one", null),
         Messages.getString("com.clarity.messages", "three", null),
         Messages.getString("com.clarity.messages", "five", null),
         Messages.getString("com.clarity.messages", "ten", null)
       }),
        
   new Question(
       Messages.getString("com.clarity.messages", "closestSunQuestion", null),
       new String[] {
         Messages.getString("com.clarity.messages", "venus", null),
         Messages.getString("com.clarity.messages", "mercury", null),
         Messages.getString("com.clarity.messages", "mars", null),
         Messages.getString("com.clarity.messages", "earth", null)
       })         
  };
   
  public int size()                        { return questions.length; }
  public String getQuestion()              { return question; }
  public void setQuestion(String question) { this.question = question; }
  public Question[] getQuestions()         { return questions; }
}




和  均没有什么特别之处 — 它们只是提供给我服务器上的一列问题 — 不过其中有一点值得一提,即我借助编程的方式用 helper 方法从一个资源包拉出字符串。您可以通过  来了解这个方法是如何工作的,而在 Core JavaServer Faces 则可以更深入地阅读到相关信息(参见 )。
以上就是对此应用程序的 bean 的全部介绍了,只有一点需要补充,即 Wizard bean,它充当了此向导的控制器 。它里面的代码是此应用程序内惟一真正有趣的 Java 代码。我在  部分,还会讨论这个 Wizard bean。
您对应用程序内的文件和这些 question bean 有所了解后,我接下来将向您显示我是如何实现这个向导的视图的。
返回列表