如何使用带有POST参数的JSF隐式重定向 [英] How do you use JSF implicit redirection with POST parameters

查看:112
本文介绍了如何使用带有POST参数的JSF隐式重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JSF应用程序中,我有两个页面,分别是list.jsfdetails.jsf,每个页面都有其自己的带有视图范围的控制器.在list.jsf中,我有一个<h:commandLink>,它调用一个动作并传递一个参数:

In my JSF application I have two pages, list.jsf and details.jsf, and each page has its own controller with view scope. In list.jsf I have a <h:commandLink> that calls an action and pass a parameter:

<h:commandLink value="details" action="#{listBean.goToDetails}" >
   <f:param  name="id" value="#{listBean.object.pk}"/></h:commandLink>

这是bean方法:

@ManagedBean
@ViewScoped
public class ListBean {
    public String goToDetails() {
        // some code
        return "details?faces-redirect=true";
    }
}

我像这样读取第二个bean中的参数:

I read the parameter in the second bean like this:

Map<String, String> params = FacesContext.getCurrentInstance()
                .getExternalContext().getRequestParameterMap();
        this.setIdParam(params.get("id"));

运行此代码时,参数未传递给第二个bean实例. 但是,当我将导航更改为forward(不带faces-redirect=true)时,会传递参数,并且可以在details.jsf中看到详细信息,但URL与当前页面不匹配.

When I run this code, the parameter is not passed to the second bean instance. However when I change navigation to forward (without faces-redirect=true), the parameter is passed and I can see the details in details.jsf but the URL doesn't match with the current page.

所以我想做的是使用带有POST参数(f:param)的"jsf隐式重定向"(不是转发).

So what I want to do is to use a "jsf implicit redirection" (not a forward) with POST parameters (f:param).

推荐答案

您无法使用POST进行重定向.

You can't redirect using POST.

使用faces-redirect=true时,您使用的是 HTTP重定向,会发生什么情况是:服务器向浏览器发送 HTTP 302 响应,并带有URL进行重定向,然后该浏览器在该URL上执行GET请求.

When you use faces-redirect=true you are using an HTTP redirect, and what happens is: the server sends a HTTP 302 response to the browser with an URL for redirection, then the browser does a GET request on that URL.

您可以做的是重定向到通过GET发送id参数的URL,如下所示:

What you can do instead is to redirect to an URL sending the id parameter via GET, going something like this:

public void goToDetails(){
    // some code
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext()
    String id = object.getPk().toString();
    ec.redirect(ec.getRequestContextPath() + "/details.jsf?id=" + id);
}

您可能想为此类事情创建util方法,例如 Faces#redirect() " rel ="nofollow"> OmniFaces库.

You may want to create an util method for this sort of thing, like Faces#redirect() of OmniFaces library.

更新:如注释中所述,也可以仅在返回字符串中添加ID:

UPDATE: As noted in the comments, it's also possible to just add the id in the return string:

public String goToDetails(){
    // some code
    String id = object.getPk().toString();
    return "details?faces-redirect=true&id=" + id;
}

这篇关于如何使用带有POST参数的JSF隐式重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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