【Go web开发之revel+mgo】第6章 实现留言板功能
演示地址 http://gblog-revel.herokuapp.com/
1.实现留言板
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更加的相似。
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) }
恩,很熟系吧。
POST /putmessage WMessage.Putup
ok了吗,下面我们来显示它。
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}}
revel.TemplateFuncs["mo"] = func(a, b int) bool { return a%b==0 }
这一次我们真的完成了,看效果:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。