【问题标题】:Input value doesn't clear after validation failure验证失败后输入值不清除
【发布时间】:2014-11-25 04:26:42
【问题描述】:

我搜索了很多,但没有找到解决方案。

我正在使用 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>

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

}

如果其中一个(比如密码)未填写,则验证失败并显示一条消息(应该如此)。

现在我删除用户名并输入密码。 验证失败,因为用户名为空。 它清除密码字段并显示用户名消息。

现在错误发生了:之前输入的用户名再次出现! 如何防止这种行为?

我知道在流程验证之后,更新模型值调用应用程序被跳过,渲染响应 被执行。 As described here. render response 获取存储在 ui 组件中的值(在 apply requests 处)并使用它们重新渲染而不是删除无效值。

我试过this solutionthis idea。两种方式都找不到我的组件。

非常感谢任何帮助或想法。

您好, 凯文

【问题讨论】:

  • 对我来说这听起来像是浏览器缓存了页面
  • Valar Morghulis!我已经禁用了here 中提到的缓存。我们的开发环境无论如何都不允许浏览器缓存页面。所以这不是原因。还是谢谢!

标签: validation jsf jsf-2


【解决方案1】:

如果您只是从输入字段中删除用户名,则该值应作为empty string 提交,这将是用户名的合法替换,并且也与定义的模式匹配。

我只能假设你已经配置了

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

在您的 web.xml 中。因此,当您尝试提交一个空字段时,它会被解释为null,这与您在用户名属性上的@NotNull 注释不匹配。因此,该值的设置被取消,它将保持其先前的状态,因为它是 SessionScoped - 这是曾经输入过的用户名。

至少这是我可以想象的唯一szenario在提交空字符串时不更新(字符串)值。

将此值设置为true 允许重现您所描述的行为。


您可能会问,为什么这不适用于您在擦除密码时输入密码的文本框?由于您使用的是secret="true",因此文本框不会显示有关输入的字符串的任何信息(即长度),即使支持 bean 属性 not 设置为 null(删除密码后)并且仍然包含错误的密码。

但请注意,简单地删除此属性可能会破坏其他开发人员依赖于获取 null 而不是 empty string 的功能。所以你也许应该考虑删除@NotNull 注释并手动检查null

【讨论】:

  • 感谢您的回答。它肯定把我推向了正确的方向。看来我必须维护 &lt;context-param&gt; 但现在我知道 为什么 它的行为就像它一样。在将用户名值设置为"" 时,有什么方法可以维护 bean 验证?因为由于密码验证,它不会输入我的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
相关资源
最近更新 更多