environment OS : macOS Hight Sierra Version 10.13.2 Eclipse : Neon.3 Release (4.6.3) Java : JDK8 GlassFish : 4.1.2
inputPassword.xhtml(screen)
<abridgement>
<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" />
<f:attribute name="target" value="password" />
</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 />
<f:validator validatorId="passwordValidator" />
<f:attribute name="target" value="rePassword" />
<f:attribute name="passwordId" value="password" />
</h:inputSecret>
<h:message for="rePassword" errorClass="error" />
<abridgement>
PasswordValidator.java(Custom validator)
<abridgement>
@FacesValidator(value = "passwordValidator")
public class PasswordValidator implements Validator {
/**Character strings that should not be used for passwords. */
private static final String KINSHI = "password";
/**The name of the name attribute that specifies the check target. */
private static final String TARGET_KEY = "target";
/**The value of the name attribute specified when checking the password. */
private static final String TARGET_PASSWORD = "password";
/**The value of the name attribute specified when checking the confirmation password. */
private static final String TARGET_RE_PASSWORD = "rePassword";
/**ID of password input field. */
private static final String PASSWORD_ID_KEY = "passwordId";
/*
* (non-Javadoc)
* @see javax.faces.validator.Validator#validate(javax.faces.context.
* FacesContext, javax.faces.component.UIComponent, java.lang.Object)
*/
@Override
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
String target = (String) component.getAttributes().get(TARGET_KEY);
if (TARGET_PASSWORD.equals(target)) {
validatePassword(value);
}
if (TARGET_RE_PASSWORD.equals(target)) {
validateRePassword(component, value);
}
}
/**
*Validate the password entered.
* @param value input value.
*/
private void validatePassword(Object value) {
String inputedValue = (String) value;
if (inputedValue.equals(KINSHI)) {
FacesMessage errorMessage = new FacesMessage(KINSHI + "Cannot be used.");
errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(errorMessage);
}
}
/**
*Validate the password re-entered for verification.
* @param component {@link UIComponent}
* @param value input value.
*/
private void validateRePassword(UIComponent component, Object value) {
String rePassword = (String) value;
String passwordId = (String) component.getAttributes().get(PASSWORD_ID_KEY);
if (StringUtils.isNotBlank(passwordId)) {
String password = getInputedPasswordValue(component, passwordId);
if (StringUtils.isNotEmpty(rePassword)) {
if (!rePassword.equals(password)) {
FacesMessage errorMessage = new FacesMessage("It does not match the password you entered.");
errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(errorMessage);
}
}
}
}
/**
*Get the password input value from the id attribute value of the input tag.
* @param component {@link UIComponent}
* @id attribute value of param id input tag.
* @return password input value.
*/
private String getPasswordValueById(UIComponent component, String id) {
HtmlInputSecret inputTextConf = (HtmlInputSecret) component.findComponent(id);
Object submittedValue = inputTextConf.getSubmittedValue();
return submittedValue.toString();
}
}
** JSF Life Cycle **
This time, getSubmittedValue () resulted in a null value when an error occurred in the first validation in 3 of this life cycle.
Details of JSF 2.0 | Kao Terada --Yoshio Terada For the second and subsequent requests (when you fill in the required items and press the button), after the UIView tree is restored (first in the life cycle), the request value application phase (second in the life cycle) ) Applies the input value to the UIComponent. (In this example, HtmlInput # setSubmittedValue () is executed.) This setSubmittedValue () value is fetched by getSubmittedValue () until the validation phase of the input value (third life cycle) is completed. I can. At the end of the input value validation phase, ** setSubmittedValue (null) is executed ** and HtmlInput # setValue (“foo”) is called for the input value, and in the following phases, the input value is HtmlInput # getValue ( ) Will be used to get **.
<No corrections in other parts>
private String getPasswordValueById(UIComponent component, String id) {
HtmlInputSecret inputTextConf = (HtmlInputSecret) component.findComponent(id);
Object submittedValue = inputTextConf.getSubmittedValue();
if (submittedValue == null) {
// getSubmittedValue()When the value becomes null with, getValue()Get the value with
submittedValue = inputTextConf.getValue();
}
return submittedValue.toString();
}
<No corrections in other parts>
-JSF validation-What DEN thinks
Recommended Posts