Java – how to bind two parameters using play 2.0 routing

I'm learning play 2.0 (using the Java API) and want to have a double / float parameter (location coordinates), like http://myfooapp.com/events/find?latitude=25.123456&longitude=60.251253.

I can get the parameters as strings and parse them in the controller, but can I use automatic binding here?

Now, I'll try a double value first:

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

with

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

What I get is "no querytype binding found type double. Try to implement implicit querystringbindable for this type

I missed something already provided. Do I need to customize querystringbindable to analyze double?

I found something about http://julien.richard-foy.fr/blog/2012/04/09/how-to-implement-a-custom-pathbindable-with-play-2/ Description of using Scala to make custom string query string binder on

I tried

I implemented doublebinder on the packaging machine:

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 try to add to the project / build The main of scala:

routesImport += "binders._"

But the result is the same: "querystring binding of type double... Cannot be found"

>I also changed the route signature to Java Lang. double, but it doesn't help > I also changed doublebinder to implement play api. mvc. Querystringbindable (instead of play. MVC. Querystringbindable), dual sum & Java Lang. double signed the route, but it didn't help

Solution

Currently (in version 2.0), Java binding is only applicable to self - recursive types That is, similar to the following:

class Foo extends QueryStringBindable<Foo> {
  …
}

Therefore, if you want to define Java Lang. double (which is an existing type of Java), you need to wrap it as 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,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 in your application:

GET    /foo     controllers.Application.action(d: util.DoubleW)
public static Result action(DoubleW d) {
      …
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>