zoukankan      html  css  js  c++  java
  • Play Framework 完整实现一个APP(十三)

    添加用户编辑区

    1.修改Admin.index()

    public static void index() {
        List<Post> posts = Post.find("author.email", Security.connected()).fetch();
        render(posts);
    }
    

      

    2.修改页面 app/views/Admin/index.html

    #{extends 'admin.html' /}
     
    <h3>Welcome ${user}, <span>you have written ${posts.size() ?: 'no'} ${posts.pluralize('post', 'posts')} so far</span></h3>
     
    #{list items:posts, as:'post'}
        <p class="post ${post_parity}">
            <a href="#">${post.title}</a>
        </p>
    #{/list}
     
    <p id="newPost" >
        <a href="#"><span>+</span> write a new post</a>
    </p>
    

    个人展示页完成

    3.添加新增Post页面

    Admin新增Action

    public static void form() {
        render();
    }
     
    public static void save() {
        // Not implemented yet
    }

    添加路由

    GET     /admin/new                          Admin.form
    POST    /admin/new                          Admin.save
    

      

    创建页面 /app/views/Admin/form.html 

    #{extends 'admin.html' /}
    
    <h3>Write, <span>a new post</span></h3>
    
    #{form @save()}
    	#{ifErrors}
            <p class="error">
                Please correct these errors.
            </p>
    	#{/ifErrors}	
    	
    	 <p>
            #{field 'title'}
            <label>Post title:</label>
            <input type="text" name="${field.name}" 
                value="${post?.title}" class="${field.errorClass}" />
            #{/field}
        </p>
        
        <p>
            #{field 'content'}
            <label>Write here:</label>
            <textarea name="${field.name}" 
                class="${field.errorClass}">${post?.content}</textarea>
            #{/field}
        </p>
        
        <p>
            #{field 'tags'}
            <label>Enter some tags:</label>
            <input type="text" size="50" 
                name="${field.name}" value="${post?.tags?.join(' ')}" />
            #{/field}
        </p>
        
        <p>
            <input type="submit" value="Publish this post to the blog" />
        </p>
    
    #{/form}
    

      

    Admin.index.html 添加Post按钮

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

      

    3.完善Admin.Save方法

    public static void save(String title, String content, String tags) {
        // Create post
        User author = User.find("byEmail", Security.connected()).first();
        Post post = new Post(author, title, content);
        // Set tags list
        for(String tag : tags.split("\s+")) {
            if(tag.trim().length() > 0) {
                post.tags.add(Tag.findOrCreateByName(tag));
            }
        }
        // Validate
        validation.valid(post);
        if(validation.hasErrors()) {
            render("@form", post);
        }
        // Save
        post.save();
        index();
    }

    新建Post可以保存了

    4.修改Admin.form

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

      

    修改Admin/index.html 

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

      

    添加Route

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

      

    5.修改 app/views/Admin/form.html 

    #{extends 'admin.html' /}
     
    #{ifnot post?.id}
        <h3>Write, <span>a new post</span></h3>
    #{/ifnot}
    #{else}
        <h3>Edit, <span>this post</span></h3>
    #{/else}
     
    #{form @save(post?.id)}
     
        #{ifErrors}
            <p class="error">
                Please correct these errors.
            </p>
        #{/ifErrors}
         
        <p>
            #{field 'title'}
            <label>Post title:</label>
            <input type="text" name="${field.name}" 
                value="${post?.title}" class="${field.errorClass}" />
            #{/field}
        </p>
     
        <p>
            #{field 'content'}
            <label>Write here:</label>
            <textarea name="${field.name}" 
                class="${field.errorClass}">${post?.content}</textarea>
            #{/field}
        </p>
     
        <p>
            #{field 'tags'}
            <label>Enter some tags:</label>
            <input type="text" size="50" 
                name="${field.name}" value="${post?.tags?.join(' ')}" />
            #{/field}
        </p>
        
        <p>
            <input type="submit" value="Publish this post to the blog" />
        </p>
        
    #{/form}
    

      

    改进Admin.Save方法

    public static void save(Long id, String title, String content, String tags) {
        Post post;
        if(id == null) {
            // Create post
            User author = User.find("byEmail", Security.connected()).first();
            post = new Post(author, title, content);
        } else {
            // Retrieve post
            post = Post.findById(id);
            // Edit
            post.title = title;
            post.content = content;
            post.tags.clear();
        }
        // Set tags list
        for(String tag : tags.split("\s+")) {
            if(tag.trim().length() > 0) {
                post.tags.add(Tag.findOrCreateByName(tag));
            }
        }
        // Validate
        validation.valid(post);
        if(validation.hasErrors()) {
            render("@form", post);
        }
        // Save
        post.save();
        index();
    }

    添加Route

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

      

    。。

  • 相关阅读:
    [转]在Ubuntu 下安装Redis 并使用init 脚本启动
    [资源]PHP使用消息队列
    [转]reids客户端 redis-cli用法
    [转]redis.conf的配置解析
    【转】微信公共号开发,提示“该公众号暂时无法提供服务,请稍后再试”,如何解决?
    [转]php 解决json_encode中文UNICODE转码问题
    [资料]Keychain 获取设备唯一
    [转]PHP 获取服务器详细信息代码
    crontab任务取消发送邮件
    [转]php返回json数据中文显示的问题
  • 原文地址:https://www.cnblogs.com/alex09/p/4923406.html
Copyright © 2011-2022 走看看