Tuesday, March 1, 2011

JSF form for container-based authentication

If you have a HTML form with j_security_check
<form method="post" action="j_security_check">
and want to rewrite it as JSF h:form, it's easy to do. You can write
<h:form id="login" onsubmit="document.login.action = j_security_check;">
or better (if you aren't sure about client Id of the form)
<h:form id="login" onsubmit="document.#{p:component('login')}.action='j_security_check';">
p:component is an EL function from the PrimeFaces library calculating client Id of any component. RichFaces has a similar function rich:component.


  1. p:component does a search in component tree, faster and easier way would be;

    <h:form onsubmit="document.#{component.clientId}.action='j_security_check'">

  2. Yes, I thought about it. But ${component.clientId} returns the nearest parent UIComponent. Is it not?

    BTW, this article shows various possibilities to get client Id http://java.dzone.com/articles/jsf-20-clientid-jquery

  3. I meant #{component} with nearest parent, of course. The spec. says "component - the last component pushed on the component stack using UIComponent.pushComponentToEL()".

  4. I think, we can also use something like

    <h:form ...>
    <h:commandButton onclick="document.#{component.clientId}.action='j_security_check';" value="Submit"/>

  5. Hi Oleg, this code piece which is "onsubmit="document.login.action = j_security_check;" not working for me.(exactly im using jsf + spring). i did not understand but it cant find "id=login". also i tried el expression type of this approach, but still it gives me error.

    is "document.getElementById('loginForm').action" much better ?

  6. Yes, document.getElementById('loginForm').action is fine. You need Id of the HTML form.


Note: Only a member of this blog may post a comment.