How to implement j_security_check with Primefaces? Normally in JSP if you want to use JAAS for login, the login form generally is:
<form action="j_security_check" method="POST">
Username:<input type="text" name="j_username"><br>
Password:<input type="password" name="j_password">
<input type="submit" value="Login">
</form>
But how do we implement it in JSF or in Primefaces!
formId:componentId
p:commandButton
is ajaxified in Primefaces by default, so how does it
submit the form in non-ajax wayI had a requirement to implement the JAAS form authentication with Primefaces and I am sharing the solution here; it might come handy to someone.
The solution is pretty straightforward.
h:form
with prependId="false"
, so that it will not generate id or name of the component as formId:componentId
.action="j_security_check"
in the h:form
as onsubmit="document.getElementById('login').action='j_security_check';"
ajax
attribute of the p:commandButton
to false
, so that the form doesn't get submitted in ajax way.That's it. Here is the complete code of the login form which can be replaced by the aforesaid form:
<h:form id="login" onsubmit="document.getElementById('login').action='j_security_check';" prependId="false">
<h:panelGrid columns="2">
<p:outputLabel for="j_username" value="Username" />
<p:inputText id="j_username" name="j_username" />
<p:outputLabel for="j_password" value="Password" />
<p:password id="j_password" name="j_password"/>
<p:commandButton id="submit" value="Login" ajax="false"/>
</h:panelGrid>
</h:form>
Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With