How to add request parameter in jsf2?

2019-08-31 06:17发布

In my app, before upgrading to jsf 2, when doing a custom redirect I used to manually put a request parameter with a specific value in external context like this:

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .put(CmwNavigationControllerBean.PARAM_DISPLAY_TARGET_POPUP, "true");

Now this line, throws an exception because it seems that this map is no longer allowed to be modified:

at java.util.Collections$UnmodifiableMap.put(Unknown Source) [rt.jar:1.7.0]

Isn't really no other way to bypass this exception? I'm doing refactoring because of upgrade and I try to keep the changes at minimal level.

2条回答
贪生不怕死
2楼-- · 2019-08-31 07:07

Rather than getRequestParameterMap() (which is read-only) you should invoke getRequestMap() on the ExternalContext.

For example:

FacesContext.getCurrentInstance()
            .getExternalContext()
            .getRequestMap()
            .put(CmwNavigationControllerBean.PARAM_DISPLAY_TARGET_POPUP, "true");
查看更多
一纸荒年 Trace。
3楼-- · 2019-08-31 07:09

You can either use a view parameter or use the flash scope for that. A view parameter is in practice a GET parameter which you can pass when you request the page you want to redirect to. For your case, you should redirect to it with the parameter appended.

Return the navigation case with the parameter appended:

//Will be reflected in browser's address bar as /context/myDestinationView.xhtml?displayTargetPopUp=true
return "myDestinationView?displayTargetPopUp=true&faces-redirect=true&includeViewParams=true";

Catch it from your destination view:

<f:viewParam name="displayTargetPopUp" value="#{displayTargetPopUp}" />

Another way if you want to avoid including it in your GET request, is to use flash scope, which is supposed to be fixed for Mojarra 2.1.27 and 2.2.5 versions. Flash scoped values are designed to support a redirection, while the request ones are not.

See also:

查看更多
登录 后发表回答