j_security_check with Primefaces

断了今生、忘了曾经 提交于 2019-12-03 09:22:20

The solution is pretty straightforward.

  • You need to define the h:form with prependId="false", so that it will not generate id or name of the component as formId:componentId.
  • You need to defined the action="j_security_check" in the h:form as onsubmit="document.getElementById('login').action='j_security_check';"
  • Set the 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.

There is working (with Primefaces 5) code (removed name attributes from p:inputText and p:password, removed suggested by BalusC part):

<h:form id="login" onsubmit="action='j_security_check';" prependId="false">
    <h:panelGrid columns="2">
        <p:outputLabel for="j_username" value="Username" />
        <p:inputText id="j_username" />            
        <p:outputLabel for="j_password" value="Password" />
        <p:password id="j_password" />
        <p:commandButton id="submit" value="Login" ajax="false"/>
    </h:panelGrid>
</h:form> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!