play framework学习笔记之 表单提交,超链接提交 与 页面渲染

    技术2022-05-20  44

     

    1,超链接 提交

     

    <a href="@{Application.run(1)}">run</a>

     

    public static void run(long num,String name){

    System.out.println(num); render(); }        

    2,表单提交

    //推荐使用这种非侵入式的写法,能使用Html的地方就不要用play自己的标签。这样有利于减轻前端的压力。

     

    <form action="@@{Application.check()}" method="post" enctype="multipart/form-data">

    <input type="text" name="name"/>

    <input type="password" name="pw"/>

    <input type="file" name="f"/>

    <input type="submit" value="submit"/>

    </form>

     

     

     

    #{form @Application.postComment(post.id)}            

    //注意到了这个地方了吗?在超链接的时候@{Application.run(1)}是加大括号的,此处没有加,也仅仅是这里没加。

     

    <input type="text" name="author" id="author" />

    <textarea name="content" id="content"></textarea>

     

    <input type="submit" value="Submit your comment" />

    #{/form}

     

    public static void postComment(Long postId, String author, String content) {

    Post post = Post.findById(postId);

    post.addComment(author, content);

    show(postId);

    }

    其中show是另外一个static方法,在其内调用render对应渲染 对应的页面,取决于 route配置文件内的配置。

     

    Post提交,路径控制

    POST   /run/{postId}            Application.run

    注意因为postId在#{form @Application.postComment(xx)}这个地方注入了,所以可以在route配置文件内被侦查到,其它的author,content都侦查不到。路径配置也是无效的。

     

     

     

    render(n,m)

    则在页面${n}  ${m}来获取

     

    或者也可以指明要渲染的页面 render("Application/run.html",n,m)

     


    最新回复(0)