How to bind Double parameter with Play 2.0 routing

后端 未结 1 544
离开以前
离开以前 2020-12-30 10:28

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?lati

1条回答
  •  长情又很酷
    2020-12-30 10:54

    Currently (in Play 2.0), Java binders only work with self-recursive types. That is, types looking like the following:

    class Foo extends QueryStringBindable {
      …
    }
    

    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 {
    
        public Double value = null;
    
        @Override
        public Option bind(String key, Map 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) {
          …
    }
    

    0 讨论(0)
提交回复
热议问题