简体   繁体   English

仅当支持bean在会话作用域上时,JSF页面才加载数据

[英]JSF page only loads data when backing bean is on session scoped

This is my admin_order_search.xhtml page and it has a <p:dataTable> and I'm trying to call a admin_view_order.xhtml page with the particulate order object 这是我的admin_order_search.xhtml页面,它有一个<p:dataTable> ,我正在尝试使用颗粒order对象调用admin_view_order.xhtml页面

<p:column>
   <f:facet name="header">
     <h:outputText value="View" />
   </f:facet>

   <p:commandButton action="admin_view_order?faces-redirect=true" id="view" value="View Info" update="dataTable" actionListener="#{viewOrderController.loadOrder(order)}" />

</p:column>

and this is my "admin_view_order.xhtml" 这是我的“ admin_view_order.xhtml”

<h:form id="orderViewForm">
    <p:growl id="growl" showDetail="true" autoUpdate="true" sticky="false" />           

    <p:outputLabel value="#{viewOrderController.order.orderId}"></p:outputLabel>
    <p:outputLabel value="#{viewOrderController.order.customerName}"> </p:outputLabel>

</h:form>

and hear is my "ViewOrderController.java" page 听到的是我的“ ViewOrderController.java”页面

@Component
@ManagedBean
@ViewScoped
public class ViewOrderController implements Serializable{
    private static final long serialVersionUID = 1L;
    private Order order;

    public void loadOrder(Order order){
        System.out.println("ID : "+order.getOrderId());
        System.out.println("Name : "+order.getCustomerName());
        this.order = order;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}

the problem is admin_view_order.xhtml shows the order details, only if my ViewOrderController is on @SessionScoped but I want it to be on ViewScoped 问题是admin_view_order.xhtml显示订单的详细信息,只有当我ViewOrderController@SessionScoped ,但我希望它是在ViewScoped

Please let me knows how to do that? 请让我知道该怎么做?

Why do you want the Controller to be ViewScoped? 为什么要让Controller成为ViewScope?

After leaving the view (navigate to admin_view_order.xhtml) a new instance of your controller is used. 离开视图后(导航至admin_view_order.xhtml),将使用控制器的新实例。 So the changes you made before (setting the order in loadOrder) are gone. 因此,您之前所做的更改(在loadOrder中设置顺序)已消失。

There is another way to do that , is to passing parameter through the GET request ( for example you can pass the id of the order object in admin_order_search.xhtml to , and then recover this id and use it in your backing bean by searching the object with this id ). 还有另一种方法,就是通过GET请求传递参数(例如,您可以将admin_order_search.xhtml中的order对象的id传递给,然后恢复该ID并通过搜索对象在后备bean中使用它与此ID)。

This is possible by using view parameters 这可以通过使用视图参数来实现

1-pass the id of the object (add the folowing tag inside your button ) 1传递对象的ID(在按钮中添加以下标记)

<f:param name="id" value="#{viewOrderController.order.orderId}" /> 

2-After that, in your admin_view_order.xhtml page, catch the view param and load the info for that order: 2-之后,在您的admin_view_order.xhtml页面中,捕获视图参数并加载该订单的信息:

<f:metadata>
    <f:viewParam name="id" value="#{viewOrderController.newOrderId}" />
    <f:event type="preRenderView" listener="#{viewOrderController.loadOrder}"/>
</f:metadata>

note that i added an attribut ( newOrderId ) to put the order.orderId into it 请注意,我添加了一个属性(newOrderId)将order.orderId放入其中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM