【问题标题】:List from managed bean is not showing in faces datatable来自托管 bean 的列表未显示在 faces 数据表中
【发布时间】:2013-05-19 18:42:31
【问题描述】:

我无法从托管 bean 获取列表以显示在 JSF 的数据表中。

当我调试它时,当它调用bean上的方法时,列表只有一个元素,但页面在数据表中没有显示任何元素。

托管 bean 是:

@ManagedBean
@ViewScoped
public class MyBeanMB {

    private List<MyBean> results = new ArrayList<MyBean>();
    private MyBean myBean = new MyBean();
    @EJB
    private MyBeanService myBeanService;

    public String findMyBeans() {
        results = myBeanService.findMyBeans(myBean);
        myBean = new myBeans();
        if (results != null && !results.isEmpty()) {
            return "success";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No results found"));
        return null;
    }

index.xhtml 页面中的表单如下所示:

<h:form>
            <h:messages />
            <h:outputLabel value="Nombre: " for="nombre"/>
            <h:inputText id="nombre" value="#{myBeanMB.myBean.name}" />
            <h:commandButton value="Buscar" action="#{myBeanMB.findMyBeans}" />

            <h:dataTable id="list" value="#{myBeanMB.results}" var="item">
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Name"/>
                    </f:facet>
                    <h:outputText value= "#{item.name}" />
                </h:column>
            </h:dataTable>
        </h:form>

我错过了什么?

【问题讨论】:

    标签: jsf datatable view-scope


    【解决方案1】:

    当您在托管 bean 中返回 success 时,JSF 将导航到 success.xhtml 视图(假设您没有在 faces-config.xml 中设置导航规则 文件)并且列表应该在这个视图中处理,而不是在 index.xhtml 中。为了修复您的代码,请将您的 findMyBeans 方法更改为返回 void 而不是 String

    public void findMyBeans() {
        results = myBeanService.findMyBeans(myBean);
        myBean = new myBeans();
        if (results != null && !results.isEmpty()) {
            return;
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No results found"));
    }
    

    【讨论】:

    • 我确实有一个导航规则,它返回到 index.xhtml ......这是它应该做的......但是当它返回后返回 index.xhtml 从findMyBeans 和 alough results 有一个包含一个结果的列表,它在数据表中没有显示任何内容......
    • 换句话说,您甚至没有尝试过 Luiggi 的答案吗?当您返回非 null/void 时,将创建一个新视图(由此将重新创建所有视图范围的 bean,包括您存储结果的那个!)
    • 抱歉花了这么长时间才接受答案,但我直到现在才尝试:S
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多