【问题标题】:List Data from Java-EE bean file is not showing up in JSF XHTML DataTable elementJava-EE bean 文件中的列表数据未显示在 JSF XHTML DataTable 元素中
【发布时间】:2014-07-29 10:23:36
【问题描述】:

以下是当前运行多少应用程序的步骤:

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


}

使用表单页面上的提交按钮调用上述类的方法:

<h:commandButton id = "Yes" value = "Yes" action = "#{voterData.saveRegistration}"/><br/>

4.在将第一个 bean 的信息保存到第二个 bean 的 List 变量中后,用户将被带到一个 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 页面上。它只显示列名称但没有数据。

我不确定我做错了什么。我是否正确添加了信息?我是不是读错了信息?

【问题讨论】:

  • 请注意,VoterBean 只是 POJO(带有字段 +getters/setters 的简单 java 类),不应将其视为(注释)为托管 bean .去掉VoterBean类上面的两个注解。
  • 您是否尝试使用通过`private VoterBean voter = new VoterBean();` 创建的VoterBean?这不是托管 bean,它不能在您的页面上使用?

标签: html jsf jakarta-ee xhtml


【解决方案1】:

我想你想做这样的事情:

<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 的类型。

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    相关资源
    最近更新 更多