JSF 2.0查看参数以传递对象 [英] JSF 2.0 view parameters to pass objects

查看:105
本文介绍了JSF 2.0查看参数以传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个对象从一页传递到另一页,而每一页都在不同的视图中.

I am trying to pass an object from one page to another page where each page is in a different view.

在第一页上,我有一个输入文本,其中myBean是一个ViewScoped Bean,而name是一个对象. <h:inputText value="#{myBean.name}"/>

On the first page I have an input text, where myBean is a ViewScoped Bean and name is an object. <h:inputText value="#{myBean.name}"/>

第二页包含
<f:metadata>
<f:viewParam name="userId" value="#{myBean.name}"/>
</f:metadata>

The second page contains,
<f:metadata>
<f:viewParam name="userId" value="#{myBean.name}"/>
</f:metadata>

我得到空转换器"的错误转换错误"设置值mypackage.myBean@257100b'. 我们可以传递String值以外的其他对象来查看参数吗?

I get the error Conversion Error setting value mypackage.myBean@257100b' for 'null Converter'. Can we pass objects other than String values to view parameters?

推荐答案

是的,可以.您只需要提供一个转换器即可在#{myBean.name}之后的对象类型的字符串表示形式与实际对象之间进行转换.字符串表示形式通常是所讨论对象的唯一技术/自然标识符.例如,表的PK.它必须是字符串,仅因为HTTP请求参数只能是字符串.您不能在URL中传递复杂的Java对象. URL只是字符串.

Yes, you can. You just have to supply a converter which converts between the string representation of the object type behind #{myBean.name} and the real object. The string representation is usually the unique technical/natural identifier of the object in question. For example, the table's PK. It has got to be a string, simply because HTTP request parameters can be strings only. You can't pass complex Java objects around in URLs. URLs are just strings.

这是一个开球示例:

例如初始视图中的以下内容:

E.g. the following in the initial view:

<h:link value="Edit" outcome="edit">
    <f:param name="id" value="#{personViewer.person.id}" />
</h:link>

(生成<a href="edit.xhtml?id=123">Edit</a>)

以及链接视图中的以下内容:

and the following in the linked view:

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

使用此转换器

@ManagedBean
@RequestScoped
public class PersonConverter implements Converter {

    @EJB
    private PersonService personService;

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(((Person) value).getId());
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return personService.find(Long.valueOf(value));
    }

}

(过于简单;省略了所有空/数字检查,但您知道了)

更新,根据您的评论,您实际上要触发POST请求.您根本不需要<f:viewParam>.它仅用于设置/验证/转换GET请求参数.尝试序列化整个对象也没有任何意义.只需使用@ManagedProperty.

Update as per the comments, you actually want to fire a POST request. You don't need a <f:viewParam> at all. It's for setting/validating/converting GET request parameters only. Attempting to serialize the whole object makes also no utter sense. Just use @ManagedProperty.

例如

@ManagedBean
@ViewScoped
public class PersonEditor implements Serializable {

    @ManagedProperty("#{personViewer.person}")
    private Person person;

    // ...
}

它不再是可书签的,并且不是SEO友好的(但这就是POST的本质,您可能已经很早就意识到了这一点).注意,#{personViewer} bean本身也必须是@ViewScoped(因此不是@ReqestScoped).您还需要确保您不使用重定向来向后导航,而只是使用向前导航.

It's only not bookmarkable anymore and not SEO-friendly (but that's the nature of POST, you're probably already for long aware of this). Note that the #{personViewer} bean must by itself also be @ViewScoped (and thus not @ReqestScoped). You also need to make sure that you don't navigate back with a redirect, but just a forward.

这篇关于JSF 2.0查看参数以传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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