f:param does not work with p:commandLink or h:commandLink on query string

前端 未结 2 524
抹茶落季
抹茶落季 2021-02-04 18:24

f:param works great with h:link, but not with p:commandLink or h:commandLink.

For example, I have two pages tes

相关标签:
2条回答
  • 2021-02-04 18:45

    You could do it by concatenating the parameters with & directly at the action attribute:

    <p:commandLink value="link3" action="test_second?faces-redirect=true&id=3"/>
    

    Update 1

    You might also consider to add &includeViewParams=true. This way view parameters of your target navigation will be included automatically.

    0 讨论(0)
  • 2021-02-04 18:56

    You misinterpreted the meaning of those two tags, namely <h:link> and <h:commandLink>, therefore, you also misinterpreted the meaning of <f:param> attached to either of the two. In anycase it is worthwhile to always read the documentation before asking the questions to get more insight.

    <h:link> renders an HTML "a" anchor element. The value of the component is rendered as the anchor text and the outcome of the component is used to determine the target URL rendered in the "href" attribute. Any child UIParameter components are appended to the String to be output as the value of the "href" attribute as query parameters before rendering...

    <h:commandLink> render an HTML "a" anchor element that acts like a form submit button* when clicked ... if the disabled attribute is not present, or its value is false. It renders "#" as the value of the "href" attribute, renders the current value of the component as the link text if it is specified and *renders JavaScript that is functionally equivalent to the following as the value of the "onclick" attribute:

    document.forms['CLIENT_ID']['hiddenFieldName'].value='CLIENT_ID';    
    document.forms['CLIENT_ID']['PARAM1_NAME'].value='PARAM1_VALUE'; 
    document.forms['CLIENT_ID']['PARAM2_NAME'].value='PARAM2_VALUE'; return false;
    document.forms['CLIENT_ID'].submit()" 
    

    where hiddenFieldName is as described above, CLIENT_ID is the clientId of the UICommand component, PARAM_NAME and PARAM_VALUE are the names and values, respectively, of any nested UIParameter children.

    In other words, within <h:link> tag nested <f:param> will end up as a query parameter of the generated URL, while within <h:commandLink> tag nested <f:param> will end up as a request parameter with a given value.

    While the first one is clear, the second one deserves a better elaboration. To understand what it does, consider that if we abstract away from the details <h:commandLink> sends a POST request and attaches all nested <f:param> tags as request parameters. But it is up to you how you will handle them, as navigation is entirely in your hands.

    So, the first option is to set a hardcoded action attribute, which use case is dubious, like in action="second-page", in which way you didn't pass any query parameter at all. What will be done is POSTing to the same view and forwarding to the second without undertaking any action. Quite a dumb action.

    The second option is to specify an action method, like in action="#{bean.action}". In this case you must handle navigation in the provided action method, i.e. return null/void from the method for a postback, or return a navigation case outcome as a string to make a forward to the specified view. As for the request parameters that you passed with <f:param> they will be available with standard JSF means like @ManagedProperty("#{param.name}") on a request-scoped bean, or by calling ExternalContext#getRequestParameterMap() in any-scoped bean, for example, in action method, like in String param = externalContext.getRequestParameterMap().get("name"). So now you have your parameter in action method that you're free to use how you like, just adhere to a set of rules that exist for URLs.

    Two things left worth mentioning. Remember that request parameters passed with calling the command link will be available only within that same request, as you might expect it to survive a faces-redirect=true that basically fires another request. The other option is to specify includeviewparams=true to pass through the paramaters of the current view, if that's desired, as mentioned in the other answer.

    0 讨论(0)
提交回复
热议问题