JSF - Pass a parameter on ajax call - What's wrong on this code?

10,267

Because the value of <h:inputHidden> is only set during update model values phase. This is indeed an unintuitive behaviour which existed for long in JSF. I've ever reported an issue about this, but this was closed as "by design".

There are several ways to fix this, among others the view scope. In your particular case, you can use <f:param> instead of <h:inputHidden>:

<h:commandLink>
    <f:param name="page" value="#{selector.page}" />
    <f:setPropertyActionListener target="#{articlesSelector.order}" value="1" />
    <f:ajax event="click" render=":articlesContent"/>
    <h:graphicImage value="img/arrow_up.png" alt="Arrow Up"/>
</h:commandLink>

It will then be available as request parameter #{param.page} and in your request scoped bean thus be set as @ManagedProperty.

Share:
10,267
markzzz
Author by

markzzz

Updated on June 05, 2022

Comments

  • markzzz
    markzzz almost 2 years

    I need to pass a parameter to a bean when i do an ajax call.

    My bean is this :

    @ManagedBean
    @RequestScoped
    public class Selector {
        @ManagedProperty(value="#{param.page}")
        private String page;
    
        @PostConstruct
        public void init() {
            if(page==null || page.trim().isEmpty()) {
                this.page="homepage";
            }
    
            System.out.println(this.page);
        }
    
        public String getPage() { return page; }
        public void setPage(String page) { this.page=page; }
    }
    

    And, when i do the ajax call, i need (due to the fact that i want to render a different context) the page parameter. So i've done this :

    // in this moment selector.page = articles
    <h:inputHidden value="#{selector.page}" id="page" />
    
    <h:commandLink>
        <f:setPropertyActionListener target="#{articlesSelector.order}" value="1" />
        <f:ajax event="click" render=":articlesContent"/>
        <h:graphicImage value="img/arrow_up.png" alt="Arrow Up"/>
    </h:commandLink>
    

    But, on the Apply request phase, the page still "homepage". It should get the page-parameter from the request, apply it to the Component tree and render the "articles" context. Why doesnt happens?

    Cheers

  • markzzz
    markzzz over 13 years
    P.S. as JSF rescue, don't forget stackoverflow.com/questions/4334424/… and stackoverflow.com/questions/4304763/jsf-problem-with-viewsco‌​pe (maybe you didnt see them hehe)
  • BalusC
    BalusC over 13 years
    Sorry, busy at work. Fighting with friggin' SQL and datetime issues. The problem wasn't immediately obvious in those questions, I couldn't answer in a sec. Will look if I have more time.