在不使用GET参数的情况下在@ViewScoped bean之间传递对象

Chr*_*ris 16 jsf parameter-passing omnifaces view-scope jsf-2.2

我有一个browse.xhtml浏览列表的地方cars,我想在details.xhtml按下"查看更多"按钮时查看汽车的详细信息.他们的支持豆分别@ViewScoped被称为BrowseBeanDetailsBean.

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

有没有办法实现这个目标?我正在使用Mojarra 2.2.8和PrimeFaces 5以及OmniFaces 1.8.1.

Bal*_*usC 39

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

如果您要发送重定向,请将其放入闪存范围:

Faces.setFlashAttribute("car", car);
Run Code Online (Sandbox Code Playgroud)

这可以@PostConstruct在下一个bean中使用:

Car car = Faces.getFlashAttribute("car");
Run Code Online (Sandbox Code Playgroud)

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

Faces.setRequestAttribute("car", car);
Run Code Online (Sandbox Code Playgroud)

这可以@PostConstruct在下一个bean中使用:

Car car = Faces.getRequestAttribute("car");
Run Code Online (Sandbox Code Playgroud)

也可以看看:

请注意,我假设您非常了解具有两个完全独立的视图的设计选择,这两个视图在没有其他视图的情况下不能存在(是幂等的),而不是具有例如具有条件渲染内容的单个视图.并且您已经知道视图在实际被请求时(或者通过书签,共享链接,搜索机器人等)的行为应该如何.如果没有,那么我强烈建议仔细阅读这个问题的答案:如何在JSF中导航?如何使URL反映当前页面(而不是之前的页面).


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

FacesContext.getCurrentInstance().getExternalContext().getFlash().put("car", car);
Run Code Online (Sandbox Code Playgroud)
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("car");
Run Code Online (Sandbox Code Playgroud)
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("car", car);
Run Code Online (Sandbox Code Playgroud)
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car");
Run Code Online (Sandbox Code Playgroud)