Jsf 跨回发保留原始GET请求参数

Jsf 跨回发保留原始GET请求参数,jsf,jsf-2,Jsf,Jsf 2,我只有一个页面,它与@RequestScoped支持bean相关联。我从传递参数“project”的另一个页面进入此页面。所以当我进入正确的页面时,我会有类似于contextRoot/faces/jsf.xhtml?project=123的url 视图: ... 支持bean: @Named("entityBean") @RequestScoped public class EntityBean implements Serializable{ private String proj

我只有一个页面,它与
@RequestScoped
支持bean相关联。我从传递参数“project”的另一个页面进入此页面。所以当我进入正确的页面时,我会有类似于
contextRoot/faces/jsf.xhtml?project=123
的url

视图:


...
支持bean:

@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) {}
}
@Named(“entityBean”)
@请求范围
公共类EntityBean实现可序列化{
私有字符串投影;
@施工后
公共void init(){
params=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
对于(Map.Entry:params.entrySet()){
System.out.println(entry.getKey()+“/”+entry.getValue());
}
if(params.get(“项目”)!=null){
projectId=params.get(“项目”);
}否则{
HttpServletRequest请求=
(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
字符串projectId=request.getParameter(“项目”);
}
}
//投影吸气剂和塞特
//public void addNewEntity(字符串名称,字符串描述){}
}
第一次打开页面时,一切正常。已成功处理GET参数。然而,由于bean是请求范围的,所以它在请求结束时被销毁,并在随后的回发中重新创建。在这些回发过程中,GET参数不再可用,即使它在浏览器地址栏中可见。我尝试了三种获取参数的方法 通过
f:viewParam
ExternalContext
甚至从
ServletContext
获得这些参数

我不想将
@RequestScoped
更改为
@sessionscoped
,我也不能使用
@ViewScoped
,因为我使用的是CDI bean,我不想混合使用它们。

您需要在组件中保留后续请求的请求参数。例如

<p:commandButton ...>
    <f:param name="project" value="#{param.project}" />
</p:commandButton>
如果您有多个命令按钮/链接和ajax操作,这可能会变得更容易

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


与具体问题无关,在构造后手动收集参数,基本上忽略了
的有用性和功能。我建议仔细阅读以下答案,以了解如何正确使用它们:


将Bean的范围从“请求”更改为“查看”。@kshitij:不幸的是,OP使用CDI而不是JSF来管理Bean。因此,JSF特定的视图范围不起作用。OP在问题的最后一句中也明确提到了它。为了解决这个问题,我使用了ApacheMyFacesCodi。它为CDI范围(如ViewAccessScope)提供了非常有用的扩展。Wokrs可以与JSF2.0和Primefaces配合使用。@AdrianX:也可以。它只在URL中留下丑陋的
windowId
参数。注意,它还将JSF
@ViewScoped
透明地连接到CDI,以便您可以使用
@Named@ViewScoped
<p:commandButton ...>
    <f:param name="project" value="#{param.project}" />
</p:commandButton>
<o:form includeViewParams="true">
    ...
</o:form>