-2

I'm trying to call a managed bean from h:commandLink in JSF. But I don't see href attribute in the rendered HTML a tag.

Am I missing something?

There is a ManagedBean called AccountSetupController with a signUp method in it.

This is the tag I used in JSF:

  <h:form prependId="false">
    <h:commandLink  action="#{accountSetupController.signUp()}" 
         value="#{msg['homepage.createaccount']}" styleClass="button large">
    </h:commandLink>
  </h:form>

This is the rendered tag. See there is nothing in href attribute.

 <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt15'),
  {'j_idt33':'j_idt33'},'');return false" 
  class="button large">CREATE MY ACCOUNT</a>

This is the form tag that is generated

    <form id="j_idt15" name="j_idt15" 
    method="post" action="/myproject/faces/homepage/homepage.xhtml" 
    enctype="application/x-www-form-urlencoded">       .... </form>

As you can see, the form action is pointing to some place I don't need.

Am I missing something?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2434
  • 6,339
  • 18
  • 63
  • 87
  • Related: [h:outputLink vs h:commandLink](http://stackoverflow.com/questions/4317684/when-should-i-use-houtputlink-instead-of-hcommandlink/) – BalusC Jul 24 '13 at 12:47

1 Answers1

3

Command links in JSF are rendered that way. The form will be submitted by JSF via JavaScript's onclick method using JSF JS library, while the href will always stay #.

Moreover, you won't find the bound action / action listener method names in browser tools due to understandable reasons. Rather, JSF will find the id of a clicked link on the server and will trigger all of the component's action(listeners).

All in all, reading <h:commandLink> documentation unsurprisingly helps a lot (all emphasis mine):

General behavior: Both the encode and decode behavior require the ability to get the id/name for a hidden field, which may be rendered in markup or which may be programmatically added via client DOM manipulation, whose value is set by the JavaScript form submit (further referred to as hiddenFieldName.

Decode behavior: Obtain the "clientId" property of the component. Obtain the Map from the "requestParameterMap" property of the ExternalContext. Derive hiddenFieldName as above. Get the entry in the Map under the key that is the hiddenFieldName. If the there is no entry, or the entry is the empty String, or the entry is not equal to the value of the "clientId" property, return immediately. If there is an entry, and its value is equal to the value of the "clientId" property, create a new javax.faces.event.ActionEvent instance around the component and call queueActionEvent() on the component, passing the event.

Encode behavior: Render "#" as the value of the "href" attribute. Render the current value of the component as the link text if it is specified. Render JavaScript that is functionally equivalent to the following as the value of the "onclick" attribute: document.forms['CLIENT_ID']['hiddenFieldName'].value='CLIENT_ID'; ocument.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.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67