如何处理 h:inputText 验证?

Elf*_*yer 0 validation jsf

我想在错误的字段旁边显示错误。我的页面有该代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<ui:composition template="/WEB-INF/templates/basic.xhtml">
    <ui:define name="content">
        <h:form>
            <h:panelGrid columns="3">
                <h:outputText value="Firstname"/>
                <h:inputText id="firstName" value="#{account.firstName}" required="true">
                    <f:validator validatorId="stringAlphaValidator"/>
                </h:inputText>
                <h:message for="firstName" errorStyle="color:red; display:block"/>
                <h:outputText value="Lastname"/>
                <h:inputText id="lastName" value="#{account.lastName}" required="true">
                    <f:validator validatorId="stringAlphaValidator"/>
                </h:inputText>
                <h:message for="lastName" errorStyle="color:red; display:block"/>
                <h:outputText value="Login"/>
                <h:inputText id="login" value="#{account.login}" required="true">
                    <f:validator validatorId="stringAlphaValidator"/>
                </h:inputText>
                <h:message for="login" errorStyle="color:red; display:block"/>
                <h:outputText value="Password"/>
                <h:inputText id="password" value="#{account.password}" required="true">
                </h:inputText>
                <h:message for="password" errorStyle="color:red; display:block"/>
                <h:outputText value="Address"/>
                <h:inputText id="address" value="#{account.address}" required="true">
                    <f:validator validatorId="stringAlphaNumericValidator"/>
                </h:inputText>
                <h:message for="address" errorStyle="color:red; display:block"/>
                <h:outputText value="Email"/>
                <h:inputText id="email" value="#{account.email}" required="true">
                    <f:validator validatorId="emailAddressValidator"/>
                </h:inputText>
                <h:message for="email" errorStyle="color:red; display:block"/>
            </h:panelGrid>
            <h:commandButton value="Register"/>
            <h:messages globalOnly="true"/>
        </h:form>
    </ui:define>
</ui:composition>
</html>
Run Code Online (Sandbox Code Playgroud)

如果一个字段是空的,我按下Register按钮,我会得到(这里所有的字段都是空的):

空字段时出错

(抱歉错误是法语,不知道是我的eclipse语言还是我的tomcat服务器)

这不是我写的!这是我的程序以某种方式自行编写的文本......我如何删除它?

此外,这是一个注册页面,我想将用户添加到我的数据库中(如果字段正确),然后使用 action 更改页面login。如何在页面更改之前调用方法来执行此操作?

如果您需要查看更多代码,例如验证器类,我可以添加它。

Pat*_*ick 6

这应该有效。

<h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="Please enter your Lastname">
Run Code Online (Sandbox Code Playgroud)

和你验证的字段,你可以添加一个 validatorMessage="<Message>"

** 更新* 如果您想使用 Bean 验证,如我的评论中所述,请这样做:

// for example your NamesBean
@Named(value = "namesBean")
@SessionScoped
public class NamesBean {

// attributes

public void setFirstName(String firstName) {
 this.firstName = firstName;
}

@Size(min=3, max=10, message="Min 3 and max 10 characters")
public String getFirstname() {
 return firstName;
}

}
Run Code Online (Sandbox Code Playgroud)

和 xhtml

<h:message for="lastName"/>
<h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="Please enter your Lastname">
Run Code Online (Sandbox Code Playgroud)

就是这样。