environment OS : macOS Hight Sierra Version10.13.2 Eclipse : Version: Neon.3 Release (4.6.3) GlassFish : 4.1.2 JSF : 2.2 JDK : 1.8
inputPassword.xhtml
<abridgement>
<h:head>
<title>input screen</title>
<h:outputStylesheet library="css" name="base.css"/>
</h:head>
<body>
<h:form>
<h:outputLabel>Please enter the password.</h:outputLabel>
<br />
<h:inputSecret id="password" value="#{passwordBean.password}">
<f:validateRequired />
<f:validateLength minimum="3" maximum="10" />
<f:validator validatorId="passwordValidator" />
</h:inputSecret>
<h:message for="password" errorClass="error" />
<br />
<h:outputLabel>Already for confirmation Please enter it once.</h:outputLabel>
<br />
<h:inputSecret id="rePassword" value="#{passwordBean.kakuninPassword}">
<f:validateRequired />
</h:inputSecret>
<h:message for="rePassword" errorClass="error" />
<br />
<h:commandButton value="Send" action="#{passwordBean.onClickSend}" />
</h:form>
</body>
</html>
base.css
@CHARSET "UTF-8";
.error {
color: red;
}
PasswordValidator.java
<abridgement>
/**Custom validator for password entry screen. */
@FacesValidator(value = "passwordValidator")
public class PasswordValidator implements Validator {
private static final String KINSHI = "password";
@Override
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
String inputedValue = (String) value;
if (inputedValue.equals(KINSHI)) {
FacesMessage errorMessage = new FacesMessage(KINSHI + "Cannot be used.");
throw new ValidatorException(errorMessage);
}
}
}
State seen with browser development tools
<label>Please enter the password.</label>
<br>
<input id="j_idt6:password" type="password" name="j_idt6:password" value="">
"password cannot be used.
"
<br>
<label>Already for confirmation Please enter it once.</label>
<br>
<input id="j_idt6:rePassword" type="password" name="j_idt6:rePassword" value="">
<span class="error">Validation error:Requires a value.</span>
PasswordValidator.java
<abridgement:Others are the same as the above code>
FacesMessage errorMessage = new FacesMessage(KINSHI + "Cannot be used.");
errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(errorMessage);
<abridgement:Others are the same as the above code>
State seen with browser development tools
<label>Please enter the password.</label>
<br>
<input id="j_idt6:password" type="password" name="j_idt6:password" value="">
<span class="error">password cannot be used.</span>
<br>
<label>Already for confirmation Please enter it once.</label>
<br>
<input id="j_idt6:rePassword" type="password" name="j_idt6:rePassword" value="">
<span class="error">Validation error:Requires a value.</span>
errorClass attribute: Specifies the CSS class for error messages
-[Display errors / information / warnings on JSF pages --Apache MyFaces --Apache Software Foundation](http://oss.infoscience.co.jp/myfaces/cwiki.apache.org/confluence/display/MYFACES/Displaying+ Errors +-+ Infos +-+ Warnings + in + JSF + pages.html) -Sample custom validator by lab. JSF2
Recommended Posts