没有会话bean的属性的优雅处理 [英] Elegant handling of attributes without a session bean

查看:179
本文介绍了没有会话bean的属性的优雅处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2.0和Richfaces 4开发一个应用程序,它包含许多显示元素的表,当然还有通常的View / Edit / Delete选项。经过一些SO浏览和谷歌搜索后,我决定发布一个问题,因为我找到的答案并没有解决我的问题。

I'm working on an application using JSF 2.0 and Richfaces 4, that consists of many tables that display elements and of course, the usual View/Edit/Delete options. After some SO browsing and Google search I've decided to post a question because the answers I found did not solve my problem.

现在,直接到了这一点,我的应用程序在处理存储在请求bean中的某些属性时遇到问题,并且在某些点上由于连续请求而丢失。

Right now, and going straight to the point, my application is having issues when handling certain attributes that are stored in request beans and, on certain points, are lost due to successive requests.

例如,当我想要时要编辑对象,将对象(f:propertyActionListener)发送到在表单上显示数据的请求bean,然后在该请求结束时将其丢弃。保存时,会创建一个新对象,并将表单上的属性设置为该对象并保存该项而不是更新,因为它没有id(JPA + Hibernate)。

For example, when I want to edit an object, the object is sent (f:propertyActionListener) to a request bean that displays the data on a form, then it is discarded as that request ends. When saving, a new object is created and the attributes on the form are setted to it and the item gets saved instead of updated, since it has no id (JPA + Hibernate).

我调查了很多选项,这是我到目前为止所做的和结果:

I've investigated many options and this is what I've did so far and the results:

f:param + h:link or h :commandLink:对于@ManagedProperty,param为null,我无法在Context上找到它以通过JNDI查找它。

f:param + h:link or h:commandLink: With @ManagedProperty the param is null, and I can't find it on the Context to look it up through JNDI.

f:setPropertyActionListener + h:commandLink + Request Bean: Works ...但是我丢失了一些数据。显示数据的表单有一些有条件渲染的字段,我无法保存该信息,因此如果验证阶段找到无效数据,表单就会混乱。

f:setPropertyActionListener + h:commandLink + Request Bean: Works... but I'm losing some data. The form that displays the data has some conditionally rendered fields and I can't hold that info, so the form is messed if the validation phase finds invalid data.

f:viewParam + h:commandLink +查看Scoped Bean:这里有奇怪的东西。这个不直接工作,因为bean在呈现表单之前似乎被丢弃了,因为表单没有信息呈现,因为bean是干净的。

f:viewParam + h:commandLink + View Scoped Bean: Weird stuff here. This one doesn't directly work because the bean seems to get discarded before rendering the form, because the form is rendered with no information since the bean is clean.

使用会话bean:像魅力一样工作,但我不想为每个表单创建会话bean只是因为我还在学习JSF生命周期的事情,我想以正确的方式做到这一点。

Using a session bean: Works like a charm, but I don't want to make a session bean for every form just because I'm still learning things about the JSF lifecycle, I want to do it the proper way.

如果我想保持Request会话方法,是否有办法存储参数(对象或普通字符串)并稍后在请求bean上获取?。

If I want to keep the Request session approach, is there a way to store a parameter (either an object or a plain string) and obtain later on a request bean?.

Dunno如果这有帮助但我通过ui使用母版页:insert和ui:define。

Dunno if this helps but I'm using a master page through ui:insert and ui:define.

推荐答案

使用视图范围的bean。它应该工作。您在此处描述的问题表明您将其绑定到JSTL标记或 id 绑定属性。您不应该在视图范围的bean上执行此操作。另请参见 @ViewScoped 标记处理程序失败。另一个可能的原因是你使用CDI的 @Named 来管理bean而不是JSF的 @ManagedBean 。这也可以解释为什么 @ManagedProperty 在你的一次尝试中不起作用,因为它还要求bean由JSF的 @ManagedBean管理

Use a view scoped bean. It should work. The problems which you describe there suggests that you're binding it to JSTL tags or id or binding attributes. You should not do that on a view scoped bean. See also @ViewScoped fails in tag handlers. Another possible cause is that you're using CDI's @Named to manage the bean instead of JSF's @ManagedBean. That would also explain why @ManagedProperty doesn't work in one of your attempts as it also requires the bean to be managed by JSF's @ManagedBean.

关于主 - 详细信息页面方法,请使用< h:link> 在表格页面中使用< f:param> 创建母版页中的查看/编辑链接。

As to the master-detail page approach, use a <h:link> with <f:param> in the table page to create view/edit links in the master page.

EG user / list.xhtml

<h:dataTable value="#{userList.users}" var="user">
    <h:column>#{user.id}</h:column>
    <h:column>#{user.name}</h:column>
    <h:column>
        <h:link value="Edit" outcome="edit">
            <f:param name="id" value="#{user.id}" />
        </h:link>
    </h:column>
</h:dataTable>

bean可以只是请求作用域。

The bean can be just request scoped.

然后,在defail页面(在本例中为编辑页面)中,使用< f:viewParam> 来转换,验证并设置 id as User

Then, in the defail page, which is in this case an edit page, use <f:viewParam> to convert, validate and set the id as User.

例如 user / edit.xhtml

<f:metadata>
    <f:viewParam name="id" value="#{userEdit.user}"
        converter="#{userConverter}" converterMessage="Bad request. Unknown user."
        required="true" requiredMessage="Bad request. Please use a link from within the system." />
</f:metadata>

<h:messages />
<h:link value="Back to all users" outcome="users" />

<h:form id="user" rendered="#{not empty userEdit.user}">
    <h:inputText value="#{userEdit.user.name}" required="true" />
    ...

    <h:commandButton value="Save" action="#{userEdit.save}">
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
</h:form>

使用 @ViewScoped bean来保存数据,服务和行动方法:

Use a @ViewScoped bean to hold the data, service and action methods:

@ManagedBean
@ViewScoped
public class UserEdit {

    private User user;

    @EJB
    private UserService service;

    public String save() {
        service.save(user);
        return "users";
    }

    // Getter+setter.
}

这篇关于没有会话bean的属性的优雅处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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