4

如何使用Primefaces实现j_security_check?通常在JSP中如果要使用JAAS登录,登录形式一般为:

<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>

但是我们如何在 JSF 或 Primefaces 中实现它!

  • 会有什么动作
  • 我们如何摆脱 id 或 name likeformId:componentId
  • Primefacesp:commandButton默认情况下也是 ajaxified,那么它如何以非 ajax 方式提交表单

我需要使用Primefaces实现JAAS表单身份验证,我在这里分享解决方案;它可能对某人很方便。

4

2 回答 2

18

解决方案非常简单。

  • 您需要定义h:formwith prependId="false",这样它就不会生成组件的 id 或名称 as formId:componentId
  • 您需要action="j_security_check"h:formas中定义onsubmit="document.getElementById('login').action='j_security_check';"
  • 设置to的ajax属性,这样表单就不会以 ajax 方式提交。p:commandButtonfalse

就是这样。这是登录表单的完整代码,可以用上述表单代替:

<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>

谢谢。

于 2012-11-29T06:47:24.883 回答
2

有工作(使用 Primefaces 5)代码(从 p:inputText 和 p:password 中删除了名称属性,由 BalusC 部分建议删除):

<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> 
于 2015-01-22T22:24:50.803 回答