【Go web开发之revel+mgo】第6章 实现留言板功能

jov123 · · 2042 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

演示地址   http://gblog-revel.herokuapp.com/


1.实现留言板

我们在上一章做了简单的设计,挑就留言的表单有了,那么。。。。。

首先,在app/models下新建message.go 内容:

package models
import (
	"github.com/revel/revel"
	"labix.org/v2/mgo/bson"
	"time"
)
type Message struct{
	Email string
	QQ string
	Url string
	CDate time.Time
	Content string
}
func (message *Message) Validate(v *revel.Validation) {
	v.Check(message.Email,
		revel.Required{},
		revel.MaxSize{50},
	)
	v.Email(message.Email)
	v.Check(message.QQ,
		revel.MaxSize{20},
	)
	v.Check(message.Url,
		revel.MaxSize{200},
	)
	v.Check(message.Content,
		revel.Required{},
		revel.MinSize{1},
		revel.MaxSize{1000},
	)
}
func (dao *Dao) InsertMessage(message *Message) error {
	messCollection := dao.session.DB(DbName).C(MessageCollection)
	//set the time
	message.CDate = time.Now();
	err := messCollection.Insert(message)
	if err != nil {
		revel.WARN.Printf("Unable to save Message: %v error %v", message, err)
	}
	return err
}
func (dao *Dao) FindAllMessages() []Message{
	messCollection := dao.session.DB(DbName).C(MessageCollection)
	mess := []Message{}
	query := messCollection.Find(bson.M{}).Sort("-cdate")
	query.All(&mess)
	return mess
}

看一看,跟我们的comment.go更加的相似。

在app/controllers下新建wmessage.go 内容:

package controllers

import (
	"github.com/revel/revel"
	"GBlog/app/models"
	"strings"
	"fmt"
)
type WMessage struct {
	App
}
func (c WMessage) Putup(message *models.Message) revel.Result {
	message.Email = strings.TrimSpace(message.Email);
	message.Url = strings.TrimSpace(message.Url);
	message.Content = strings.TrimSpace(message.Content);
	message.QQ = strings.TrimSpace(message.QQ);
	message.Validate(c.Validation)
	fmt.Println(c.Validation)
	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		c.Flash.Error("Errs:The email and the content should not be null,or the maxsize of email is 50.")
		return c.Redirect(App.Message)
	}
	dao, err := models.NewDao()
	if err != nil {
		c.Response.Status = 500
		return c.RenderError(err)
	}
	defer dao.Close()
	err = dao.InsertMessage(message)
	if(err!=nil){
		c.Response.Status = 500
		return c.RenderError(err)
	}
	return c.Redirect(App.Message)
}

恩,很熟系吧。

好,来添加路由,在conf/routes里添加:

POST    /putmessage                 			WMessage.Putup

ok了吗,下面我们来显示它。

在app.go的 Message()方法修改为:

func (c App) Message() revel.Result {
	dao, err := models.NewDao()
	if err != nil {
		c.Response.Status = 500
		return c.RenderError(err)
	}
	defer dao.Close()
	//dao := models.NewDao(c.MongoSession)
	messages := dao.FindAllMessages()
	return c.Render(messages)
}

将Views/App/Message.html最后修改为:

{{set . "title" "Message - GBlog"}}
{{set . "mess" "active" }}
{{template "header.html" .}}
<div class="content">
    <div class="comment-nav">
       <div class="comment-title">
         留言板
         <span style="float:right" title="Total messages">[{{len .messages}}]</span>
       </div>
       <div class="comment-cell">
         <div class="comment-inputbox">
            <form action="/putmessage" method="post" >
                  <ul class="comment-input-infor">
                    {{with $field := field "message.Email" .}}
                    <li>
                      EMAIL
                      <input type="email"  id="{{$field.Id}}" name="{{$field.Name}}" class="form-control " required value="{{if $field.Error}}{{$field.Error}}{{else}}{{$field.Value}}{{end}}"/>
                    </li>
                     {{end}}
                    <li>
                      QQ
                      {{with $field := field "message.QQ" .}}
                      <input type="text"  id="{{$field.Id}}" name="{{$field.Name}}" class="form-control" value="{{if $field.Error}}{{$field.Error}}{{else}}{{$field.Value}}{{end}}" />
                      {{end}}
                    </li>
                    <li>
                      个人主页
                      {{with $field := field "message.Url" .}}
                      <input type="text"  id="{{$field.Id}}" name="{{$field.Name}}" class="form-control" placeholder="Don't with http://  " value="{{if $field.Error}}{{$field.Error}}{{else}}{{$field.Value}}{{end}}"/>
                      {{end}}
                    </li>
                  </ul>
                  <div class="comment-input-text">
                    MESSAGE
                    <div>
                      {{with $field := field "message.Content" .}}
                      <textarea class="form-control ctextarea" id="{{$field.Id}}" name="{{$field.Name}}" required>{{if $field.Error}}{{$field.Error}}{{else}}{{$field.Value}}{{end}}</textarea>
                      {{end}}
                    </div>
                  </div>
                  <button type="submit" class="btn btn-success comment-input-text-btn">SUBMIT</button>
            </form>
         </div>
       </div>
       {{if .messages}}
       {{range $index,$message:=.messages}}
       {{if mo $index 2}}
       <div class="comment-cell">
         <pre ><code><span class="func-color">func</span><span class="func-name"> UserMessage</span><span class="pln">() </span><span class="pln">{</span><span class="pln-w">
    </span><span class="func-color">var</span><span class="pln-w"> </span><span class="pln">email </span><span class="func-type">string </span><span class="pln">=</span><span class="pln"> </span><span class="func-str">"{{$message.Email}}"</span><span class="pln">;</span>{{if $message.QQ}}<span class="pln-w">
    </span><span class="func-color">var</span><span class="pln-w"> </span><span class="pln">QQ </span><span class="pln">=</span><span class="pln-w"> </span><span class="func-str" >{{$message.QQ}}</span><span class="pln">;</span>{{end}}{{if $message.Url}}<span class="pln-w">
    </span><span class="func-color">var</span><span class="pln-w"> </span><span class="pln">url </span><span class="pln">=</span><span class="pln-w"> </span><a href="http://{{$message.Url}}" class="func-str" target="_blank">http://{{$message.Url}}</a><span class="pln">;</span>{{end}}<span class="pln-w">
    </span><span class="pln">Date </span><span class="func-color">:=</span><span class="pln-w"> </span><span class="func-str">"{{$message.CDate.Format "2006-01-02 15:04"}}"</span><span class="pln">;</span><span class="pln-w">
    </span><span class="func-color">var </span><span class="pln">Message</span><span class="pln-w"> </span><span class="pln">=</span><span class="pln-w"> </span><span class="func-str">"{{$message.Content}}"</span><span class="pln">;
</span><span class="pln">}</span><span class="pln-w">
</code></pre>
       </div>
       {{else}}
       <div class="comment-cell">
         <pre style="background:#2d2d2d;"><code><span class="func-color">func</span><span class="func-name"> UserMessage</span><span class="pln-w">() </span><span class="pln-w">{</span><span class="pln-w">
    </span><span class="func-color">var</span><span class="pln-w"> </span><span class="pln-w">email </span><span class="func-type">string </span><span class="pln-w">=</span><span class="pln-w"> </span><span class="func-str">"{{$message.Email}}"</span><span class="pln-w">;</span>{{if $message.QQ}}<span class="pln-w">
    </span><span class="func-color">var</span><span class="pln-w"> </span><span class="pln-w">QQ </span><span class="pln-w">=</span><span class="pln-w"> </span><span class="func-str" >{{$message.QQ}}</span><span class="pln-w">;</span>{{end}}{{if $message.Url}}<span class="pln-w">
    </span><span class="func-color">var</span><span class="pln-w"> </span><span class="pln-w">url </span><span class="pln-w">=</span><span class="pln-w"> </span><a href="http://{{$message.Url}}" class="func-str" target="_blank">http://{{$message.Url}}</a><span class="pln-w">;</span>{{end}}<span class="pln-w">
    </span><span class="pln-w">Date </span><span class="func-color">:=</span><span class="pln-w"> </span><span class="func-str">"{{$message.CDate.Format "2006-01-02 15:04"}}"</span><span class="pln-w">;</span><span class="pln-w">
    </span><span class="func-color">var </span><span class="pln-w">Message</span><span class="pln-w"> </span><span class="pln-w">=</span><span class="pln-w"> </span><span class="func-str">"{{$message.Content}}"</span><span class="pln-w">;
</span><span class="pln-w">}</span><span class="pln-w">
</code></pre>
       </div>
       {{end}}
       {{end}}
       {{end}}
  </div>
</div>
{{template "footer.html" .}}


呀,完成了把?完成了吗?看看其中的这段代码:


 {{if mo $index 2}}

恩,我们又加了一个自定义的方法,还记得上一个怎么做的吗?
打开app/init.go 在func init方法里添加:

revel.TemplateFuncs["mo"] = func(a, b int) bool { return a%b==0 }

这一次我们真的完成了,看效果:





交流QQ:158325682



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:jov123

查看原文:【Go web开发之revel+mgo】第6章 实现留言板功能

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2042 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传