package com.carloscortina.paidos.model;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.ScriptAssert;
@ScriptAssert(lang="javascript",
script="_this.confirmPassword.equals(_this.password)",
message = "personal.password.mismatch.message")
public class PersonalRegistryForm {
private Long id;
private String pNombre,sNombre,apellidoP,apellidoM,
username,password,confirmPassword,email,telCel,telefono,categoria;
boolean acceptTerms=true;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the pNombre
*/
public String getpNombre() {
return pNombre;
}
/**
* @param pNombre the pNombre to set
*/
public void setpNombre(String pNombre) {
this.pNombre = pNombre;
}
/**
* @return the sNombre
*/
public String getsNombre() {
return sNombre;
}
/**
* @param sNombre the sNombre to set
*/
public void setsNombre(String sNombre) {
this.sNombre = sNombre;
}
/**
* @return the apellidoP
*/
public String getApellidoP() {
return apellidoP;
}
/**
* @param apellidoP the apellidoP to set
*/
public void setApellidoP(String apellidoP) {
this.apellidoP = apellidoP;
}
/**
* @return the apellidoM
*/
public String getApellidoM() {
return apellidoM;
}
/**
* @param apellidoM the apellidoM to set
*/
public void setApellidoM(String apellidoM) {
this.apellidoM = apellidoM;
}
/**
* @return the username
*/
@NotNull
@Size(min=4,max=12)
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
@NotNull
@Size(min=8,max=12)
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the confirmPassword
*/
@NotNull
public String getConfirmPassword() {
return confirmPassword;
}
/**
* @param confirmPassword the confirmPassword to set
*/
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
/**
* @return the email
*/
@NotNull
@Email
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the telCel
*/
public String getTelCel() {
return telCel;
}
/**
* @param telCel the telCel to set
*/
public void setTelCel(String telCel) {
this.telCel = telCel;
}
/**
* @return the telefono
*/
public String getTelefono() {
return telefono;
}
/**
* @param telefono the telefono to set
*/
public void setTelefono(String telefono) {
this.telefono = telefono;
}
/**
* @return the categoria
*/
public String getCategoria() {
return categoria;
}
/**
* @param categoria the categoria to set
*/
public void setCategoria(String categoria) {
this.categoria = categoria;
}
/**
* @return the acceptTerms
*/
public boolean isAcceptTerms() {
return acceptTerms;
}
/**
* @param acceptTerms the acceptTerms to set
*/
public void setAcceptTerms(boolean acceptTerms) {
this.acceptTerms = acceptTerms;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this);
builder.append("id", id);
builder.append("pNombre", pNombre);
builder.append("sNombre", sNombre);
builder.append("apellidoP", apellidoP);
builder.append("apellidoM", apellidoM);
builder.append("username", username);
builder.append("password", password);
builder.append("confirmPassword", confirmPassword);
builder.append("email", email);
builder.append("telCel", telCel);
builder.append("telefono", telefono);
builder.append("categoria", categoria);
builder.append("acceptTerms", acceptTerms);
return builder.toString();
}
}
控制器
package com.carloscortina.paidos;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.carloscortina.paidos.model.PersonalRegistryForm;
@Controller
@RequestMapping(value="/personal")
public class PersonalController {
private static final Logger logger = LoggerFactory.getLogger(PersonalController.class);
private final static String regForm="personal/RegistryForm";
private final static String regSubmit="redirect:RegistrySubmit";
@RequestMapping(value="new",method=RequestMethod.GET)
public String getPersonalRegistrationForm(Model model){
logger.info("desplegando forma");
PersonalRegistryForm form = new PersonalRegistryForm() ;
model.addAttribute("personalRegForm",form);
return regForm;
}
@RequestMapping(value="",method=RequestMethod.POST)
public String submitPersonalRegistrationForm(@ModelAttribute("personalRegForm") @Valid PersonalRegistryForm form,
BindingResult result){
//convertPasswordError(result);
logger.info("Errores -->"+result.toString());
return (result.hasErrors() ? regForm: regSubmit);
}
@InitBinder
public void initBinder(WebDataBinder binder){
binder.setAllowedFields(new String[]{
"username","password","confirmPassword","pNombre",
"sNombre","apellidoP","apellidoM","email","telefono",
"telCel","categoria","acceptTerms"});
}
}
JSP
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:message var="pageTitle" code="newUserRegistration.pageTitle" />
<spring:message var="allFields" code="newUserRegistration.message.allFieldsRequired" />
<spring:message var="username" code="newUserRegistration.label.username" />
<spring:message var="password" code="newUserRegistration.label.password" />
<spring:message var="confirmPassword" code="newUserRegistration.label.confirmPassword" />
<spring:message var="email" code="newUserRegistration.label.email" />
<spring:message var="firstName" code="newUserRegistration.label.pnombre" />
<spring:message var="secondName" code="newUserRegistration.label.snombre" />
<spring:message var="lastName" code="newUserRegistration.label.apellidop" />
<spring:message var="lastName1" code="newUserRegistration.label.apellidom" />
<spring:message var="telephone" code="newUserRegistration.label.telefono" />
<spring:message var="cellphone" code="newUserRegistration.label.celular" />
<spring:message var="category" code="newUserRegistration.label.categoria" />
<spring:message var="acceptTerms" code="newUserRegistration.label.terms" />
<spring:message var="submit" code="newUserRegistration.label.registrar" />
<head>
<title>${pageTitle}</title>
</head>
<body>
<div>
<form:form action="." modelAttribute="personalRegForm">
<h1>${pageTitle}</h1>
<form:errors path="*">
<div><spring:message code="error.global" /></div>
</form:errors>
<div>
${username}
<form:input path="username"/>
<form:errors path="username">
<div><form:errors path="username" htmlEscape="false" /></div>
</form:errors>
</div>
<div>
${password}
<form:password path="password"/>
<form:errors path="password">
<div><form:errors path="password" htmlEscape="false" /></div>
</form:errors>
</div>
<div>
${confirmPassword}
<form:password path="confirmPassword"/>
</div>
<div>
${email}
<form:input path="email"/>
<form:errors>
<div><form:errors path="email" htmlEscape="false"/></div>
</form:errors>
</div>
<div>
${firstName}
<form:input path="pNombre"/>
</div>
<div>
${secondName}
<form:input path="sNombre"/>
</div>
<div>
${lastName}
<form:input path="ApellidoP"/>
</div>
<div>
${lastName1}
<form:input path="ApellidoM"/>
</div>
<div>
${telephone}
<form:input path="telefono"/>
</div>
<div>
${cellphone}
<form:input path="telCel"/>
</div>
<div>
${category}
<form:input path="categoria"/>
</div>
<div>
<form:checkbox path="acceptTerms"/>
${acceptTerms}
</div>
<div>
<input type="submit" value="${submit}"/>
</div>
</form:form>
</div>
</body>
答案 0 :(得分:1)
似乎是我对验证的条款和功能的误解。 @NotNull和@Email确实都在工作但不像我想象的那样。 @NotNull用于验证Null不是空的,这是我的误解,同样@Email不能对空字符串进行验证。
感谢大家读到这篇文章,我希望这可以帮助别人。它可能太棒了,但它很好学。