2013-06-18 27 views
0

1)通過模板方法渲染登錄頁面。 例:這是index.html的GO:如何將數據發佈到數據存儲?

{{ define "title" }}Guestbook{{ end }} 

    {{ define "content" }} 
     <form action="/login" method="post"> 
      <div><label>UserName : </label><input name="username" type="text" /></div> 
      <div><label>Password : </label><input name="password" type="password" /></div> 
      <div><input type="submit" value="login"></div> 
     </form> 

    {{ end }} 

2)hello.go文件:

package main 

import (
    "fmt" 
    "html/template" 
    "net/http" 
) 

var index = template.Must(template.ParseFiles(
    "templates/base.html", 
    "templates/index.html", 
)) 
type UserLogin struct{ 
    UserName string 
    PassWord string 
} 


func handler(w http.ResponseWriter, r *http.Request) { 
    index.Execute(w, nil)  
} 



/*func login(w http.ResponseWriter, r *http.Request) { 
     remPartOfURL := r.URL.Path[len("/login/"):] 
     if r.Method == "POST" { 
      fmt.Fprintf(w, "after",r.FormValue("username"))  
     } 
     fmt.Fprintf(w, "Hello %s!", remPartOfURL) 
    }*/ 
    func login(w http.ResponseWriter, r *http.Request) {  
     fmt.Fprint(w, "login : ", "\n") 
    remPartOfURL := r.URL.Path[len("/login/"):]  
    if r.Method == "POST" {   
     g := UserLogin{ 
      UserName: r.FormValue("username"), 
      PassWord: r.FormValue("password"), 
     } 
     fmt.Fprint(w, "&g : ", &g,"\n") 
    } 
    fmt.Fprintf(w, "Hello %s!", remPartOfURL) 
    } 


func init() { 
    http.HandleFunc("/", handler) 
    http.HandleFunc("/login/", login) 
} 

問題:點擊登錄按鈕後:&摹應打印的用戶名和密碼值卻是露出空:& g:& {} 可以做些什麼?

+0

更改問題和代碼不好,焦點不準! 將其更改爲:'fmt.Fprint(w,「%#v:」,g,「\ n」)' –

回答

0

您可能會嘗試的一件事是用http.Error語句替換fmt.Fprintf語句。

例如更換:

fmt.Fprintf(w, "after",r.FormValue("username")) 

有:

http.Error(w, fmt.Sprintf("after %s", r.FormValue("username")), http.StatusOK) 

你直接寫入http.ResponseWriter這可能會導致問題。

+0

現在正在工作。謝謝 – eegloo