我对 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>";
}
}
}