下面是显示当前有多少应用程序在工作的步骤:
1。用户使用表单注册为投票人。
2。当用户提交表单时,数据被保存到bean中:
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class VoterBean implements Serializable{
private String firstName;
private String lastName;
private String address;
private String city;
private String state;
private String zip;
private String phone;
private String affil;
public VoterBean(){
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getAddress(){
return address;
}
public String getCity(){
return city;
}
public String getState(){
return state;
}
public String getZip(){
return zip;
}
public String getPhone(){
return phone;
}
public String getAffil(){
return affil;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAddress(String address){
this.address = address;
}
public void setCity(String city){
this.city = city;
}
public void setState(String state){
this.state = state;
}
public void setZip(String zip){
this.zip = zip;
}
public void setPhone(String phone){
this.phone = phone;
}
public void setAffil(String affil){
this.affil = affil;
}
}
3。此外,在提交表单时,上述bean中的信息保存到List变量中的另一个bean中:
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import java.util.*;
@Named
@SessionScoped
public class VoterData implements Serializable{
private VoterBean voter = new VoterBean();
private List<VoterBean> voterList = new ArrayList<VoterBean>();
public VoterData(){
}
public List<VoterBean> getVoterList() {
return voterList;
}
public String saveRegistration(){
voterList.add(voter);
return "VoterList";
}
}
上面的类的方法是通过表单页面上的Submit按钮调用的:
<h:commandButton id = "Yes" value = "Yes" action = "#{voterData.saveRegistration}"/><br/>
4。在第一个bean中的信息保存到第二个bean中的List Variable之后,用户将被带到一个JSF XHTML页面,该页面显示了在DataTable中注册的所有当前用户:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Voter Summary</title>
</h:head>
<h:body>
<h:form id = "form" >
<h:outputLabel value = "First Name:"/>
<h:outputLabel value = "#{voterBean.firstName}" />
<br/>
<h:outputLabel value = "Last Name:"/>
<h:outputLabel value = "#{voterBean.lastName}"/>
<br/>
<h:outputLabel value = "Address:"/>
<h:outputLabel value = "#{voterBean.address}"/>
<br/>
<h:outputLabel value = "City:"/>
<h:outputLabel value = "#{voterBean.city}"/>
<br/>
<h:outputLabel value = "State"/>
<h:outputLabel value = "#{voterBean.state}"/>
<br/>
<h:outputLabel value = "Zip:"/>
<h:outputLabel value = "#{voterBean.zip}"/>
<br/>
<h:outputLabel value = "Phone:"/>
<h:outputLabel value = "#{voterBean.phone}"/>
<br/>
<h:outputLabel value = "Affiliation:"/>
<h:outputLabel value = "#{voterBean.affil}"/>
<br/>
Is the following information correct?
<br/>
<h:commandButton id = "Yes" value = "Yes" action = "#{voterData.saveRegistration}"/><br/>
<h:commandButton id = "No" value = "No" action = "Register"/><br/>
</h:form>
</h:body>
</html>
然而问题来了:
即使在我注册了一个选民之后,列表中的数据也没有显示在HTML页面上。它只显示列名而不显示数据。
我不知道我做错了什么…我添加的信息正确吗?我读错信息了吗?
我想你应该这样做:
<h:outputLabel value = "Address:"/>
<h:outputLabel value = "#{voterData.voter.address}"/>
<br/>
<h:outputLabel value = "City:"/>
<h:outputLabel value = "#{voterData.voter.city}"/>
<br/>
<h:outputLabel value = "State"/>
<h:outputLabel value = "#{voterData.voter.state}"/>
<br/>
<h:outputLabel value = "Zip:"/>
<h:outputLabel value = "#{voterData.voter.zip}"/>
<br/>
<h:outputLabel value = "Phone:"/>
<h:outputLabel value = "#{voterData.voter.phone}"/>
<br/>
<h:outputLabel value = "Affiliation:"/>
<h:outputLabel value = "#{voterData.voter.affil}"/>
您正在尝试对原始对象进行更改。您想要访问您的voterData属性voter,它是voterBean的类型。