如何将双参数与 Play 2.0 路由绑定 [英] How to bind Double parameter with Play 2.0 routing
问题描述
我正在学习自己的 Play 2.0(使用 Java API)并且想要一个双精度/浮点参数(用于位置坐标),例如 http://myfooapp.com/events/find?latitude=25.123456&longitude=60.251253.
I'm learning myself Play 2.0 (Java API used) and would like to have a double/float parameter (for location coordinates), something like http://myfooapp.com/events/find?latitude=25.123456&longitude=60.251253.
我可以通过将参数作为字符串获取并在控制器等处解析它们来做到这一点,但我可以在这里使用自动绑定吗?
I can do this by getting the parameters as String and parsing them at controller etc but can I use automatic binding here?
现在,我首先尝试简单地使用一个 double 值:
Now, I first tried simply having one double value:
GET /events/foo controllers.Application.foo(doublevalue: Double)
与
public static Result foo(Double doublevalue) {
return ok(index.render("Foo:" + doublevalue));
}
我得到的是找不到 Double 类型的 QueryString 绑定器.尝试为这种类型实现隐式 QueryStringBindable."
我是否错过了已经提供的内容或我必须制作一个自定义的 QueryStringBindable 来解析 Double 吗?
Have I missed something already provided or do I have to make a custom QueryStringBindable that parses Double?
我在 http://julien.richard-foy.fr/blog/2012/04/09/how-to-implement-a-custom-pathbindable-with-play-2/
我在包活页夹中实现了 DoubleBinder:
I implemented DoubleBinder at package binders:
import java.util.Map;
import play.libs.F.Option;
import play.mvc.QueryStringBindable;
public class DoubleBinder implements QueryStringBindable<Double>{
@Override
public Option<Double> bind(String key, Map<String, String[]> data) {
String[] value = data.get(key);
if(value == null || value.length == 0) {
return Option.None();
} else {
return Option.Some(Double.parseDouble(value[0]));
}
}
@Override
public String javascriptUnbind() {
// TODO Auto-generated method stub
return null;
}
@Override
public String unbind(String key) {
// TODO Auto-generated method stub
return null;
}
}
并尝试将其添加到 project/Build.scala 的 main 中:
And tried to add it to project/Build.scala's main:
routesImport += "binders._"
但相同的结果:未找到 Double 类型的 QueryString 绑定器...."
but same result : "No QueryString binder found for type Double...."
- 我还将路由签名更改为 java.lang.Double 但这也无济于事
- 我还改变了 DoubleBinder 来实现 play.api.mvc.QueryStringBindable(而不是 play.mvc.QueryStringBindable)java.lang.Double 在路由签名处,但仍然没有帮助
推荐答案
目前(在 Play 2.0 中),Java 绑定器仅适用于自递归类型.也就是说,类型如下所示:
Currently (in Play 2.0), Java binders only work with self-recursive types. That is, types looking like the following:
class Foo extends QueryStringBindable<Foo> {
…
}
因此,如果要为 java.lang.Double
定义一个绑定器,它是 Java 的现有类型,则需要将其包装在自递归类型中.例如:
So, if you want to define a binder for java.lang.Double
, which is an existing type of Java, you need to wrap it in a self-recursive type. For example:
package util;
public class DoubleW implements QueryStringBindable<DoubleW> {
public Double value = null;
@Override
public Option<DoubleW> bind(String key, Map<String, String[]> data) {
String[] vs = data.get(key);
if (vs != null && vs.length > 0) {
String v = vs[0];
value = Double.parseDouble(v);
return F.Some(this);
}
return F.None();
}
@Override
public String unbind(String key) {
return key + "=" + value;
}
@Override
public String javascriptUnbind() {
return value.toString();
}
}
然后你可以在你的应用程序中使用它:
Then you can use it as follows in your application:
GET /foo controllers.Application.action(d: util.DoubleW)
public static Result action(DoubleW d) {
…
}
这篇关于如何将双参数与 Play 2.0 路由绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!