2013-06-18 3 views
0

1) 템플릿 메소드를 통해 로그인 페이지를 렌더링합니다. 예는 :이 index.html을GO : 데이터를 데이터 저장소에 POST하는 방법?

입니다
{{ 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하지만 보여주고있다 빈 : & g : & {} 할 수있는 작업은 무엇입니까?

+0

변경 질문과 코드가 좋지 않다, 초점이 맞지 않는다! 다음과 같이 변경하십시오 :'fmt.Fprint (w, "% # v :", g, "\ n")' –

답변

0

진술을 fmt.Fprintf 문으로 대체하는 것이 좋습니다. 예를 들어

교체 :

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

당신은 문제가 발생할 수 있습니다 http.ResponseWriter에 직접 쓰고있어 :

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

와 함께.

+0

이제 작동 중입니다. 감사 – eegloo

관련 문제