简体   繁体   English

JSF请求范围的Bean不显示页面中的数据

[英]JSF Request Scoped Bean not displaying data in page

I'm experimenting with JSF scopes, and have some issues when using a session scoped bean within a request scoped bean. 我正在尝试JSF范围,并且在请求范围的bean中使用会话范围的bean时遇到一些问题。

The basic workflow is that the user would click a View button on an item from the home page, which then calls my code to load & display that item's information. 基本的工作流程是用户单击主页上某个项目上的“查看”按钮,然后调用我的代码以加载和显示该项目的信息。

Here's my UPDATED code: 这是我的更新代码:

@ManagedBean(name="itemViewBean")
@RequestScoped
class ItemViewBean {
    @ManagedProperty("#{itemBean}")
    private ItemBean itemBean;

    private ItemViewObject itemViewObject;
    private int itemId;

    public void init() {
         itemViewObject = itemBean.readItem(itemId);
    }

    public String readItem(int itemId) {
     //     itemViewObject = itemBean.readItem(itemId);  // moved to init();
          return "/pages/viewItem.xhtml?faces-redirect=true";
    }

    public ItemViewObject getItemViewObject() {
          return itemViewObject;
    }

    // getters & setters as needed

}

@ManagedBean(name="itemBean")
@SessionScoped
class ItemBean {
    public ItemViewObject readItem(int itemId) {
        // hardcoded creating a ItemViewObject for now.
        // eventually would be loaded from the database.
        ....
    }
}

My UPDATED view page then has things like: 然后,我的“ 更新的视图”页面具有以下内容:

<!-- added the metadata -->
<f:metadata>
    <f:viewParam name="id" value="#{itemViewBean.itemId}" />
    <f:event listener="#{itemViewBean.init}" type="preRenderView" />
</f:metadata>

<!-- same as before -->
<h:outputText value="#{itemViewBean.itemViewObject.description}" />

If my viewBean is request scoped (or view scoped), I get empty data on the view page. 如果我的viewBean是请求范围(或视图范围)的,则在视图页面上会得到空数据。 If the viewBean is session scoped, things work. 如果viewBean是会话作用域的,则一切正常。 And I'm not understanding why? 我不明白为什么?

From what I see in my debugger, readItem(itemId) is called (from the home page when clicking a view button), but when the view page itself calls the getItemViewObject(), the itemViewObject is null. 根据我在调试器中看到的内容,将调用readItem(itemId)(单击视图按钮时从主页上),但是当视图页面本身调用getItemViewObject()时,itemViewObject为null。

What am I doing wrong? 我究竟做错了什么?

UPDATE I forgot to mention earlier how my home page was calling readItem method, and that was through a command button: UPDATE我忘了前面提到我的主页如何调用readItem方法,而那是通过命令按钮进行的​​:

<h:commandButton class="btn btn-mini firefoxBtnMiniCorrection"
    value="View"
    action="#{itemViewBean.readItem(b.itemId)}"/>

Each item listed in the home page has its own View button. 主页中列出的每个项目都有其自己的“查看”按钮。

Also forgot to mention that both the home page and my view page are using JSF Templates. 还忘了提及主页和我的视图页面都在使用JSF模板。 I don't know if that matters. 我不知道这是否重要。

From the comments below which people have made, I came up with the code changes above. 从下面人们的评论中,我想到了上面的代码更改。 And things work now. 现在一切正常。 Using either request scope or view scope with ItemViewBean works now. 现在可以将请求范围或视图范围与ItemViewBean一起使用。

And I am surprised this worked! 我很惊讶这行得通! I'm not quite certain I fully understand why it works. 我不太确定我是否完全理解它的工作原理。

Are my changes the correct way to do things? 我的更改是做事的正确方法吗? Or is there a better way? 或者,还有更好的方法?

Also, I'm using JSF 2.1. 另外,我正在使用JSF 2.1。

Update 2 It doesn't work. 更新2它不起作用。 The scoping works, but I discovered that the itemId in the viewParam is always null. 范围确定有效,但是我发现viewParam中的itemId始终为null。 Why? 为什么?

itemViewObject is private in a RequestScoped bean. itemViewObjectRequestScoped bean中是私有的。 After readItem() got a value for itemViewObject, this value will be forgotten after this request and will be null at the next request (`@RequestScoped'). readItem()获得readItem()的值之后,该值将在此请求后被忘记,并且在下一个请求(@RequestScoped)时为null。

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

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