제 신청서에 약간의 문제가 있습니다. 필드 암호와 암호가 일치하는지 확인하고 싶습니다. 그래서이 질문에 대한 첫 번째 대답에서와 같이 시도했습니다. Cross field validation with Hibernate Validator (JSR 303)크로스 필드 콩 유효성 검사 - 왜 작동하지 않습니까?
문제는 실제로 작동하지 않으며 IDEA가 없습니다. 왜. 도와주세요! 이것은 여기에 내 첫 게시물, 그래서 제발 나를 위해 가혹한하지 마십시오. JSF
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pl.lodz.p.zsk.ssbd2012.ssbd12.Beans;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.CheckEmail;
import pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch;
import pl.lodz.p.zsk.ssbd2012.ssbd12.entities.Account;
import pl.lodz.p.zsk.ssbd2012.ssbd12.mok.endpoint.MokEndpointLocal;
/**
*
* @author krzys
* @author lukasz
*/
@ManagedBean
@RequestScoped
@FieldMatch(first = "password", second = "password2", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch}")
public class RegisterBean {
/**
* Creates a new instance of RegisterBean
*/
@EJB
MokEndpointLocal endpoint;
@Size(min = 3, max = 16, message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.login.size}")
@Pattern(regexp = "[a-zA-Z0-9]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.login.invalid}")
private String login;
@Size(min = 6, max = 64, message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.password.size}")
@Pattern(regexp = "[a-zA-Z0-9]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.password.invalid}")
private String password;
private String password2;
@Pattern(regexp = "[A-Ż][a-ż]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.name.invalid}")
private String name;
@Pattern(regexp = "[A-Ż][a-ż]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.surname.invalid}")
private String surname;
@CheckEmail
private String email = "";
/**
* @return the login
*/
public String getLogin() {
return login;
}
/**
* @param login the login to set
*/
public void setLogin(String login) {
this.login = login;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String register()
{
// Account account = new Account();
// account.setLogin(login);
// account.setHaslo(password);
// account.setImie(name);
// account.setNazwisko(surname);
// account.setEmail(email);
// endpoint.register(account);
return "registerSucces";
}
/**
* @return the surname
*/
public String getSurname() {
return surname;
}
/**
* @param surname the surname to set
*/
public void setSurname(String surname) {
this.surname = surname;
}
}
그리고 여기 :
여기/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints;
import java.lang.annotation.*;
import javax.validation.Constraint;
import javax.validation.Payload;
/**
*
* @author lukasz
*/
@Documented
@Constraint(validatedBy = FieldMatchValidator.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldMatch {
String message() default "{pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String first();
String second();
}
내 ValidatorClass입니다 :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.beanutils.BeanUtils;
/**
*
* @author lukasz
*/
public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object> {
private String firstFieldName;
private String secondFieldName;
@Override
public void initialize(FieldMatch constraintAnnotation) {
firstFieldName = constraintAnnotation.first();
secondFieldName = constraintAnnotation.second();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
try {
String sFirstField = BeanUtils.getProperty(value, firstFieldName);
String sSecondField = BeanUtils.getProperty(value, secondFieldName);
if(sFirstField.equals(sSecondField)){
return true;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
}
그리고 여기 내 빈의 다음
내 주석의<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<h:outputStylesheet name="menu.css" library="css" />
<title>#{messages.registration}</title>
</h:head>
<h:body>
<ui:composition template="./../resources/mainTemplate.xhtml">
<h4>#{messages.registration}</h4>
<ui:define name="content">
<h4>#{messages.registration}</h4>
<h1>#{messages.registrationInfo1}</h1>
#{messages.registrationInfo2}
<h:form>
<h2>
<h:outputText value="#{messages.loginForm}"/>
<h:inputText id="login" value="#{registerBean.login}" />
</h2>
<h2>
<h:outputText value="#{messages.passwordForm}" />
<h:inputSecret id="password" value="#{registerBean.password}" />
</h2>
<h2>
<h:outputText value="#{messages.repeatPasswordForm}"/>
<h:inputSecret id="confirm" value="#{registerBean.password2}" />
</h2>
<h2>
<h:outputText value="#{messages.nameForm}"/>
<h:inputText id="name" value="#{registerBean.name}" />
</h2>
<h2>
<h:outputText value="#{messages.surnameForm}"/>
<h:inputText id="surname" value="#{registerBean.surname}" />
</h2>
<h2>
<h:outputText value="#{messages.emailForm}"/>
<h:inputText id="email" value="#{registerBean.email}" />
</h2>
<h2>
<h:commandButton value="#{messages.send}" action="#{registerBean.register()}" />
</h2>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
,
를? Bean에 대한 유효성 검증을 실행하려면'Validator'를 작성하고'validate()'메소드를 호출하여 객체를 전달해야합니다. 당신의 프레임 워크가 그것을하지 않는다면, 당신 스스로 그것을해야합니다. –
잘 모르겠지만 주석만으로도 충분하다는 것을 알았습니다. Pattern이나 CheckEmail 같은 다른 주석을 만들었습니다. 틀 렸으면 고쳐줘. – AjMeen