Handling view parameters in JSF after post
Asked Answered
M

2

12

I have a few pages that needs a userId to work, thus the following code:

userpage.xhtml

<!-- xmlns etc. omitted -->
<html>
<f:metadata>
    <f:viewParam name="userId" value="#{userPageController.userId}"/>
</f:metadata>
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
    <h:form>
        <h:commandButton action="#{userPageController.doAction}" value="post"/>
    </h:form>
</h:body>
</f:view>

userPageController.java

@Named
@ViewScoped
public class userPageControllerimplements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject protected SessionController sessionController;
    @Inject private SecurityContext securityContext;
    @Inject protected UserDAO userDAO;

    protected User user;
    protected Long userId;

    public UserPage() {
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        if(!FacesContext.getCurrentInstance().isPostback()){
            User u = userDAO.find(userId);
            this.userId = userId;
            this.user = u;
        }
    }

    public void doAction(){

    }

}

However, after doAction is called, the view parameter in the url disappears. The bean still works due to its viewscoped nature, but it ruins my attempts of future navigation. When i search around, I get the impression that the view parameter should remain after a post thus reading userpage.jsf?userId=123, but this is not the case. What is really the intended behaviour?

Related to this, I've tried to implement automatic adding of view parameters when navigating to another page where I want to keep the userId. It seems to work for others, but for me, the userId in the ViewRoot is always null. Code below used to retrieve the viewparameter (i know i could use my temporarily stored userId in the bean for navigation, but this solution would be much fancier):

    String name = "userId";
    FacesContext ctx = FacesContext.getCurrentInstance();
    ViewDeclarationLanguage vdl = ctx.getApplication().getViewHandler().getViewDeclarationLanguage(ctx, viewId);
    ViewMetadata viewMetadata = vdl.getViewMetadata(ctx, viewId);
    UIViewRoot viewRoot = viewMetadata.createMetadataView(ctx);
    UIComponent metadataFacet = viewRoot.getFacet(UIViewRoot.METADATA_FACET_NAME);

    // Looking for a view parameter with the specified name
    UIViewParameter viewParam = null;
    for (UIComponent child : metadataFacet.getChildren()) {
        if (child instanceof UIViewParameter) {
            UIViewParameter tempViewParam = (UIViewParameter) child;
            if (name.equals(tempViewParam.getName())) {
                viewParam = tempViewParam;
                break;
            }
        }
    }

    if (viewParam == null) {
        throw new FacesException("Unknown parameter: '" + name + "' for view: " + viewId);
    }

    // Getting the value
    String value = viewParam.getStringValue(ctx);  // This seems to ALWAYS be null.

One last thought is that the setter methods still seem to work, setUserId is called with the correct value on post.

Have I completly missunderstood how view parameters work, or is there some kind of bug here? I think my use case should be extremly common and have basic support in the framework.

Mckellar answered 27/4, 2012 at 14:35 Comment(0)
S
25

When i search around, I get the impression that the view parameter should remain after a post thus reading userpage.jsf?userId=123, but this is not the case. What is really the intended behaviour?

This behaviour is correct. The <h:form> generates a HTML <form> element with an action URL without any view parameters. The POST request just submits to exactly that URL. If you intend to keep the view parameters in the URL, then there are basically 3 ways:

  1. Bring in some ajax magic.

    <h:commandButton action="#{userPageController.doAction}" value="post">
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
    

    This way the initially requested page and thus also the request URL in browser's address bar remains the same all the time.

  2. If applicable (e.g. for page-to-page navigation), make it a GET request and use includeViewParams=true. You can use <h:link> and <h:button> for this:

    <h:button outcome="nextview?includeViewParams=true" value="post" />
    

    However, this has an EL security exploit in Mojarra versions older than 2.1.6. Make sure that you're using Mojarra 2.1.6 or newer. See also issue 2247.

  3. Control the generation of action URL of <h:form> yourself. Provide a custom ViewHandler (just extend ViewHandlerWrapper) wherein you do the job in getActionURL().

    public String getActionURL(FacesContext context, String viewId) {
        String originalActionURL = super.getActionURL(context, viewId);
        String newActionURL = includeViewParamsIfNecessary(context, originalActionURL);
        return newActionURL;
    }
    

    To get it to run, register it in faces-config.xml as follows:

    <application>
        <view-handler>com.example.YourCustomViewHandler</view-handler>
    </application>
    

    This is also what OmniFaces <o:form> is doing. It supports an additional includeViewParams attribute which includes all view parameters in the form's action URL:

    <o:form includeViewParams="true">
    

Update: obtaining the view parameters of the current view programmatically (which is basically your 2nd question) should be done as follows:

Collection<UIViewParameter> viewParams = ViewMetadata.getViewParameters(FacesContext.getCurrentInstance().getViewRoot());

for (UIViewParameter viewParam : viewParams) {
    String name = viewParam.getName();
    Object value = viewParam.getValue();
    // ...
}
Shoddy answered 27/4, 2012 at 15:50 Comment(8)
I think third one is well known options.Hardandfast
I'll probably use 1. to handle the url bar issue. About 2: it would be a clean solution to view parameter handling, but it only works when using the same managed bean for 2 pages (something that i think is normally a bad choice), thus rendering it useless. I'll most likely end up just defining the parameters manually in each link. Do you have any idea of the code at the bottom doesn't work? Getting the view parameter programatically that is.Mckellar
It's because you're creating a brand new view and instead of using the current view. See updated answer.Shoddy
Works like a charm, I should have examined the code I included closer and noticed the "createViewMetadata"... Great answer as always!Mckellar
On which object the includeViewParamsIfNecessary(context, originalActionURL); method is called?Scevor
@DUKE: write yourself :) Just do exactly as method signature suggests. By the way, the <o:form> of the OmniFaces utility library supports an includeViewParams="true" attribute which does exactly this. See also showcase-omnifaces.rhcloud.com/showcase/components/form.xhtmlShoddy
1. Bring in some ajax magic: It doesn't work for me. When I click commandButton and validation fail then I enter valid value click commandButton again. Form is executed and rerendered, but I am not redirected to another page - action is not invoked.Russ
Solved here #14027202Russ
B
0

Am I right that for solution 1 <f:ajax execute="@form" render="@form" /> to work one would also have to include something like

<f:param name="userId" value="#{userPageController.userId}"/>

inside the <h:commandButton>?

Like it is described in https://mcmap.net/q/473834/-jsf-viewparam-required-ajax-breaks-page

Balfore answered 11/11, 2016 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.