我想在错误的字段旁边显示错误。我有我的页面的代码:
<!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>
如果某个字段为空,我按下Register
按钮,我就知道了(这里所有字段都是空的):
(抱歉,错误是法语。我不知道它是来自我的eclipse语言还是我的tomcat服务器)
我没写这个!这是我的程序以某种方式自行编写的文本...如何删除它?
此外,这是一个注册页面,我想向我的数据库添加用户(如果字段正确),然后使用操作login
更改页面。如何在更改页面之前调用方法来执行此操作?
如果你需要查看更多代码,比如验证器类,我可以添加它。
答案 0 :(得分:4)
这应该有用。
<h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="Please enter your Lastname">
您验证的字段可以添加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;
}
}
和xhtml
<h:message for="lastName"/>
<h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="Please enter your Lastname">
多数民众赞成。
答案 1 :(得分:1)
我这样管理(个人):
<!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" requiredMessage="The field is empty" validatorMessage="String is not valid (only characters a-z A-Z)">
<f:validateRegex pattern="[a-zA-Z]+"/>
</h:inputText>
<h:message for="firstName" errorStyle="color:red; display:block"/>
<h:outputText value="Lastname"/>
<h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="The field is empty" validatorMessage="String is not valid (only characters a-z A-Z)">
<f:validateRegex pattern="[a-zA-Z]+"/>
</h:inputText>
<h:message for="lastName" errorStyle="color:red; display:block"/>
<h:outputText value="Login"/>
<h:inputText id="login" value="#{account.login}" required="true" requiredMessage="The field is empty" validatorMessage="String is not valid (only characters a-z A-Z 0-9)">
<f:validateRegex pattern="[a-zA-Z0-9]+"/>
</h:inputText>
<h:message for="login" errorStyle="color:red; display:block"/>
<h:outputText value="Password"/>
<h:inputSecret id="password1" value="#{account.password1}" required="true" requiredMessage="Enter a password">
</h:inputSecret>
<h:message for="password1" errorStyle="color:red; display:block"/>
<h:outputText value=""/>
<h:inputSecret id="password2" value="#{account.password2}"/>
<h:outputText value=""/>
<h:outputText value="Address"/>
<h:inputText id="address" value="#{account.address}" required="true" requiredMessage="The field is empty" validatorMessage="Wrong address (eg.: 42bis My address)">
<f:validateRegex pattern="[a-zA-Z0-9]+ [a-zA-Z ]+"/>
</h:inputText>
<h:message for="address" errorStyle="color:red; display:block"/>
<h:outputText value="Email"/>
<h:inputText id="email" value="#{account.email}" required="true" requiredMessage="The field is empty" validatorMessage="The email is not valid">
<f:validateRegex pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"/>
</h:inputText>
<h:message for="email" errorStyle="color:red; display:block"/>
</h:panelGrid>
<h:commandButton value="Register" action="#{account.action}"/>
<h:messages globalOnly="true"/>
</h:form>
</ui:define>
</ui:composition>
</html>