我正在学习游戏框架并了解我可以映射一个请求,例如/manager/user
:
GET /manage/:user Controllers.Application.some(user:String)
我将如何映射请求/play/video?video_id=1sh1
?
我正在学习游戏框架并了解我可以映射一个请求,例如/manager/user
:
GET /manage/:user Controllers.Application.some(user:String)
我将如何映射请求/play/video?video_id=1sh1
?
You have at least two possibilities, let's call them approach1
and approach2
.
0
is good candidate, as it will be easiest to build some condition on top of it. Also it's typesafe
, and pre-validates itself. I would recommend this solution at the beginning.String
so you need to parse it to integer and additionally validate if required.routes
:
GET /approach1 controllers.Application.approach1(video_id: Int ?=0)
GET /approach2 controllers.Application.approach2
actions:
public static Result approach1(int video_id) {
if (video_id == 0) return badRequest("Wrong video ID");
return ok("1: Display video no. " + video_id);
}
public static Result approach2() {
int video_id = 0;
if (form().bindFromRequest().get("video_id") != null) {
try {
video_id = Integer.parseInt(form().bindFromRequest().get("video_id"));
} catch (Exception e) {
Logger.error("int not parsed...");
}
}
if (video_id == 0) return badRequest("Wrong video ID");
return ok("2: Display video no. " + video_id);
}
PS: LOL I just realized that you want to use String identifier... anyway both approaches will be similar :)
我会简单地使用:
GET /play/video Controllers.Application.video(video_id:String)
在控制器上,你当然会有,比如:
public static Result video(String video_id) {
return ok("We got video id of: " + video_id);
}
或者,您不必添加,video_id:String
因为默认情况下 play 似乎将参数视为字符串,因此它也可以这样工作(至少在最新的播放中):
GET /play/video Controllers.Application.video(video_id)
现在输入localhost:9000/play/video?video_id=1sh1
地址栏应该可以查看打印的内容:
我们得到的视频 ID 为:1sh1
添加更多参数很简单,如下所示:
GET /play/video controllers.Application.video(video_id:String, site:String, page:Integer)
控制器:
public static Result video(String video_id, String site, Integer page) {
return ok("We got video id of: " + video_id + " site: " + site + " page: " + page);
}
现在输入localhost:9000/play/video?video_id=1as1&site=www.google.com&page=3
地址栏应该可以查看打印的内容:
我们的视频 ID 为:1as1 站点:www.google.com 页面:3
不客气^^。
我不太确定我是否明白你的意思,如果你只是想映射一个 url 在控制器中运行,biesior 的答案是完美的,但如果你的意思是提交一个带有 get 方法的表单,比如
@helper.form(action = routes.YourController.page1()) {
}
并在 url 中以 url 重写格式包含表单的参数,例如
page1/foo/bar instead of page1?param1=foo¶m2=bar
没有办法做到这一点,因为那是 http 规范
我经常通过在控制器的第一个函数中获取参数来规避这个限制,然后将它们重定向到另一个视图,如下所示
public static Result page1(){
String param1 = Form.form().bindFromRequest().get("param1");
String param2= Form.form().bindFromRequest().get("param2");
return ( redirect( routes.YourController.page2(param1,param2)));
}
然后在page2中有任何内容
public static Result page2(String param1,String param2){
...............
}
并在路由文件中有这个:
GET page2/:param1/:param2 controllers.YourControllers.page2(param1 : String, param2 : String )
我希望它会有所帮助,但我不确定这是最佳做法
好的,所以我只是阅读了文档,我的理解是您需要
GET /play/video Controllers.Application.video()
然后在控制器中调用HttpRequest对象的getQueryString
http://www.playframework.com/documentation/api/2.1.0/java/index.html