我开发了一个简单的登录表单,用于我的 JSF + PrimeFaces 页面:
<form action="j_security_check" method="post">
<p:dialog modal="true" header="Login" widgetVar="loginDlg">
<h:panelGrid columns="3" cellpadding="5">
<h:outputLabel for="j_username">Username:</h:outputLabel>
<h:inputText id="j_username" required="true" />
<h:message for="j_username" />
<h:outputLabel for="j_password">Password:</h:outputLabel>
<h:inputSecret id="j_password" required="true" />
<h:message for="j_password" />
<br />
<h:commandButton value="Login" />
</h:panelGrid>
</p:dialog>
</form>
尝试使用空密码,但丢失的密码(必需的)未被捕获h:message
成分。我也切换到了p:commandButton
认为问题可能出在按钮的 Ajax 行为中,但页面未呈现,因为 PrimeFaces 抱怨 CommandButton 不在表单元素内。容器抛出的异常是:
com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Access denied on empty password for user pippo
总而言之,我有两个问题:
- 为什么在提交表单之前缺少密码不会产生消息?
- 如何捕获 LoginException 并在对话框中显示错误消息?
The j_security_check
请求由 Web 容器处理,而不是由 JSF 处理。这解释了required="true"
行不通的。仅当您使用 JSF 时才有效<h:form>
和编程登录HttpServletRequest#login()
在与命令按钮关联的操作方法中。
你能做的最好的事情就是配置一个<form-error-page>
in web.xml
指向与<form-login-page>
。然后您可以检查请求是否已转发j_security_check
本身,这意味着发生了登录错误。
<h:panelGroup rendered="#{requestScope['javax.servlet.forward.servlet_path'] == '/j_security_check'}">
<h:outputText value="Unknown login, please try again" styleClass="error" />
</h:panelGroup>
使用这个代替<h:message>
.
至于为什么<p:commandButton>
抱怨没有表格只是因为你没有使用<h:form>
.
无关针对具体问题,<form>
(or <h:form>
每当您决定切换到编程登录时)最好放在正文中<p:dialog>
,不在外面。这<p:dialog>
可以通过 JS 重新定位到正文的末尾,这将导致它不再处于表单中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)