在回发之间保留原始的GET请求参数 [英] Retain original GET request parameters across postbacks

查看:143
本文介绍了在回发之间保留原始的GET请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有与@RequestScoped辅助Bean相关联的页面.我从另一个页面通过了参数"project"到达该页面.因此,当我进入正确的页面时,我的网址类似于contextRoot/faces/jsf.xhtml?project=123.

I have simply page to which is associated @RequestScoped backing bean. I get to this page from other page on which one I passed parameter "project". So when I get on right page I have url like contextRoot/faces/jsf.xhtml?project=123.

查看:

<f:metadata>
    <f:viewParam name="project" value="#{entityBean.projectId}" />
</f:metadata>       
...
<p:commandButton value="#{msg['button.add']}"
    actionListener="#{entityBean.addNewEntity((entityName),(entityDescritpion))}"
    ajax="true" update=":projectDetailForm"/>

后备豆:

@Named("entityBean")
@RequestScoped
public class EntityBean implements Serializable{
    private String projectId;

    @PostConstruct
    public void init() {
        params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

        for (Map.Entry<String, String> entry : params.entrySet()) {
            System.out.println(entry.getKey() + " / " + entry.getValue());
        }

        if (params.get("project") != null) {
            projectId = params.get("project");
        } else {
            HttpServletRequest request =
                (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            String projectId = request.getParameter("project");
        }
    }

    //projectId getter and setter
    //public void addNewEntity(String name, String desc) {}
}

首次打开页面时,一切都正常. GET参数已成功处理.但是,由于bean是请求范围的,因此它会在请求结束时销毁,并在随后的回发中重新创建.在这些回发期间,即使GET参数在浏览器地址栏中可见,也不再可用.我尝试了三种获取参数的方法 通过f:viewParamExternalContext甚至从ServletContext开始,但我无法获取这些参数.

Everything works fine when the page is opened for the first time. The GET parameter is successfully processed. However, as the bean is request scoped, it is destroyed by end of request and recreated on the subsequent postback(s). During those postbacks, the GET parameter is not available anymore even though it is visible in the browser address bar. I tried three methods of getting parameter by f:viewParam and ExternalContext and even from ServletContext but I can't get those parameters.

我不想将@RequestScoped更改为@SessionsScoped并且我不能使用@ViewScoped,因为我正在使用CDI bean并且不想将它们混合.

I don't want to change @RequestScoped to @SessionsScoped and I can't use @ViewScoped, beacuse I'm using CDI beans and I don't want to mix them.

推荐答案

您需要 <f:param> . html"rel =" nofollow noreferrer> UICommand 组件可保留后续请求的请求参数.例如

You need the <f:param> in the UICommand component to retain request parameters for the subsequent request. E.g.

<p:commandButton ...>
    <f:param name="project" value="#{param.project}" />
</p:commandButton>

或者,您可以使用JSF实用程序库 <o:form> ="http://omnifaces.org" rel ="nofollow noreferrer"> OmniFaces ,它基本上扩展了<h:form>并带有附加属性includeViewParams,该属性使您可以保留通过<f:viewParam>注册的请求参数随后的请求.

Alternatively, you can use the <o:form> of the JSF utility library OmniFaces, it basically extends the <h:form> with an additional attribute includeViewParams which enables you to retain request parameters registered via <f:viewParam> for the subsequent request.

<o:form includeViewParams="true">
    ...
</o:form>

如果您有多个命令按钮/链接和ajax操作,最终可能会更容易.

This may end up to be easier if you have multiple command buttons/links and ajax actions.

在您的情况下,浏览器地址栏中的URL不会更改,因为您正在触发ajax请求.但是,您可以在浏览器中通过右键单击查看源代码在生成的HTML输出的<form action>中看到的 actual URL默认情况下不包含当前的GET参数.

The URL in browser address bar is in your case not changed because you're firing an ajax request. But the actual URL, which you can see in <form action> of the generated HTML output via rightclick - View Source in browser, does by default not contain the current GET parameters.

无关与具体问题无关,通过在后构造中手动收集参数,您基本上会忽略<f:viewParam>的有用性和功能.我建议仔细检查以下答案,以学习如何正确使用它们:

Unrelated to the concrete problem, with manually collecting the parameter in postconstruct, you're basically ignoring the usefulness and powers of the <f:viewParam>. I recommend to carefully go through the following answers to learn how to utilize them properly:

  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
  • ViewParam vs @ManagedProperty(value = "#{param.id}")

这篇关于在回发之间保留原始的GET请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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