Question

I'm trying to get na few parameters (now it's two, but on the other xhtml probably I'll need more).

On page index.html I have a link, to page Threads.xhtml, with parameters user_id and section_id:

<h:dataTable value="#{Sections.sections}" var="Section">
    <h:column>
        <h:form>
            <h:link value="#{Section.section_label}" outcome="Threads">
                <f:param name="user_id" value="#{LoginIn.user_id}"/>
                <f:param name="section_id" value="#{Section.section_id}"/>
            </h:link>
        </h:form>
    </h:column>
</h:dataTable>

When I click into on of the links, it goes to for ex.:

Project/faces/Threads.xhtml?user_id=5&section_id=2

So it's good :).

Now, on page Threads.xhtml I have a link (one link, not dataTable, as on index.xhtml - to create new section), to page NewThread.xhtml, with parameters user_id and section_id:

<h:form>
    <h:link value="#{msg.create_new_thread}" outcome="NewThread">
        <f:param name="user_id" value="#{param.user_id}"/>
        <f:param name="section_id" value="#{param.section_id}"/>
    </h:link>
</h:form>

When I click into on of the links, it goes to, for ex..:

Project/faces/NewThread.xhtml?user_id=5&section_id=2

So it's also nice :).

Now, I check on NewThread.xhtml page values of user_id and section_id, by:

<h:outputText value="User_id: #{param.user_id}"/><br/>
<h:outputText value="Section_id: #{param.section_id}"/><br/>

And I see on page values:

User_id: 5
Section_id: 2

So ok :). But now, when I'm trying to get these values in Java code NewThread.java, they returns only null:

FacesContext facesContext = FacesContext.getCurrentInstance();
String user_id = (String) facesContext.getExternalContext().getRequestParameterMap().get("user_id");
String section_id= (String) facesContext.getExternalContext().getRequestParameterMap().get("section_id");
//For test:
System.out.println("User_id: " + user_id);
System.out.println("Section_id: " + section_id);

And in console i've got:

User_id: null
Section_id: null

I've also try toString() method, but it didn't help.

Going back to the index.xhtml, when I click on the link to one of the section, ex. second section (by any user, ex. user_id=5), it goes to Threads.xhtml:

Project/faces/Threads.xhtml?user_id=5&section_id=2

And on Threads.xhtml I've got list of Threads of second section (but this part of xhtml code is irrelevant):

<h:dataTable value="#{Threads.threads}" var="Thread">
    <h:column>
        <h:link value="#{Thread.thread_label}" outcome="Threads">
            <f:param name="thread_id" value="#{Thread.thread_id}"/>
        </h:link>
    </h:column>
</h:dataTable>

And on Java code of Threads.java, I've got:

FacesContext facesContext = FacesContext.getCurrentInstance();
String section_id = (String) facesContext.getExternalContext().getRequestParameterMap().get("section_id");
System.out.println("Section_id: " + section_id);

And in console i've got:

Section_id: 2

So WTF? Once it works, but another time it won't.

EDIT:

Sorry, I forgot. I access into NewThread.java, by commandButton in NewThread.xhtml:

<h:commandButton value="#{msg.add_thread}" action="#{NewThread.AddThread}"/>

and it calls AddThread method in NewThread.java; and in try (of try-catch) of AddThread method, there I'm trying to get parameters.

I've also already add into faces-config.xml:

<managed-bean>
    <managed-bean-name>Threads</managed-bean-name>
    <managed-bean-class>forum.Threads</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
    <managed-bean-name>NewThread</managed-bean-name>
    <managed-bean-class>forum.NewThread</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Was it helpful?

Solution

When you click an h:commandButton, a new request is made. The problem is that the parameters are not being passed to this request.

So, if you want your request scoped bean NewThread to get the data from the request parameters, you have to include the parameters in the request made from the h:commandButton too. Try this:

<h:commandButton value="#{msg.add_thread}" action="#{NewThread.AddThread}">
    <f:param name="user_id" value="#{param.user_id}"/>
    <f:param name="section_id" value="#{param.section_id}"/>
</h:commandButton>

As you are using JSF 2, you may find useful to use f:metadata with f:viewParam together with the includeViewParameters=true trick for navigating between pages, check out these questions:

OTHER TIPS

As elias says, when you click on the command button there's a new request (a post) and this won't include the get parameters from the previous request.

An alternative solution is using the mentioned view parameters in combination with the o:form component from OmniFaces, that has an attribute that automatically retains all view parameters as GET parameters:

<o:form includeViewParams="true">

This has the additional advantage that users keep to see those parameters in the URL, so if they copy it or refresh the page by hitting enter in the address bar of their browser, things keep working as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top