我正在尝试使用托管bean在xhtml页面中更改ArrayList
。
当我向表格输入数据时,只有表格中的最后一行将数据输入ArrayList
。这是代码。
<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css"/>
<h:head>
<title>Web TIme : A simple example Title</title>
<meta http-equiv="refresh" content="60"/>
</h:head>
<h:body>
<div class="w3-container w3-light-grey w3-border">
<p>Today : #{webTimeBean.time}</p>
<p>Hello : #{userBean.email}</p>
</div>
<table class="w3-table w3-bordered w3-striped">
<tr>
<th>Game #</th>
<th>Home Team</th>
<th>Away Team</th>
<th>Home Score </th>
<th>Away Score</th>
<th>Bet</th>
</tr>
<ui:repeat var="elem" value="#{userBean.matchList}" >
<tr>
<h:form>
<td>
<h:outputText value="#{elem.matchNumber}" />
</td>
<td>
<h:outputText value="#{elem.homeTeam}" />
</td>
<td>
<h:outputText value="#{elem.awayTeam}" />
</td>
<td>
<h:inputText id="homeS" value="#{elem.homeScore}" size="2">
<f:convertNumber maxFractionDigits="1" type="number" integerOnly="true" />
</h:inputText>
</td>
<td>
<h:inputText id="awayS" value="#{elem.awayScore}" size="2" />
</td>
<td>
<h:commandButton value=" Bet " class="w3-btn-block w3-teal" action="#{userBean.setGameBet(elem.matchNumber , elem.homeScore , elem.awayScore)}" />
</td>
</h:form>
</tr>
</ui:repeat>
</table>
</h:body>
</html>
现在到Java代码:
@ManagedBean( name="userBean" )
@SessionScoped
public class UserBean implements Serializable {
private static List<User> tblScore ;
private String firstName= "First Name ";
private String lastName = "Last Name ";
private String email = "Email";
private String userName = "User Name";
private int totalPoints ;
private String password = "Password";
private static Logger logger = LoggerFactory.getLogger();
private static PrelogInQueries login ;
//private List<Game> temp ;
private List<matchBean> matchList ;
public UserBean()
{
matchList = new ArrayList();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getTotalPoints() {
return totalPoints;
}
public void setTotalPoints(int totalPoints) {
this.totalPoints = totalPoints;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<User> getTblScore() {
return tblScore;
}
public void setTblScore(List<User> tblScore) {
this.tblScore = tblScore;
}
public static PrelogInQueries getLogin() {
return login;
}
public static void setLogin(PrelogInQueries login) {
UserBean.login = login;
}
public List<matchBean> getMatchList() {
return matchList;
}
public void setMatchList(List<matchBean> matchList) {
this.matchList = matchList;
}
public String verifyLogin()
{
logger.log(Logger.LEVEL.INFO, "Insert to verifyLogin with email :" + email );
login = new PrelogInQueries();
boolean verified = false ;
verified = login.verifyMail(email , password ) ;
//userName = login.getUserName(email);
//tblScore = new ArrayList();
try {
tblScore = login.loadTableScore();
//temp = login.getRoundInfo();
for ( int i = 0 ; i<tblScore.size() ; i++ )
{
logger.log(Logger.LEVEL.INFO, "tblScore :" + tblScore.get(i).getUserName() + " " + tblScore.get(i).getTotalPoints() );
}
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(UserBean.class.getName()).log(Level.SEVERE, null, ex);
}
if ( verified ==false )
return "/index.xhtml";
else
{
return "/welcome.xhtml";
}
}
public String nextRound()
{
try {
matchList = login.getRoundInfo2();
printArray ();
logger.log(logging.Logger.LEVEL.INFO, "nextRound ");
} catch (SQLException ex) {
logger.log(Logger.LEVEL.INFO, " catch EXCEPTION ERROR : nextRound() " ) ;
}
return "/nextround.xhtml";
}
public void printArray ()
{
logger.log(logging.Logger.LEVEL.INFO, "printArray ");
for (int i = 0 ; i <matchList.size() ; i ++ )
{
logger.log(logging.Logger.LEVEL.INFO, "USER_BEAN = Game # " +matchList.get(i).getMatchNumber() + " @ "+ matchList.get(i).getHomeTeam() + " vs " + matchList.get(i).getAwayTeam() +" "+ matchList.get(i).getHomeScore() +" "+ matchList.get(i).getAwayScore() ) ;
}
}
public void setGameBet( int gamenumber , int hScore , int aScore )
{
logger.log(logging.Logger.LEVEL.INFO, "*** setGameBet *** " + gamenumber + " = " + hScore + " - " + aScore );
printArray();
boolean t = setBet ( gamenumber , hScore ,aScore);
logger.log(logging.Logger.LEVEL.INFO, "*** Is setGameBet Done secsessfully ?: " + t );
printArray();
}
public boolean setBet( int gamenumber , int home , int away)
{
int indexToUpDate = getGameIndex (gamenumber) ;
if ( indexToUpDate == -1 )
{
return false ;
}
else
{
logger.log(logging.Logger.LEVEL.INFO, "*** Match Number Found !! *** match number: " + gamenumber + " = " + home + " - " + away );
matchBean toUpdate = matchList.get(indexToUpDate);
toUpdate.setHomeScore(home);
toUpdate.setAwayScore(away);
matchList.set(indexToUpDate, toUpdate) ;
return true ;
}
}
public int getGameIndex (int matchNum )
{
for ( int i = 0 ; i <matchList.size() ; i ++ )
{
if ( matchList.get(i).getMatchNumber() == matchNum )
{
logger.log(logging.Logger.LEVEL.INFO, "*** getGameIndex *** i="+ i );
return i ;
}
}
return -1 ;
}
}
注意:在日志中,我可以看到,一旦点击“下注”按钮,我将获得匹配号,但除非我点击最后一行的最后一个按钮,否则分数保持为0。