1)通过模板方法呈现登录页面。例如:这是 index.html{{ 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 mainimport (  "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 应该打印用户名和密码值,但显示为空: &g : &{ } 可以做什么?
                    
                    
                1 回答
 
                    
                    
                            HUH函数
                            
                                
                            
                        
                        
                                                
                    TA贡献1836条经验 获得超4个赞
您可以尝试的一件事是用fmt.Fprintf语句替换http.Error语句。
例如替换:
fmt.Fprintf(w, "after",r.FormValue("username"))和:
http.Error(w, fmt.Sprintf("after %s", r.FormValue("username")), http.StatusOK)您正在直接写入http.ResponseWriter可能会导致问题的内容。
- 1 回答
- 0 关注
- 253 浏览
添加回答
举报
0/150
	提交
		取消
	