CommandButton with parameters

Splash Forums PrettyFaces Users CommandButton with parameters

This topic contains 3 replies, has 2 voices, and was last updated by  Christian Kaltepoth 6 years, 3 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #17987

    emon
    Participant

    Hi,

    I just started playing with PrettyFaces few days ago, but I cant solve one thing.

    How can I use commandButton(or commandLink) to navigate to another page?

    We are using Mojarra 2.0.6, Tomcat 7.0.16, as well as Spring 3.0.5 and have registered the SpringBeanFacesELResolver in the faces-config.xml.

    What I am trying to do is:

    <url-mapping id=”employeeForm”>

    <pattern value=”/employees/#{action}/#{id}/” />

    <view-id value=”/WEB-INF/pages/employees/modify.xhtml” />

    <action onPostback=”false”>#{employeeForm.init}</action>

    </url-mapping>

    <h:commandLink value=”myCommandLink” action=”#{employeeListBean.getEditUrl}” >

    <f:param name=”action” value=”edit” />

    <f:param name=”id” value=”2″ />

    </h:commandLink>

    public String getEditUrl(){

    // … some actions

    return “pretty:employeeForm”;

    }

    But it does not work. The result is:

    com.ocpsoft.pretty.PrettyException: PrettyFaces: Exception occurred while building URL for MappingId < employeeForm >, Required value < #{action} > was null

    Am i doing something wrong? Is it possible to use commandButton like this?

    Thanks

    #21135

    The f:param tags won’t work with a h:commandLink.

    I don’t think that using a h:commandLink is the best way to go in your case. It will create a postback and then redirect the user to the target page. But this postback is dispensable.

    You could use a pretty:link which will simply render a standard HTML link for the page you want to navigate to. Something like this:

    <pretty:link mappingId="employeeForm">
    <f:param name="action" value="edit" />
    <f:param name="id" value="2" />
    <h:outputText value="edit"/>
    </pretty:link>

    I hope this helps :)

    Christian

    #21136

    emon
    Participant

    First, thank you for your quick response. I tried pretty:link and it worked. The problem is that I need execute some actions on server before redirect. The other thing I tried was to use EL-injected path parameters instead of Named path parameters:

    <url-mapping id="employeeForm">

    <pattern value="/employees/#{empList.selectedAction}/#{empList.selectedId}/" />

    <view-id value="/WEB-INF/pages/employees/modify.xhtml" />

    <action onPostback="false">#{employeeForm.init}</action>

    </url-mapping>

    <h:commandButton value="Edit with h:commandButton" action="#{empList.getEditUrl}" immediate="true">

    <f:setPropertyActionListener target="#{empList.selectedAction}" value="edit" />

    <f:setPropertyActionListener target="#{empList.selectedId}" value="#{emp.id}" />

    </h:commandButton>

    and this works together with commandButton.

    So I did little research and I believe that the “problem” is in ExtractedValuesURLBuilder.buildURL. If I understand it right, it uses FacesContext and evaluates EL expression form pattern. But it cannot find #{action} because there is no one. I think if evaluating of #{action} failed it should try to find #{param}. Then it should be possible to use commandButton or commandLink.

    It is just my guess, I did not test it.

    But maybe I complete wrong:)

    Anyway PrettyFaces is great library. Thanks

    #21137

    I’m glad to hear you got it working. :)

    I think you figured it out correctly. When you return something like “pretty:employeeForm” from a JSF action method PrettyFaces has to build the correct URL of the mapping to redirect the user to this URL. Therefore PrettyFaces tries to read the current values of the path and query parameters. In your second example you are using EL-injected query parameters and you populate these values correctly using f:setPropertyActionListener components. So this should work fine.

    If you are using something like “#{action}” in your path parameter, PrettyFaces will translate the path parameter into a query parameter that you read using the standard Servlet API. But if you are using something like “#{empList.selectedAction}”, PrettyFaces will simply inject the value into the referenced bean property. Both ways have their advantages and disadvantages. I myself prefer the latter one, but that’s just my personal opinion! :)

    But I think you are right! Perhaps we should really implement this fallback behavior of reading #{param['action']}. I’ll check if this is easily possible. I currently don’t know if this may cause any side effects. But I’ll check this.

    Thanks for the feedback! We are always happy to receive any kind of suggestions of enhancements! :)

    Christian

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Comments are closed.