数据表不保留新添加的对象 [英] Datatable don't retain the newly added Object

查看:40
本文介绍了数据表不保留新添加的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是,我的列表已映射到数据表中,我有一个添加按钮,通过单击它,我在数据表中添加了一个空的StudentVO.单击添加按钮后,我会维护一个映射到h:inputHidden的计数器cnt,

what i am trying to do is, my List is mapped in datatable, i have an add button, by clicking on it, i add an empty StudentVO in the datatable. i maintain a counter cnt mapped to h:inputHidden, on click of the add button,

所以我在数据表的最后一行的每一列中得到输入文本,

So i get input text in each column at the last row of the datatable,

但是当我再次单击添加"按钮时,我的计数器增加,但是数据表列表中没有新添加的学生",我得到的两行内容都是空的输入文本

but when i again click the Add button, my counter increments, but the datatable list don't have the newly added Student, what i get i two rows with empty input text

因此,在页面加载时,我得到的最后一行是输入文本,而我期望是两行,其中一行是输入文本,用于保存以前输入的数据,而另一行是空白的输入文本,

so again on the load of the page i get last row with input text whereas i am expecting two rows, one row with input text which holds data entered previosly and a new row with empty input text,

这是下面的代码

public class StudentBeanThree {
    @Resource(name="jdbc/rahul_sample_pool",type=DataSource.class)
    private List<StudentVO> studentList;
    private Integer cnt;

    public List<StudentVO> getStudentList() {
       -----
    }
    @PostConstruct
    public void init(){
       try{
          loadStudents();
       }catch(Exception e){
          e.printStackTrace();
       }
    }

    private void loadStudents() throws Exception{
       //load student list
    }



public String addNewStudent(){
     // This method is called on click of the add button,
     if(getCnt() == null){
          setCnt(0);
     }

    this.cnt = getCnt();
    this.cnt++;
    this.setCnt(cnt);
    if(this.getStudentList() == null){
       loadStudents();
    }
    for(int x=0; x < getCnt().intValue();x++){
        this.getStudentList().add(new StudentVO());
    }   
   }

}

推荐答案

您正在使用action方法而不是bean的(post)构造函数来准备数据模型.对于JSF而言,用提交的值更新数据模型为时已晚.该数据模型应该在应用请求值阶段之前完成,但是在您的情况下,它是在 invoke action 阶段准备的.您需要将数据模型准备工作移至bean的(后)构造函数.

You're preparing the data model in the action method instead of (post)constructor of the bean. It's then too late for JSF to update the data model with the submitted values. The data model is supposed to be finished before the apply request values phase, but in your case it's prepared during invoke action phase. You need to move the data model preparing job to bean's (post)constructor.

根据您的问题历史记录,您正在使用JSF 2.0.我建议一种更简单的方法:将bean放入视图范围.然后就这么简单:

Based on your question history, you're using JSF 2.0. I would suggest a much simpler approach: put the bean in the view scope. It's then as simple as:

<h:form>
    <h:dataTable value="#{students.list}" var="student">
        <h:column><h:inputText value="#{student.name}" /></h:column>
    </h:dataTable>
    <h:commandButton value="Add" action="#{students.add}" />
    <h:commandButton value="Save" action="#{students.save}" />
</h:form>

@ManagedBean
@ViewScoped
public class Students {

    private List<Student> list;

    @EJB
    private StudentService service;

    @PostConstruct
    public void init() {
        list = service.list();
    }

    public void add() {
        list.add(new Student());
    }

    public void save() {
        service.save(list);
    }

    public List<Student> getList() {
        return list;
    }

}

另请参见:

  • @ViewScoped
  • 这篇关于数据表不保留新添加的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆