How to bind Double parameter with Play 2.0 routing(如何将 Double 参数与 Play 2.0 路由绑定)
问题描述
我正在学习自己的 Play 2.0(使用 Java API)并希望有一个 double/float 参数(用于位置坐标),例如 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?
现在,我首先尝试简单地设置一个双精度值:
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."
我是否错过了已经提供的内容或我是否必须制作一个解析 Double 的自定义 QueryStringBindable?
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 以使用 Double & 实现 play.api.mvc.QueryStringBindable(而不是 play.mvc.QueryStringBindable).java.lang.Double 在路由签名处,但仍然没有帮助
 
推荐答案
目前(在 Play 2.0 中),Java binders 仅适用于自递归类型.也就是说,类型如下所示:
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 定义一个 binder,它是 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) {
      …
}
                        这篇关于如何将 Double 参数与 Play 2.0 路由绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Double 参数与 Play 2.0 路由绑定
				
        
 
            
        基础教程推荐
- 从 python 访问 JVM 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				