路线的playframework问题:NoRouteFoundException

时间:2011-08-20 14:05:56

标签: exception routes playframework

在游戏教程中试用自定义编辑器部分,我创建了如下路线

GET     /admin/myPosts/{id}                     Admin.form
GET     /admin/new                              Admin.form
POST    /admin/new                              Admin.save

GET      /admin?                                Admin.index
*        /admin                                 module:crud

并在Admin类

中创建方法
..
public static void form() {
        logger.info("Admin.form()");
        render();
    }       
    public static void save(String title,String content,String tags) {
        User author = User.find("byEmail",Security.connected()).first();
        logger.info("author="+author.getEmail());
        //create a post
        Post newPost = new Post(author,title,content);
        logger.info("new post="+newPost.getTitle()+" created");
        //set tags
        String[] tagArray = tags.split("\\s+");
        logger.info("tag array="+tagArray.length);
        for(String tag : tagArray ) {
            logger.info("tag="+tag);
            if(tag.trim().length() > 0) {
                newPost.getTags().add(Tag.findOrCreateByName(tag));
            }               
        }
        validation.valid(newPost);
        if(validation.hasErrors()) {
            logger.error("error in post");
            render("@form", newPost);
        }
        newPost.save();
        logger.info("new post saved");
        logger.info("going to index");
        index();

    }       
    public static void form(Long id) {
        if(id!=null) {
            Post post = Post.findById(id);
            render(post);
        }
        render();
    }
...

views / Admin / index.html是

#{extends 'admin.html'/}
Welcome ${user}!! <span>you have written ${posts.size()?:'no'} ${posts.pluralize('post','posts')} so far </span>

#{list items:posts,as:'post' }
    <p class="post ${post_parity}">
        ${post_index}.<a href="@{Admin.form(post.id)}">${ post.title}</a>
    </p>
#{/list}
<p id="newPost">
    <a href="@{form()}"><span>+</span>write new post</a>
</p>

当我点击登录时,包含链接"@{Admin.form(post.id)}"的行会导致

Internal Server Error (500) for request GET /admin?

No route found (In /app/views/Admin/index.html around line 7)
No route able to invoke action Admin.form was found.

play.exceptions.NoRouteFoundException: No route found
    at play.templates.BaseTemplate.throwException(BaseTemplate.java:80)
    at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:237)
    at play.templates.Template.render(Template.java:26)
    at play.templates.GroovyTemplate.render(GroovyTemplate.java:184)
    at play.mvc.results.RenderTemplate.<init>(RenderTemplate.java:24)
    at play.mvc.Controller.renderTemplate(Controller.java:659)
    at play.mvc.Controller.renderTemplate(Controller.java:639)
    at play.mvc.Controller.render(Controller.java:694)
    at controllers.Admin.index(Admin.java:33)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:543)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:499)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:475)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:470)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:158)
    at Invocation.HTTP Request(Play!)

如何解决此问题?有人可以提供帮助吗?路径中的路径顺序是否存在问题?根据启动消息可以使用crud and secure模块

更新: play所示的stacktrace就在这里

application error page

我试图更改路线文件中的条目顺序。请* /admin path before GET / admin? as below ..现在我得到一个奇怪的呈现页面

GET     /admin/myPosts/{id}                     Admin.form
GET     /admin/new                              Admin.form
POST    /admin/new                              Admin.save

*        /admin                                 module:crud
GET      /admin?                                Admin.index

weird page 这是登录时bob @ gmail结束的地方! 对于网址http://localhost:9000/admin?,页面应显示MyPosts链接为选中..但此处评论链接显示为已选中..

所以,它必须是路径文件中路径顺序的问题...特别是* /admin?的问题..有人可以告诉我它应该放在哪里吗?

路线文件中有一个小问题(缺少/) 完整的路线文件是

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

#import crud routes

GET     /admin/myPosts/{id}                     Admin.form
GET     /admin/new                              Admin.form
POST    /admin/myPosts/{id}                     Admin.save
POST    /admin/new                              Admin.save

GET     /admin/?                                Admin.index
*       /admin                                  module:crud

# Home page
GET     /                                       Application.index

# details of a post
GET     /posts/{<[0-9]+>id}                             Application.showPost
GET     /captcha                                Application.captcha
GET     /posts/{tag}                            Application.taggedWith
POST    /posts/{<[0-9]+>id}/comments                    Application.postComment

# Ignore favicon requests
GET     /favicon.ico                            404

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
# Import Secure routes
*       /                                       module:secure
*       /{controller}/{action}                  {controller}.{action}

当我省略index.html中的Admin.form(post.id)链接

时,管理页面会正确显示
#{list items:posts,as:'post' }
    <p class="post ${post_parity}">
        ${post_index}.<a href="#">${ post.title}</a>
    </p>
#{/list}
<p id="newPost">
    <a href="@{Admin.form()}"><span>+</span>write new post</a>
</p>

添加链接后

#{list items:posts, as:'post'}
    <p class="post ${post_parity}">
        <a href="@{Admin.form(post.id)}">${post.title}</a>
    </p>
#{/list}

发生No route able to invoke action Admin.form was found错误消息

2 个答案:

答案 0 :(得分:2)

引起混淆的原因是,在Admin类中有2个form()方法,一个没有arg,另一个id为arg。没有arg方法放在另一个之前...使路由器很混乱..

当方法表单(长号)替换时,问题解决了

答案 1 :(得分:0)

您确定是导致问题的路线吗?我刚刚尝试过这种情况并且无法重新创建错误(尽管没有使用CRUD和安全模块),但是这些路由稍后会出现在路径文件中,这些不应该导致问题。

我认为可能是导致问题的以下代码行。

 <a href="@{form()}"><span>+</span>write new post</a>

具体来说,这位@{form()}

我认为应该阅读

@{Admin.form()}