在不使用GET参数的情况下在@ViewScoped bean之间传递对象 [英] Pass an object between @ViewScoped beans without using GET params

查看:88
本文介绍了在不使用GET参数的情况下在@ViewScoped bean之间传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个browse.xhtml,在其中浏览cars的列表,并且当按下查看更多"按钮时,我想在details.xhtml中查看汽车的详细信息.它们的支持bean是@ViewScoped,分别称为BrowseBeanDetailsBean.

I have a browse.xhtml where I browse a list of cars and I want to view the details of the car in details.xhtml when a "View more" button is pressed. Their backing beans are @ViewScoped and are called BrowseBean and DetailsBean, respectively.

现在,我不希望用户/客户端在URL中看到汽车ID,所以我想避免使用GET参数,如此处.

Now, I wouldn't like the user/client to see the car ID in the URL, so I would like to avoid using GET params, as presented here and here.

有什么办法可以做到这一点?我将Mojarra 2.2.8与PrimeFaces 5和OmniFaces 1.8.1结合使用.

Is there any way to achieve this? I'm using Mojarra 2.2.8 with PrimeFaces 5 and OmniFaces 1.8.1.

推荐答案

取决于您是发送重定向还是仅进行导航.

Depends on whether you're sending a redirect or merely navigating.

如果您要发送重定向,则将其放入Flash范围:

If you're sending a redirect, then put it in the flash scope:

Faces.setFlashAttribute("car", car);

在下一个bean的@PostConstruct中可用:

This is available in the @PostConstruct of the next bean as:

Car car = Faces.getFlashAttribute("car");

或者,如果您只是在导航,则将其放在请求范围内:

Or, if you're merely navigating, then put it in the request scope:

Faces.setRequestAttribute("car", car);

在下一个bean的@PostConstruct中可用:

This is available in the @PostConstruct of the next bean as:

Car car = Faces.getRequestAttribute("car");

另请参见:

  • 将一个视图范围的bean注入另一个视图范围的bean会导致它被重新创建
  • 如何在不编写转换器的情况下将对象从JSF的一页传递到另一页
  • See also:

    • Injecting one view scoped bean in another view scoped bean causes it to be recreated
    • How to pass objects from one page to another page in JSF without writing a converter
    • 请注意,我假设您非常了解以下设计选择:拥有两个完全独立的视图,而没有另一个视图就无法存在(等幂).具有条件渲染内容的单个视图.而且您已经知道在实际上被幂等请求时(即通过书签,共享链接,搜索机器人等),该视图应如何表现.如果没有,那么我强烈建议您仔细阅读以下问题的答案:如何在JSF中导航?如何使URL反映当前页面(而不是上一页).

      Note that I assume that you're very well aware about the design choice of having two entirely separate views which cannot exist (be idempotent) without the other view, instead of having e.g. a single view with conditionally rendered content. And that you already know how exactly the view should behave when it's actually being requested idempotently (i.e. via a bookmark, shared link, by a searchbot, etc). If not, then I strongly recommend to carefully read the answer on this question: How to navigate in JSF? How to make URL reflect current page (and not previous one).

      更新:如果您不使用OmniFaces,请分别使用以下内容:

      Update: in case you're not using OmniFaces, use respectively the following:

      FacesContext.getCurrentInstance().getExternalContext().getFlash().put("car", car);
      

      Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("car");
      

      FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("car", car);
      

      Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car");
      

      这篇关于在不使用GET参数的情况下在@ViewScoped bean之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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