dongzha0813 2014-08-23 01:16
浏览 86

尝试在表单提交时呈现结果页面时,显示空白的html文档

The code below renders the initial page, which has a form in it. On submission of that form, I want to render a results page. The form submits and is handled, but all I see is a blank html document.

I don't want to only display an html page, but render it as some of the content will come from my golang code on form submission.I am trying to do this using templating ( from a template ) where designated lines in the <body></body> are the values of Golang variables.

I would greatly appreciate it if someone could help me figure out how to render a results page.

package main


import (
    //"fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"

    "html/template"
    "io/ioutil"
)


type Page struct {
     Title string
     Body []byte
}


func (p *Page) save() error{
     filename := p.Title + ".txt"
     return ioutil.WriteFile(filename, p.Body, 0600) 
}


func loadPage(title string) (*Page, error){
     filename := title + ".txt"
     body, err := ioutil.ReadFile(filename)
     if err != nil{
          return nil, err
     }
     return &Page{Title: title, Body: body}, nil 
}


func renderTemplate(w http.ResponseWriter, tmpl string, p *Page){
     t, _ := template.ParseFiles("Projects/Go/src/web/site/" + tmpl + ".html")
     t.Execute(w, p) 
}


func editHandler(w http.ResponseWriter, r *http.Request){
     title := r.URL.Path[len("/edit/"):]
     p, err := loadPage(title)
     if err != nil{
         p = &Page{Title: title}
     }
     renderTemplate(w, "edit", p)
}


func viewHandler(w http.ResponseWriter, r *http.Request){
     title := r.URL.Path[len("/ask"):]
     p, _ := loadPage(title)
     renderTemplate(w, "ask", p)
}


func response(c web.C, w http.ResponseWriter, r *http.Request){
     //name := r.FormValue("name")
     //fmt.Fprintf(w, "Hello, %s!", name)

     http.HandleFunc("/ask", viewHandler)
     http.HandleFunc("/edit/", editHandler)
     //http.HandleFunc("/save", saveHandler)
     http.ListenAndServe("8000", nil)
}


func serveSingle(filename string) func(w http.ResponseWriter, r *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, filename)
    }
}


func main() {
     goji.Get("/", serveSingle("Projects/Go/src/web/site/index.html"))
     goji.Handle("/ask", response)
     goji.Serve()
}

file structure:

root/Projects/Go/src/web/site/edit.html

root/Projects/Go/src/web/site/index.html

root/Projects/Go/src/web/site/view.html

index.html's body:

<form action="ask" method="get">
    <input type="text" name="q" />
</form>

view.html's body:

<form action="ask" method="get">
    <input type="text" name="q" />
</form>
<h1 class="abTitle">{{printf "%s" .Body}}</h1>

view.html and edit.html are the exact same.

  • 写回答

1条回答 默认 最新

  • douduan3203 2014-08-23 02:04
    关注

    Theoretically, the script you published should respond on the / requests and crash on requests to the /ask. On the request to the /ask it should execute the response handler and panic (as the documentation specifies http://golang.org/pkg/net/http/#ServeMux.Handle But what exactly will happen is quite unclear. Anyway, it will be more idiomatic (for such small server) to configure all the handlers in advance and avoid registering handlers in responses.

    Another issue is in the viewHandler function - it will be called with /ask request only, but then the /ask will be stripped and the title will be "" - this will lead to loading ".txt" template file, which is probably not intentional.

    It will be also helpful if you post the content of your form template and files structure (folders and file names).

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端