這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
示範地址 http://gblog-revel.herokuapp.com/
1.實現留言板
我們在上一章做了簡單的設計,挑就留言的表單有了,那麼。。。。。
首先,在app/models下建立message.go 內容:
package modelsimport ("github.com/revel/revel""labix.org/v2/mgo/bson""time")type Message struct{Email stringQQ stringUrl stringCDate time.TimeContent 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 timemessage.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 controllersimport ("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 = 500return c.RenderError(err)}defer dao.Close()err = dao.InsertMessage(message)if(err!=nil){c.Response.Status = 500return 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 = 500return 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 }
這一次我們真的完成了,看效果:
源碼地址:https://github.com/joveth/GBlog
交流QQ:158325682