1
<h:outputLink value="/4JVA-157292-war/supinfo/auth/mycar.xhtml">Car</h:outputLink>
<h:outputLink value="Log Out" onclick="${loginController.logout()}" />

As you see, If i click the Car link. I will excute the ${loginController.logout()}, but if i delete the Log out. I will go into the car page.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Bubble
  • 327
  • 2
  • 4
  • 13

1 Answers1

4

<h:ouputLink> component is used to render plain a anchor element with its href attribute evaluated as value attribute of <h:outputLink> with the query parameters attached in case the component has <f:param> tags as its children.

<h:ouputLink> is not used to execute server-side actions as you wrongly seemed to expect. You may have confused it with a <h:commandLink> component that is used to trigger a server-side action via a JavaScript call.

If you want to call the action method you must switch to a command comnponent (or its derivative) like <h:commandLink>. Its action attribute should point to the desired action method, like so:

<h:commandLink value="Log out" action="#{loginController.logout}" />

In this way you will be able to execute the bean action method.

Also it is worth noting that since JSF 2.0 there has been an <h:link> component that is useful for handling application-wide navigation. It takes a navigation case outcome in its outcome attribute thus leaving <h:outputLink> component useful for links to external resources.

For more information on the subject read the following answer: When should I use h:outputLink instead of h:commandLink?.


As to the cause of the problem you faced, onclick attribute is rendered as onclick event handler of the generated a element that responds to a click event on the HTML element it is attached to. In case the event is fired a client-side JavaScript code is called (by function call onclick points to or by executing inline code that is contained therein). Do note that onclick runs on the client (in JavaScript context within web browser) while action runs on the server (executes Java code depending on the submit button pressed within web server).

Usage of onclick may be fruitful at least in the following circumstances:

  • in case it is used in conjunction with a command component it may stop form submission to the server if some client-side requirements are not met (i.e. confirm dialog was shown and cancel button was pressed, or client-side validation failed) as soon as the event will be fired before any consecutive events associated with the element (like form submission):

    <h:commandLink value="Delete item" action="#{bean.delete(item)}" onclick="return confirm("Are you sure you want to delete this item?");" ... />
    

    or

    <h:commandLink value="Submit data" action="#{bean.action}" onclick="return inputValid();" ... />
    

    with

    <script type="text/javascript">
        function inputValid() {
            var isInputValid = ...;
            //decide what's valid or not and set variable to true/false accordingly
            return isInputValid;
        }
    </script>
    
  • it may be used to trigger some GUI changes, especially when used in conjunction with non-command components, like in:

    <h:button value="Show dialog" onclick="showDialog(); return false;" ... />
    
Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67