嗨,我正在Spring Roo做项目。我在哪里可以限制电话号码为10位数字?我已经在create.jspx和User_Roo_DbManaged.aj中设置了限制,但是它不起作用。
它接受字母和较短的输入。
create.jspx
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/jquery/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/jquery/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<form:create id="fc_edu_ndnu_capstone_domain_User" modelAttribute="user" path="/users" render="${empty dependencies}" z="wGrfaSchPJDWXlsq1jiCSRc0pAA=">
<field:simple field="emergencies" id="c_edu_ndnu_capstone_domain_User_emergencies" messageCode="entity_reference_not_managed" messageCodeAttribute="Emergency" z="96v/orisk+QZ0UvamFbdCmNUM9g="/>
<field:select field="typeId" id="c_edu_ndnu_capstone_domain_User_typeId" itemValue="id" items="${usertypes}" path="/usertypes" z="cuXEuKQFS8VdksWWbETwOj+Ps4k="/>
<field:input field="name" id="c_edu_ndnu_capstone_domain_User_name" required="true" z="AvB38b/End0hXemXNC8GrbD8SAw="/>
<field:input field="email" id="c_edu_ndnu_capstone_domain_User_email" required="true" validationMessageCode="field_invalid_email" z="OGADRfESfWyp/4F216swLnNdyoQ="/>
<field:input field="username" id="c_edu_ndnu_capstone_domain_User_username" required="true" z="MiEOzhgH466ktY7pKT/tYMLt5S0="/>
<field:input field="password" id="c_edu_ndnu_capstone_domain_User_password" required="true" z="kEW4oQdeXWi+JyIwdUJkmMKHYII=" type="password"/>
<field:input field="phone" id="c_edu_ndnu_capstone_domain_User_phone" required="true" validationMessageCode="field_invalid_number" max="10" min="10" z="k42Av41wIfhbInJmZhCY/WbW+h4="/>
<field:input field="year" id="c_edu_ndnu_capstone_domain_User_year" required="true" validationMessageCode="field_invalid_number" max="4" min="4" z="HL+hSR+nz8/34t6nn2HXLRJaIyA="/>
<field:input field="active" id="c_edu_ndnu_capstone_domain_User_active" required="true" validationMessageCode="field_invalid_integer" max="1" min="1" z="wMEcOQjcrIJKY5RQ7mQ4/Uu9Ago="/>
<field:input field="description" id="c_edu_ndnu_capstone_domain_User_description" required="true" z="6TJHmWCAvkCtONvKOBHftLCMtso="/>
</form:create>
<form:dependency dependencies="${dependencies}" id="d_edu_ndnu_capstone_domain_User" render="${not empty dependencies}" z="Do0kIZAQM8ZWzWNlt1c/uMANnj4="/>
</div>
User_Roo_DbManaged.aj
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
// You may push code into the target .java compilation unit if you wish to edit any member(s).
package edu.ndnu.capstone.domain;
import edu.ndnu.capstone.domain.Emergency;
import edu.ndnu.capstone.domain.User;
import edu.ndnu.capstone.domain.UserType;
import java.util.Calendar;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.springframework.format.annotation.DateTimeFormat;
privileged aspect User_Roo_DbManaged {
@OneToMany(mappedBy = "userId")
private Set<Emergency> User.emergencies;
@ManyToOne
@JoinColumn(name = "type_id", referencedColumnName = "id", nullable = false)
private UserType User.typeId;
@Column(name = "name", length = 256)
@NotNull
private String User.name;
@Column(name = "email", length = 256, unique = true)
@NotNull
private String User.email;
@Column(name = "username", length = 150, unique = true)
@NotNull
private String User.username;
@Column(name = "password")
@NotNull
private String User.password;
@Column(name = "phone", length = 10, unique = true)
@NotNull
private String User.phone;
@Column(name = "year", length = 4)
@NotNull
private String User.year;
@Column(name = "active", length = 1)
@NotNull
private Integer User.active;
@Column(name = "created", updatable = false)
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "MM")
private Calendar User.created = java.util.Calendar.getInstance();
@Column(name = "description", length = 1024)
@NotNull
private String User.description;
public Set<Emergency> User.getEmergencies() {
return emergencies;
}
public void User.setEmergencies(Set<Emergency> emergencies) {
this.emergencies = emergencies;
}
public UserType User.getTypeId() {
return typeId;
}
public void User.setTypeId(UserType typeId) {
this.typeId = typeId;
}
public String User.getName() {
return name;
}
public void User.setName(String name) {
this.name = name;
}
public String User.getEmail() {
return email;
}
public void User.setEmail(String email) {
this.email = email;
}
public String User.getUsername() {
return username;
}
public void User.setUsername(String username) {
this.username = username;
}
public String User.getPassword() {
return password;
}
public void User.setPassword(String password) {
this.password = password;
}
public String User.getPhone() {
return phone;
}
public void User.setPhone(String phone) {
this.phone = phone;
}
public String User.getYear() {
return year;
}
public void User.setYear(String year) {
this.year = year;
}
public Integer User.getActive() {
return active;
}
public void User.setActive(Integer active) {
if(active==0 || active==1)
this.active = active;
else
this.active = 2;
}
public Calendar User.getCreated() {
return created;
}
public void User.setCreated(Calendar created) {
this.created = created;
}
public String User.getDescription() {
return description;
}
public void User.setDescription(String description) {
this.description = description;
}
}
最佳答案
首先,您不应修改任何.aj
,因为Spring Roo下次运行它时将覆盖您的更改。要自定义字段,您必须将其声明移到相关的.java
文件中(我们称其为推入式)。
为了设置字段验证,Roo使用JSR-303 specification作为实现,使用Hibernate validation。
因此,要对其进行存档,请尝试:
将phone
声明推入User.java
并添加javax.validation.constraints.Size
批注:
public class User {
@Column(name = "phone", length = 10, unique = true)
@NotNull
@Size(max=10,min=10)
private String phone;
}
运行Spring Roo控制台。
之后,Roo将:
从
phone
删除User_Roo_DbManaged.aj
定义在
User_Roo_bean.aj
上创建getter和setter更新与
User
相关的.jspx请注意,
@Column
定义是指数据库声明。您应该使用javax.validation.constraints.*
批注或创建自己的其他任何字段验证(here you have an example)祝好运!