Web复习

本文介绍了Go语言进行Web开发的相关知识,包括如何使用单控制器和多控制器处理HTTP请求,以及如何获取请求头和请求参数。通过示例展示了多处理器的实现,使得不同URL可以绑定到不同的处理器。此外,还讲解了如何利用html/template包展示HTML模板,并提供了一个简单的HTML模板显示示例。最后,提到了项目结构,强调了标准的MVC思想,并给出了HTML模板和静态资源的组织方式。
摘要由CSDN通过智能技术生成
package main

import "net/http"

type MyHander struct {

}

func (m *MyHander) ServeHTTP(w http.ResponseWriter,r *http.Request) {
	w.Write([]byte("这是复习的网页"))
}
func main() {
	h:=MyHander{}
	server:=http.Server{Addr:"localhost:8030",Handler:&h}
	server.ListenAndServe()
}


在这里插入图片描述
搜索localhost:8030/first时,很惊喜,搜索结果依然是这个:
在这里插入图片描述
使用单控制器时,无论你在网址后面加什么,网站都是只能解析localhost:8090,搜索的结果是一样的。但是在实际中,单控制器用的还是没有多控制器多,接下来看一下多控制器:
2.多控制器

  • 在实际开发中大部分情况是不应该只有一个控制器的,不同的请求应该交给不同的处理单元.在Golang中支持两种多处理方式
    • 多个处理器(Handler)
    • 多个处理函数(HandleFunc)
  • 使用多处理器
    • 使用http.Handle把不同的URL绑定到不同的处理器
    • 在浏览器中输入http://localhost:8090/myhandler或http://localhost:8090/myother可以访问两个处理器方法.但是其他URl会出现404(资源未找到)页面

首先添加Myhandle结构体:

func (m *MyHandle) ServeHTTP(w http.ResponseWriter,r *http.Request) {
	w.Write([]byte("第一次复习的网页"))
}
func (m *MyHander) ServeHTTP(w http.ResponseWriter,r *http.Request) {
	w.Write([]byte("第二段"))
}

所有代码:

package main

import "net/http"

type MyHander struct {

}
type MyHandle struct {

}
func (m *MyHandle) ServeHTTP(w http.ResponseWriter,r *http.Request) {
	w.Write([]byte("第一次复习的网页"))
}
func (m *MyHander) ServeHTTP(w http.ResponseWriter,r *http.Request) {
	w.Write([]byte("第二段"))
}
func main() {
	h:=MyHander{}
	h2:=MyHandle{}
	server:=http.Server{Addr:"localhost:8090"}
	http.Handle("/first",&h)
	http.Handle("/second",&h2)
	server.ListenAndServe()
}

当你搜索localhost:8030/second时,返回的将是handle的数据:
在这里插入图片描述
2.获取请求头和请求参数
2.1.获取请求头

  • 在浏览器地址栏中输入下面信息,这属于http请求的get方式,请求携带两个参数
    • 可以使用http.Request.Header获取请求(Request Headers信息)
package main

import "fmt"
import "net/http"

func param(res http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(res, "第一个")
	//req.Header中Header本质是:type Header map[string][]string
	header:=req.Header
	fmt.Fprintln(res,"Header全部数据:",header)
	//为了让各位同学便读写代码,刻意明确给定类型
	var acc []string =header["Accept"]
	for _,n:=range acc{
		fmt.Fprintln(res,"Accepth内容:",n)
	}
}

func main() {
	server := http.Server{
		Addr: "localhost:8030",
	}
	http.HandleFunc("/param", param)
	server.ListenAndServe()
}

在这里插入图片描述

2.2获取请求参数

  • 请求参数可以一次全部获取也可以按照名称获取
package main

import "fmt"
import "net/http"

func param(res http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	fmt.Fprintln(res,req.Form)
	/*
	按照请求参数名获取参数值
	根据源码,FormValue(key)=req.Form[key]
	 */
	name:=req.FormValue("name")
	age:=req.FormValue("age")
	fmt.Fprintln(res,name,age)
}

func main() {
	server := http.Server{
		Addr: "localhost:8030",
	}
	http.HandleFunc("/param", param)
	server.ListenAndServe()
}

这里的参数是姓名zhang和年龄18
3.HTML模板和静态资源
3.1
项目结构

  • 在Go语言中web项目标准结构如下
  • –项目名
    –src
    –static
    –css
    –images
    –js
    –view
    –index.html
    –main.go
    • Go语言标准库中html/template包提供了html模版支持,把HTML当作模版可以在访问控制器时显示HTML模版信息

    • 这也符合标准的MVC思想

    • 3.2HTML模版显示

  • 使用template.ParseFiles()可以解析多个模版文件
    • 代码演示,显示index.html信息
    • 因为配置的pattern为"/"所以资源路径任意,都可以访问到这个HTML
package main

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

func welcome(w http.ResponseWriter,r *http.Request)  {
	t,_:=template.ParseFiles("view/index.html")
	t.Execute(w,nil)
}
func main() {
	server:=http.Server{Addr: ":8090"}
	http.HandleFunc("/",welcome)
	server.ListenAndServe()
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是要显示的html页面信息
</body>
</html>
package main

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

func welcome(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("view/index.html")
	t.Execute(w, nil) //第二个参数表示向模版传递的数据
}

func main() {
	server := http.Server{Addr: ":8090"}
	/*
		访问url以/static/开头,就会把访问信息映射到指定的目录中
	*/
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.HandleFunc("/", welcome)
	server.ListenAndServe()
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值