与Session Scoped支持bean绑定的JSF数据表每次请求都会一次又一次地呈现

时间:2011-08-26 05:47:04

标签: jsf-2 myfaces

我有一个XHTML页面,在提交时会回到自身。
支持bean是会话作用域。在重定向到自身时,页面呈现h:dataable两次并且给出了重复的id错误。我可以在视觉上看到正在呈现两次的表彼此相邻。我告诉我,有一些事情与boot bean具有faces html table的属性有关,并且在返回时它再次创建,但是解决方案是什么。

以下内容大致来自JavaBeat中的tutorial。唯一的区别是我的支持bean是Session Scoped。

我还说明我的问题可能不会清除它,但代码应该:)这里是

支持bean

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.event.ValueChangeEvent;

import com.example.common.web.bean.BaseBean;

@ManagedBean(name = "registrationBean")
@SessionScoped
public class RegisterationViewManagedBean extends BaseBean {


    private String field;
    private List<Employee> employees;
    private Employee employee;
    private HtmlDataTable htmlDataTable;
    private List<Employee> list = new ArrayList();

    public void setSelected(ValueChangeEvent event) {
        employee = (Employee) htmlDataTable.getRowData();
        list = new ArrayList<Employee>();
        list.add(employee);
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public HtmlDataTable getHtmlDataTable() {
        return htmlDataTable;
    }

    public void setHtmlDataTable(HtmlDataTable htmlDataTable) {
        this.htmlDataTable = htmlDataTable;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String dataSubmit() {
        for (Employee employee : this.list) {
            System.out.println(employee.getCity());
            System.out.println(employee.getName());
            System.out.println(employee.getEmpNo());
        }
        return "success";
    }

    public String submit() {
        this.employees = new ArrayList<Employee>();
        Employee employee1 = new Employee();
        employee1.setCity("Bangalore");
        employee1.setEmpNo("1");
        employee1.setName("Krishna");
        Employee employee2 = new Employee();
        employee2.setCity("Bangalore");
        employee2.setEmpNo("2");
        employee2.setName("ShunmugaRaja");
        Employee employee3 = new Employee();
        employee3.setCity("Bangalore");
        employee3.setEmpNo("3");
        employee3.setName("JoyChristy");
        this.employees.add(employee1);
        this.employees.add(employee2);
        this.employees.add(employee3);
        return "success";
    }
}

Xhtml页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">



    <ui:define name="body">

        <h:form>
            <h:dataTable var="loc" value="#{registrationBean.employees}"
                binding="#{registrationBean.htmlDataTable}">
                <h:column>
                    <h:selectOneRadio onclick="radioButton(this);"
                        valueChangeListener="#{registrationBean.setSelected}">
                        <f:selectItem itemValue="null" />
                    </h:selectOneRadio>
                </h:column>
                <h:column>
                    <h:outputText value="#{loc.name}" />
                </h:column>
                <h:column>
                    <h:outputText value="#{loc.empNo}" />
                </h:column>
                <h:column>
                    <h:outputText value="#{loc.city}" />
                </h:column>
            </h:dataTable>
            <!-- Summary ends -->
            <div class="submitButtons">
                <h:commandButton id="register" action="#{registrationBean.submit}"
                    value="Perform Some Action" />
            </div>
        </h:form>
    </ui:define>
</ui:composition>

堆栈跟踪

Caused by: java.lang.IllegalStateException: component with duplicate id "j_id397249623_10fed7a7:j_id397249623_10fed7bd:j_id397249623_10fed793" found
    at org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy.checkIds(DefaultFaceletsStateManagementStrategy.java:784)
    at org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy.checkIds(DefaultFaceletsStateManagementStrategy.java:800)
    at org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy.checkIds(DefaultFaceletsStateManagementStrategy.java:800)
    at org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy.checkIds(DefaultFaceletsStateManagementStrategy.java:800)
    at org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy.saveView(DefaultFaceletsStateManagementStrategy.java:432)
    at org.apache.myfaces.application.StateManagerImpl.saveView(StateManagerImpl.java:160)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1554)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:281)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59)
    at org.apache.myfaces.tomahawk.application.ResourceViewHandlerWrapper.renderView(ResourceViewHandlerWrapper.java:93)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)

对齐 enter image description here

1 个答案:

答案 0 :(得分:8)

问题是您正在使用会话范围的bean来存储页面绑定。第一次按预期呈现页面。第二次,绑定已经有一个组件,所以所有的子节点都是重复的。更改JSF实现无法解决问题(因为两者都具有从facelets继承的相同代码)。尝试使用两个bean,一个作用域作用,另一个请求作用于作用域。