Bean返回对象时的导航 [英] navigation when bean returns objects

查看:151
本文介绍了Bean返回对象时的导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于更改视图中的信息(而不是转到其他.xhtml页面)的入门级问题.我的.xhtml显示了一个数据表(显示了一个人的名单),但是我希望用户能够通过在输入文本中键入某人的名字的开头然后单击命令按钮来缩小显示的信息量.命令按钮和数据表都执行相同的bean方法,但是如果inputtext有数据,则sql使用Like(这使用jdbc).我的表单如下,问题是我得到了无法找到具有from-view-id的匹配导航案例",因为我的托管bean返回的是对象列表,而不是成功"或失败".而且,整个表格似乎效率很低.这似乎是一个很常见的场景-调用托管bean的方法,该方法返回对象而不是导航指令.如何清除错误消息?有更好的方法吗?

Beginner question concerning changing the information in a view (and not going to a different .xhtml page). My .xhtml displays a datatable (which shows a list of people), but I want the user to be able to narrow the amount of info displayed by typing in the start of someones name into an inputtext and then clicking the commandbutton. Both the commandbutton and the datatable execute the same bean method, but if the inputtext has data the sql uses Like (this uses jdbc). My form is below, the problem is that I get an "Unable to find matching navigation case with from-view-id" because my managed bean returns a list of objects, not "success" or "failure". Also, the whole form seems inefficient. This seems like a fairly common scenario - calling managed bean's method that returns objects and not navigation instructions. How do I get rid of the error messages? Is there a better way to do this?

这是代码:

<h:head>
    <h:outputStylesheet library="css" name="table-style.css"  />
</h:head>

<h:body>

    <h1>MavenWeb</h1>

    <h:form>
        <h:panelGrid columns="3">
            Select a customer:

            <h:inputText id="listName" value="#{customer.listName}" 
                size="20" >
            </h:inputText>
            <h:commandButton value="Submit"
                                         action="#customer.getCustomerList()}" />

        </h:panelGrid>



    <h:dataTable value="#{customer.getCustomerList()}" var="c"
            styleClass="order-table"
            headerClass="order-table-header"
            rowClasses="order-table-odd-row,order-table-even-row"
        >

        <h:column>
            <f:facet name="header">
                Customer ID
            </f:facet>
                #{c.customerID}
        </h:column>

        <h:column>
            <f:facet name="header">
                Name
            </f:facet>
                #{c.name}
        </h:column>

        <h:column>
            <f:facet name="header">
                Address
            </f:facet>
                #{c.address}
        </h:column>

        <h:column>
            <f:facet name="header">
                Created Date
            </f:facet>
                #{c.created_date}
        </h:column>

    </h:dataTable>
</h:form>
</h:body>

推荐答案

首先,请确保您的托管bean具有ViewScope,因此在同一视图中的每个请求都将仅影响表单值.其次,可以将ajax行为添加到<h:commandButton>并使用新值呈现<h:datatable>.第三,从不将业务逻辑放入属性获取器中,因为JSF框架可以对这些获取器进行2次或多次调用(取决于您的设计).

First, make sure your managed bean has ViewScope, so only the form values will be affected on every request while you're in the same view. Second, you can add ajax behavior to your <h:commandButton> and render the <h:datatable> with the new values. Third, never put business logic inside your attribute getters, because the JSF framework could make 2 or more calls of those getters (depending on your design).

使用这些规则,您可以像这样重新制作代码:

Using these rules, you can remake your code like this:

@ViewScoped
@ManagedBean
public class Customer {
    private String listName;
    private List<CustomerDTO> customerList;

    public Customer() {
        listName = "";
        customerList = null; //maybe you can initialize your list here
    }

    //getters and setters...

    //look that this method returns a void (no need to return a navigation rule)
    public void fillCustomerList() {
        //your method/way to fill the customerList goes here...
        //I'll just put a code example
        customerList = new ArrayList<CustomerDTO>();
        customerList.add(new CustomerDTO(1, "Luiggi Mendoza", "Lima", new Date());
        customerList.add(new CustomerDTO(2, "StackOverflow", "Web", new Date());
    }

}

您页面的代码片段

<h:form>
    <h:panelGrid columns="3">
        Select a customer:
        <h:inputText id="listName" value="#{customer.listName}" size="20" />
        <h:commandButton value="Submit" action="#customer.fillCustomerList}">
            <f:ajax execute="@this" render="dtMyDataTable" />
        </h:commandButton>
    </h:panelGrid>

    <h:dataTable id="dtMyDataTable" value="#{customer.customerList}" var="c"
        styleClass="order-table"
        headerClass="order-table-header"
        rowClasses="order-table-odd-row,order-table-even-row">
        <h:column>
            <f:facet name="header">
                Customer ID
            </f:facet>
            #{c.customerID}
        </h:column>
        <h:column>
            <f:facet name="header">
                Name
            </f:facet>
            #{c.name}
        </h:column>
        <h:column>
            <f:facet name="header">
                Address
            </f:facet>
            #{c.address}
        </h:column>
        <h:column>
            <f:facet name="header">
                Created Date
            </f:facet>
            #{c.created_date}
        </h:column>
     </h:dataTable>
</h:form>

有关此问题的更多信息:

More info on this matter:

  • Does view scope bean survive Navigation JSF
  • Learning JSF2: Ajax in JSF – using f:ajax tag

这篇关于Bean返回对象时的导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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