简体   繁体   English

如何在ActiveWeb中支持netsted资源

[英]How to support netsted resources in ActiveWeb

Two models named "Post" and "Comment", and I want to build RESTful route like /post/123/comment/8 and CommentsController handles all RESTful actions. 有两个名为“ Post”和“ Comment”的模型,我想构建RESTful路由,例如/post/123/comment/8CommentsController处理所有RESTful动作。

I have tried 我努力了

route("/posts/{post_id}/comments/*").to(CommentsController.class);

and

route("/posts/{post_id}/comments/*").to(CommentsController.class).get().post().put().delete(); 

And none works:( 而且没有效果:(

What you need is not a RESTful set of routes from the point of AvctiveWeb framework: http://javalite.io/routing#restful-routing . 从AvctiveWeb框架的角度出发,您不需要的是一组RESTful路由: http ://javalite.io/routing#restful-routing。

What you are trying to do is to define some custom routes. 您尝试做的是定义一些自定义路线。 Not to say that they are not REST - based though. 并不是说它们不是基于REST的。

Here is the example that works: 这是有效的示例:

The route: 路线:

public class RouteConfig extends AbstractRouteConfig{
    public void init(AppContext appContext) {
        route("/posts/{post_id}/comments/{comment_id}").to(CommentsController.class).action("index").get();
    }
}

The controller: 控制器:

public class CommentsController extends AppController {
    public void index(){
        respond("index, post:" + param("post_id") + ", comment: " + param("comment_id"));
    }
}

then hit this: 然后点击:

http://localhost:8080/posts/123/comments/456 HTTP://本地主机:8080 /职位/ 123 /评论/ 456

and observe: 并观察:

index, post:123, comment: 456

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM