【发布时间】:2022-01-24 19:21:54
【问题描述】:
我正在玩 Go,但我似乎无法用 CSS 打开页面,当我在 Go 之外打开 HTML 文件时,CSS 工作得非常好,但是当我通过 Go 运行它时,它永远不会包括在内。
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
_ "github.com/go-sql-driver/mysql"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //Parse url parameters passed, then parse the response packet for the POST body (request body)
// attention: If you do not call ParseForm method, the following data can not be obtained form
fmt.Println(r.Form) // print information on server side.
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello astaxie!") // write data to response
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
t, _ := template.ParseFiles("Add-Vehicle.html")
t.Execute(w, nil)
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.Form["select"])
fmt.Println("password:", r.Form["select-1"])
fmt.Println("username:", r.Form["year"])
fmt.Println("password:", r.Form["text"])
fmt.Println("username:", r.Form["dayrate"])
}
}
func main() {
http.HandleFunc("/", sayhelloName) // setting router rule
http.HandleFunc("/login", login)
http.Handle("/css/", http.FileServer(http.Dir("./css/")))
err := http.ListenAndServe(":9090", nil) // setting listening port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
这也是我的 html 文件中的 href
<link rel="stylesheet" href="css/nicepage.css" media="screen">
<link rel="stylesheet" href="css/Add-Vehicle.css" media="screen">
有人能帮我解决这个问题吗?
【问题讨论】:
-
直接打开会看到什么?
http://localhost:9090/css/nicepage.css -
使用绝对路径
标签: go