如何使用Play 2.0路由绑定双参数 [英] How to bind Double parameter with Play 2.0 routing

查看:125
本文介绍了如何使用Play 2.0路由绑定双参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习自己的Play 2.0(使用Java API),并希望拥有一个double / float参数(位置坐标),类似于 http://myfooapp.com/events/find?latitude=25.123456&longitude=60.251253



我可以通过获取参数为String并在控制器中解析这些参数,但可以在这里使用自动绑定吗?



现在,我第一次尝试简单有一个双重值:

  GET / events / foo controllers.Application.foo(doublevalue:Double)

  public static结果foo(Double doublevalue){
return ok(index.render(Foo:+ doublevalue));
}

我得到的是没有找到类型Double的QueryString binder。尝试为此类型实现一个隐式的QueryStringBindable。



我是否错过了已经提供的东西,或者
我必须自定义QueryStringBindable分析双倍



我在 http://julien.richard-foy.fr/blog/2012/04/09/我如何尝试:
$($)

b $ b

我在包装绑定器上实现了DoubleBinder:

  import java.util.Map; 
import play.libs.F.Option;
import play.mvc.QueryStringBindable;

public class DoubleBinder实现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自动生成的方法存根
返回null;
}

@Override
public String unbind(String key){
// TODO自动生成的方法存根
返回null;
}
}

并尝试将其添加到项目/ Build.scala的主要:

  routesImport + =binder._

但结果相同:找不到类型Double ....的QueryString binder




  • 我也把路由签名改成了java.lang.Double,但是没有帮助,因为

  • 我也改变了DoubleBinder来实现play.api.mvc.QueryStringBindable(而不是play .mvc.QueryStringBindable)与Double& java.lang.Double在路由签名,但没有帮助仍然


解决方案

目前2.0),Java绑定只适用于自递归类型。也就是说,类型如下所示:

  class Foo extends QueryStringBindable&Foo> {
...
}

所以,如果你想定义一个 java.lang.Double ,它是一种现有类型的Java,您需要将其包装为自递归类型。例如:

  package util; 

public class DoubleW实现QueryStringBindable< DoubleW> {

public Double value = null;

@Override
public选项< DoubleW> bind(String key,Map< String,String []> data){
String [] vs = data.get(key);
if(vs!= null&&对比度> 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();
}

}

然后可以使用它如下在您的应用程序中:

  GET / foo controllers.Application.action(d:util.DoubleW)



  public static Result action(DoubleW d){
...
}


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)

with

public static Result foo(Double doublevalue) {
    return ok(index.render("Foo:" + doublevalue));
}

What I got was "No QueryString binder found for type Double. Try to implement an implicit QueryStringBindable for this type."

Have I missed something already provided or do I have to make a custom QueryStringBindable that parses Double?

I found some instructions on making a custom string query string binder with Scala at http://julien.richard-foy.fr/blog/2012/04/09/how-to-implement-a-custom-pathbindable-with-play-2/

What I tried:

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;
    }
}

And tried to add it to project/Build.scala's main:

routesImport += "binders._"

but same result : "No QueryString binder found for type Double...."

  • I also changed the routing signature to java.lang.Double but that didn't help either
  • I also changed the DoubleBinder to implement play.api.mvc.QueryStringBindable (instead of play.mvc.QueryStringBindable) both with Double & java.lang.Double at the routing signature but no help still

解决方案

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> {
  …
}

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆