nie*_*els 19 java validation playframework
我尝试为play-framework编写检查,并看到两种不同的可能性.我描述了两者,并想知道我的理解是否正确(所以它更像是一个教程而不是一个问题,特别是因为我没有得到任何我错过的回复).那么存在什么可能性.
Check:AbstractAnnotationCheck.在我们查看实现之前,我想解释一下这些消息.您始终可以直接设置消息,也可以使用密钥在消息属性中引用消息.最后一个是更清洁和推荐的方式.每个验证都至少获得1个参数:属性的名称无效.因此,验证或检查特定参数总是%i$s在i> 1的情况下引用.消息字符串的格式应遵循Formatter的规则,但我不确定是否支持所有功能.据我所知只有%s,%d和%f支持定位.所以%[argument_index$][flags]conversion转换只能是s,d或f.
让我们看看两个例子:我在模块中用于乐观锁定的简单方法:
/**
* Check with proof if the version of the current edited object is lesser
* than the version in db.
* Messagecode: optimisticLocking.modelHasChanged
* Parameter: 1 the request URL.
* Example-Message: The object was changed. <a href="%2$s">Reload</a> and do your changes again.
*
*/
static class OptimisticLockingCheck extends Check {
/**
* {@inheritDoc}
*/
@Override
public boolean isSatisfied(Object model, Object optimisiticLockingViolatedValue) {
//The comparision of version was made in the setter. Here
//we only have to check the flag.
if (((VersionedModel) model).optimisiticLockingViolated) {
final Request request = Request.current();
//The following doesn't work in 1.0 but in 1.1 see https://bugs.launchpad.net/play/+bug/634719
//http://play.lighthouseapp.com/projects/57987-play-framework/tickets/116
//setMessage(checkWithCheck.getMessage(), request != null ? request.url : "");
setMessage("optimisticLocking.modelHasChanged", request != null ? request.url : "");
}
return !((VersionedModel) model).optimisiticLockingViolated;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此检查和注释 @CheckWith(value=OptimisticLockingCheck.class, message="optimisticLocking.modelHasChanged")
所以让我们仔细看看它是如何工作的.我们唯一要做的就是扩展类play.data.validation.Check并覆盖isSatisfied方法.在那里,您可以获得模型和属性的值.你所要做的就是如果一切正常则返回true,否则返回false.在我们的例子中,我们想要将当前url设置为参数.这可以通过调用setMessage()轻松完成.我们给出消息属性和参数中定义的消息或消息密钥.请记住,我们只提供1个参数,但引用为%2 $ s,因为第一个参数始终是属性的名称.
现在基于游戏范围检查的复杂方式:首先我们需要定义一个注释
/**
* This field must be lower than and greater than.
* Message key: validation.range
* $1: field name
* $2: min reference value
* $3: max reference value
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Constraint(checkWith = RangeCheck.class)
public @interface Range {
String message() default RangeCheck.mes;
double min() default Double.MIN_VALUE;
double max() default Double.MAX_VALUE;
}
Run Code Online (Sandbox Code Playgroud)
然后检查
@SuppressWarnings("serial")
public class RangeCheck extends AbstractAnnotationCheck<Range> {
final static String mes = "validation.range";
double min;
double max;
@Override
public void configure(Range range) {
this.min = range.min();
this.max = range.max();
setMessage(range.message());
}
public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) {
requireMessageVariablesRecreation();
if (value == null) {
return true;
}
if (value instanceof String) {
try {
double v = Double.parseDouble(value.toString());
return v >= min && v <= max;
} catch (Exception e) {
return false;
}
}
if (value instanceof Number) {
try {
return ((Number) value).doubleValue() >= min && ((Number) value).doubleValue() <= max;
} catch (Exception e) {
return false;
}
}
return false;
}
@Override
public Map<String, String> createMessageVariables() {
Map<String, String> messageVariables = new TreeMap<String, String>();
messageVariables.put("2-min", Double.toString(min));
messageVariables.put("3-max", Double.toString(max));
return messageVariables;
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,我认为不必解释注释.让我们看看支票.在这种情况下,它延伸net.sf.oval.configuration.annotation.AbstractAnnotationCheck.我们必须编写一个configure-method来获取注释并可以复制参数.然后我们必须定义我们的支票.这与其他检查的实施类似.所以我们只写一个条件并返回true或false,除了一个特殊的行!如果我们使用参数化消息,我们必须调用requireMessageVariablesRecreation();我们的方法.至少我们必须覆盖该方法createMessageVariables.在这里,我们必须得到一个小比特play-knowlegde(这里描述所有其他的东西).您将消息放入带有键和值的地图中,但播放只接受值(请参阅ValidCheck.java在框架代码中).因此它将被位置引用.这就是我改变RangeCheck使用TreeMap而不是使用的实现的原因HashMap.此外,我让密钥以他们可以引用的索引开头.
所以我希望这更清楚如何编写自定义验证/检查游戏.我希望描述是正确的.因此问题是我的理解正确吗?
至少你的第一个例子看起来是在正确的路径上。您可以将其与下面提供的文档进行比较,但从您示例的复杂性来看,我认为您已经参考过它。
http://www.playframework.org/documentation/1.1/validation#custom
我对播放框架了解不够,无法评论第二个示例。
| 归档时间: |
|
| 查看次数: |
8546 次 |
| 最近记录: |