JSF 2.0视图参数传递对象

发布于 2024-12-22 16:34:51 字数 436 浏览 1 评论 0原文

我试图将一个对象从一个页面传递到另一个页面,其中每个页面都位于不同的视图中。

在第一页上,我有一个输入文本,其中 myBean 是一个 ViewScoped Bean,名称是一个对象。

第二页包含,



我收到错误“null Converter”的转换错误设置值 mypackage.myBean@257100b'。 我们可以传递除 String 值之外的对象来查看参数吗?

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

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}"/>

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-12-29 16:34:51

是的,你可以。您只需提供一个转换器,用于在 #{myBean.name} 后面的对象类型的字符串表示形式和真实对象之间进行转换。字符串表示通常是相关对象的唯一技术/自然标识符。比如桌子上的PK。它必须是一个字符串,因为 HTTP 请求参数只能是字符串。您无法在 URL 中传递复杂的 Java 对象。 URL 只是字符串。

这是一个启动示例:

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

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

(生成 Edit)< /em>

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

<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 请求。您根本不需要 。它仅用于设置/验证/转换 GET 请求参数。尝试序列化整个对象也没有任何意义。只需使用@ManagedProperty

例如,

@ManagedBean
@ViewScoped
public class PersonEditor implements Serializable {

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

    // ...
}

它只是不再可添加书签并且不利于 SEO(但这就是 POST 的本质,您可能已经意识到这一点了)。请注意,#{personViewer} bean 本身也必须是 @ViewScoped(因此不是 @ReqestScoped)。您还需要确保不会通过重定向向后导航,而只是向前导航。

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.

Here's a kickoff example:

E.g. the following in the initial view:

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

(which generates an <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 />

with this converter

@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));
    }

}

(oversimplified; all null/number checks omitted, but you got the idea)

See also:


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.

E.g.

@ManagedBean
@ViewScoped
public class PersonEditor implements Serializable {

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

    // ...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文