Java Grails命令扩展了另一个命令错误
当我扩展一个通用命令以重用其自定义验证器时,Grails中的命令出现了问题 当我清理grails项目时,它会丢失对扩展自定义验证器的引用。我需要在CommandA中导致项目运行时出现语法错误,并撤消该错误,以便它编译回该命令 文件:/src/groovy/app/commands/ImageCommand.groovyJava Grails命令扩展了另一个命令错误,java,oop,grails,command,extend,Java,Oop,Grails,Command,Extend,当我扩展一个通用命令以重用其自定义验证器时,Grails中的命令出现了问题 当我清理grails项目时,它会丢失对扩展自定义验证器的引用。我需要在CommandA中导致项目运行时出现语法错误,并撤消该错误,以便它编译回该命令 文件:/src/groovy/app/commands/ImageCommand.groovy class ImageCommand { static imageValidator = { value, command -> ... DO SO
class ImageCommand {
static imageValidator = { value, command ->
... DO SOMETHING ...
}
}
文件:/src/groovy/app/commands/CommandA.groovy
class CommandA extends ImageCommand {
def file
static constraints = {
file nullable: true, blank: true, validator: imageValidator
}
}
要导致错误,我只需删除命令的一部分a:
class CommandA extends ImageCommand {
def file
static constraints = {
file nullable: true, blank:
}
}
并将其撤消以强制重新编译:
class CommandA extends ImageCommand {
def file
static constraints = {
file nullable: true, blank: true, validator: imageValidator
}
}
我该怎么办?我无法将CommandA移动到控制器文件,因为我在许多地方使用它
*使用Grails2.2.2您可以尝试将验证器移动到
src\groovy
中的单独类,如下所示:
class PhoneNumberConstraint extends AbstractConstraint {
private static final String NAME = "phoneNumber";
private static final String DEFAULT_MESSAGE_CODE = "default.phoneNumber.invalid.message";
@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
if (!isPhoneNumber(target, propertyValue)) {
Object[] args = [constraintPropertyName, constraintOwningClass, propertyValue]
rejectValue(target, errors, DEFAULT_MESSAGE_CODE, DEFAULT_MESSAGE_CODE, args);
}
}
private boolean isPhoneNumber(Object target, Object propertyValue) {
if(propertyValue instanceof String && ((String)propertyValue).isNumber() &&
(((String)propertyValue).length() == 13 || ((String)propertyValue).length() == 11)) {
return true
}
return false
}
@Override
boolean supports(Class type) {
return type != null && String.class.isAssignableFrom(type)
}
@Override
String getName() {
return NAME
}
}
beans = {
ConstrainedProperty.registerNewConstraint(PhoneNumberConstraint.NAME, PhoneNumberConstraint.class);
}
然后在resources.groovy
中注册验证器,如下所示:
class PhoneNumberConstraint extends AbstractConstraint {
private static final String NAME = "phoneNumber";
private static final String DEFAULT_MESSAGE_CODE = "default.phoneNumber.invalid.message";
@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
if (!isPhoneNumber(target, propertyValue)) {
Object[] args = [constraintPropertyName, constraintOwningClass, propertyValue]
rejectValue(target, errors, DEFAULT_MESSAGE_CODE, DEFAULT_MESSAGE_CODE, args);
}
}
private boolean isPhoneNumber(Object target, Object propertyValue) {
if(propertyValue instanceof String && ((String)propertyValue).isNumber() &&
(((String)propertyValue).length() == 13 || ((String)propertyValue).length() == 11)) {
return true
}
return false
}
@Override
boolean supports(Class type) {
return type != null && String.class.isAssignableFrom(type)
}
@Override
String getName() {
return NAME
}
}
beans = {
ConstrainedProperty.registerNewConstraint(PhoneNumberConstraint.NAME, PhoneNumberConstraint.class);
}
对我来说,它可以完美地使用命令。它应该可以工作。你能把你的错误记录出来吗?没有错误。CommandA只是忽略了验证器,因为它不在那里。我仍然无法让它为我工作。错误validation.ConstrainedProperty-将约束[uploadImage]实例化到类[class me.emotion.commands.profile.GodParentsAddCommand]时引发异常消息:class org.codehaus.groovy.grails.validation.ConstrainedProperty无法使用修饰符访问类me.emotion.constraints.ImageConstraint的成员“”确保ImageConstraint中的所有方法都是公共的。