我對JSF非常陌生,一直試圖讓它在最近一個小時左右工作。JSF堆棧異常或託管bean問題
我正在處理的這個示例程序應該顯示一個包含多種不同形式的網頁。它沒有問題(因爲它至少可以工作,我真的不知道語法的正確性),現在我使用託管bean實現了表單的一些功能。我們的目標是讓頁面顯示用戶在下面輸入的內容以顯示它已經通過,但似乎有某種* * config.xml問題,我無法弄清楚,因爲我在netbeans中創建的項目沒有任何類型的config.xml文件,它只有一個beans.xml文件和一個web.xml文件。我不確定我需要添加到beans.xml文件中的什麼(如果有的話),或者如果我需要自己創建一個新的xml文件,或者它完全不同。
我還應該提到的是,當我運行這個程序,GUI部分運行,我可以輸入的東西到箱子,檢查單選按鈕等,但,當我點擊「註冊」按鈕,沒有任何反應
這是我的文件。
的index.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>
Registration.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>";
}
}
}
你的按鈕既沒有行動,也沒有一個ActionListener。我想你必須綁定一個方法才能在用戶點擊按鈕時運行。 –