問題描述
我正在學習自己的 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 路由綁定的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!