0

我对 JSF 很陌生,并且在过去一个小时左右一直在努力让它工作。

我正在处理的这个示例程序应该显示一个网页,上面有几种不同的形式。它做得很好(因为它至少可以工作,我真的不知道语法有多正确),现在我使用托管 bean 为表单实现了一些功能。目标是让页面显示用户在下面输入的内容以显示它通过了,但似乎有某种* *config.xml 问题我无法弄清楚,因为我在 netbeans 中创建的项目没有有任何类型的 config.xml 文件,它只有一个 beans.xml 文件和一个 web.xml 文件。我不确定我需要向 beans.xml 文件添加什么(如果有的话),或者我是否需要自己创建一个新的 xml 文件,或者它是完全不同的东西。

我还应该提到,当我运行这个程序时,GUI 部分运行,我可以在框中输入内容并检查单选框等,但是当我点击“注册”按钮时,什么也没有发生

这是我的文件。

索引.xhtml

       <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:head>
    <title>Student Registration Form</title>
  </h:head>
  <h:body>
    <h:form>
      <!-- Use h:graphicImage -->
      <h3>Student Registration Form
        <h:graphicImage name="usIcon.gif" library="image"/>
      </h3>

      <!-- Use h:panelGrid -->
      <h:panelGrid columns="6" style="color:green">
        <h:outputLabel value="Last Name"/>
        <h:inputText id="lastNameInputText" 
                     value="#{Registration.lastName}"/>
        <h:outputLabel value="First Name" />
        <h:inputText id="firstNameInputText" 
                     value="#{Registration.firstName}"/>
        <h:outputLabel value="MI" />
        <h:inputText id="miInputText" size="1"
                     value="#{Registration.mi}"/>
      </h:panelGrid>

      <!-- Use radio buttons -->
      <h:panelGrid columns="2">
        <h:outputLabel>Gender  </h:outputLabel>           
        <h:selectOneRadio id="genderSelectOneRadio"
                          value="#{Registration.gender}">
          <f:selectItem itemValue="Male" 
                        itemLabel="Male"/>
          <f:selectItem itemValue="Female" 
                        itemLabel="Female"/>
        </h:selectOneRadio>
      </h:panelGrid>

      <!-- Use combo box and list -->
      <h:panelGrid columns="4">
        <h:outputLabel value="Major  "/>
        <h:selectOneMenu id="majorSelectOneMenu"
                         value="#{Registration.major}">
          <f:selectItem itemValue="Computer Science"/>
          <f:selectItem itemValue="Mathematics"/>
        </h:selectOneMenu> 
        <h:outputLabel value="Minor  "/>
        <h:selectManyListbox id="minorSelectManyListbox"
                             value="#{Registration.minor}">
          <f:selectItem itemValue="Computer Science"/>
          <f:selectItem itemValue="Mathematics"/>
          <f:selectItem itemValue="English"/>
        </h:selectManyListbox> 
      </h:panelGrid>

      <!-- Use check boxes -->
      <h:panelGrid columns="4">
        <h:outputLabel value="Hobby: "/>
        <h:selectManyCheckbox id="hobbySelectManyCheckbox"
                              value="#{Registration.hobby}">
          <f:selectItem itemValue="Tennis"/>
          <f:selectItem itemValue="Golf"/>
          <f:selectItem itemValue="Ping Pong"/>
        </h:selectManyCheckbox>
      </h:panelGrid>

      <!-- Use text area -->
      <h:panelGrid columns="1">
        <h:outputLabel>Remarks:</h:outputLabel>  
        <h:inputTextarea id="remarksInputTextarea" 
                         style="width:400px; height:50px;" 
                         value="#{Registration.remarks}"/>
      </h:panelGrid>

      <!-- Use command button -->
      <h:commandButton value="Register" />
      <br />
      <h:outputText escape="false" style="color:red"
                    value="#{Registration.response}" />
    </h:form>
  </h:body>
</html>

注册.java

    package jsf2demo;

import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean
@javax.faces.bean.RequestScoped

public class Registration {
  private String lastName;
  private String firstName;
  private String mi;
  private String gender;
  private String major;
  private String[] minor;
  private String[] hobby;
  private String remarks;

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getMi() {
    return mi;
  }

  public void setMi(String mi) {
    this.mi = mi;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }

  public String getMajor() {
    return major;
  }

  public void setMajor(String major) {
    this.major = major;
  }

  public String[] getMinor() {
    return minor;
  }

  public void setMinor(String[] minor) {
    this.minor = minor;
  }

  public String[] getHobby() {
    return hobby;
  }

  public void setHobby(String[] hobby) {
    this.hobby = hobby;
  }

  public String getRemarks() {
    return remarks;
  }

  public void setRemarks(String remarks) {
    this.remarks = remarks;
  }

  public String getResponse() {
    if (lastName == null)
      return ""; // Request has not been made
    else {
      String allMinor = "";
      for (String s: minor) {
        allMinor += s + " ";
      }

      String allHobby = "";
      for (String s: hobby) {
        allHobby += s + " ";
      }

      return "<p style=\"color:red\">You entered <br />" + 
         "Last Name: " + lastName + "<br />" + 
         "First Name: " + firstName + "<br />" + 
         "MI: " + mi + "<br />" +
         "Gender: " + gender + "<br />" + 
         "Major: " + major + "<br />" + 
         "Minor: " + allMinor + "<br />" + 
         "Hobby: " + allHobby + "<br />" + 
         "Remarks: " + remarks + "</p>"; 
    }
  }
}
4

2 回答 2

3

One of the goals of the JSF framework is to support "MVC" architecture, that's why, it provides so many fonctionalities and technologies to elaborate the MVC principles. That's why it's not strongly recommanded to mix the view with the controller tiers, so you should separate between these two tiers, because each tier has its ows special role. First, you shouldn't add/modify any other xml files if you're using JSF 2.X version. To illustrate the registration process in your example, you can for example show the registration filled informations in a second simple page named result.xhtml. First replace the method getResponse() by just response() with the content:

public String response(){
    return "result?faces-redirect=true";
}

The outcome string provided through the method above can be written simply return "result"; then, the URI adress in the browser bar will not be changed while forwarding to the destination page. Else, if the string (page's name) is followed by faces-redirect=true like the example, then the URI adress will be changed, and the destination's name will be displayed as well in the browser adress bar.

See also :
Difference between forwarding and redirecting in JSF : http://www.mkyong.com/jsf2/jsf-page-forward-vs-page-redirect/

Then, in index.xhtml, you made the <h:commandButton /> without action necessary attribute in your example, so you should replace the last part there by:

<h:commandButton value="Register" style="color:red" action="#{Registration.response()}" />

And of course, you should also omit the last <h:outputText /> tag in the index.xhtml.

Finally, create the new page result.xhtml to show registration informations:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<head>
    <title>Registration informations</title>
</head>
<body>

    <h2>Your registration informations :</h2>

    <h:outputText value="Last name : #{Registration.lastName }" /><br />
    <h:outputText value="First name : #{Registration.firstName }" /><br />
    <h:outputText value="MI : #{Registration.mi}" /><br />
    ...

</body>
</html>
于 2013-10-11T19:21:19.923 回答
0

尝试改变:

@javax.faces.bean.RequestScoped

@RequestScoped

有关更多信息,您可以查看@BalusC 答案。

于 2013-10-11T18:55:01.057 回答