Wav*_*ter 5 validation jsf jsf-2
我搜索了很多,但没有找到解决方案.
我正在使用JSF 2.1和RichFaces 4.2.3,并希望验证用户的登录数据
我有两个输入字段.一个用户名和一个密码,两者都有@NotEmpty
login.xhtml
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:mt="http://java.sun.com/jsf/composite/components">
<h:body>
<ui:composition template="/protected/user/login-template.xhtml">
<ui:define name="panel-navigation">
<ui:include src="/protected/user/login-left-menu.xhtml" />
</ui:define>
<ui:define name="contentbody">
<h:form>
<div id="content">
<div class="subcolumns">
<div class="c65l">
<div class="subcl">
<p class="sum-error-message" />
<fieldset>
<h3>
<h:outputText value="#{styleguideMessages.login}" />
</h3>
<p>
<h:outputText value="#{messages.login_text}" />
</p>
<div class="subcolumns">
<mt:inputText
id="loginName"
value="#{authenticationPM.loginName}"
label="#{messages.general_label_loginname}"
required="true" />
<div class="c33r validation"></div>
</div>
<div class="subcolumns">
<mt:inputText
id="password"
value="#{authenticationPM.password}"
label="#{styleguideMessages.db_password}"
required="true"
secret="true" />
<div class="c33r validation"></div>
</div>
<div class="subcolumns">
<h:commandLink
id="loginButton"
action="#{authenticationPM.doLogin}"
value="#{styleguideMessages.login}"
title="#{styleguideMessages.login}"
styleClass="button last" />
</div>
</fieldset>
</div>
</div>
</div>
</div>
<mt:commandButton
id="login"
action="#{authenticationPM.doLogin}"
value="hidden"
style="visibility:hidden" />
</h:form>
<script
src="#{facesContext.externalContext.requestContextPath}/js/lib/loginEnter.js"
type="text/javascript"
charset="utf-8"></script>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
AuthentificationPM.java
import *;
@Named
@SessionScoped
public class AuthenticationPM extends AbstractPM implements Serializable {
/**
* The user name from the login.
*/
private String userName;
/**
* password.
*/
private String password;
/**
* Returns the login name.
*
* @return String
*/
@NotNull
@Pattern(regexp = "^[^\"/\\[\\]:;|=,+?*<>]*$", message = "{user.loginname.pattern}")
public String getLoginName() {
return userName;
}
/**
* Returns the password.
*
* @return String
*/
@NotNull
public String getPassword() {
return password;
}
/**
* Sets the login name.
*
* @param loginName
* - the login name of the user
*/
public void setLoginName(String loginName) {
this.userName = loginName;
}
/**
* Sets the password.
*
* @param password
* - the password of the user
*/
public void setPassword(String password) {
this.password = password;
}
}
Run Code Online (Sandbox Code Playgroud)
如果未填写其中一个(例如密码),则验证失败并显示消息(应该如此).
现在我删除用户名并输入密码.验证失败,因为用户名为空.它清除密码字段并显示用户名的消息.
现在出现错误:前面输入的用户名重新出现!我该如何防止这种行为?
我知道在进程验证之后,将跳过更新模型值和调用应用程序,并执行渲染响应.如此处所述.所述呈现响应取存储在UI部件(在值应用请求),并使用它们,而不是重新呈现删除无效的值的.
我尝试过这个解决方案以及这个想法.两种方式都找不到我的组件.
非常感谢任何帮助或想法.
问候,凯文
如果您只是从输入字段中删除用户名,则该值应作为 提交empty string
,这将是用户名的合法替换,并且也与定义的模式匹配。
我只能假设,你已经配置了
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
在你的 web.xml 中。因此,当您尝试提交空字段时,它会被解释为,这与您在用户名属性上的注释null
不匹配。@NotNull
因此,该值的设置被取消,它将保持其先前的状态,因为它是SessionScoped
- 这是已经输入过一次的用户名。
至少这是我能想象到的唯一一个当提交空字符串时(字符串-)值不会更新的情况。
将此值设置为true
允许重现您所描述的行为。
您可能会问,为什么这不适用于您在删除密码时输入密码的文本框?由于您使用的secret="true"
文本框不会显示有关输入字符串的任何信息(即长度),即使支持 bean 属性未设置为 null(删除密码后)并且仍然包含错误的密码。
但请注意,简单地删除此属性可能会破坏其他开发人员依赖获取null
而不是empty string
. 因此,您也许应该考虑删除@NotNull
注释并手动检查null
。