html - 无法在Go中将图像添加到html模板

标签 html eclipse go goclipse

首先,我使用 Notepad++ 创建了一个 HTML 文件,代码如下:

<body>
    <table>
        <tr>
            <td>Jill</td>
            <td>Smith</td>
            <td><img src="images/test.jpg" border=3 height=100 width=300 /></td>
        </tr>
        <tr>
            <td>Eve</td>
            <td>Jackson</td>
            <td>94</td>
        </tr>
    </table>
</body>

当我打开这个文件时它工作正常,它显示了表格和单元格内的图像。

然后,在我的 Eclipse 内的 Go 项目中,我使用相同的代码创建了一个 HTML 文件并尝试运行但它没有工作,它没有显示图像。

因此,我尝试将图像放在与 HTML 文件相同的文件夹中,但没有成功。

两者都在文件夹中:tmpl

<body>
    <table>
        <tr>
            <td>Jill</td>
            <td>Smith</td>
            <td><img src="test.jpg" border=3 height=100 width=300 /></td>
        </tr>
        <tr>
            <td>Eve</td>
            <td>Jackson</td>
            <td>94</td>
        </tr>
    </table>
</body>

问题:

rootHandler:无法进一步转发对/tmpl/teste.jpg 的请求。

执行代码:

// * /
func rootHandler(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
        homeHandler(w, r)
    } else {
        log.Printf("rootHandler: Could not forward request for %s any further.", r.RequestURI)

        errNotFound(w, r)
    }
}

最佳答案

在项目中添加一个文件服务器:

m.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("images/"))))

关于html - 无法在Go中将图像添加到html模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25767931/

相关文章:

java - 通过组织导入,Eclipse 使用什么来决定非指定包的导入顺序?

asp.net - 将数据绑定(bind)到asp.net中的html5 DataList

javascript - 我的 JS 文件中的 HTML - 意外 token < 在 loadash.js 的第 1 行

javascript - 使用 Ajax/jQuery/PHP 发送联系表单数据

go - 在go中制作文件名截断符

ubuntu - 如何在 Win 10 中使用 WSL Ubuntu Bash 开发 Go App

go - 检查 array[key] 是否设置?抛出 : index out of range

javascript - 如何在另一个页面中突破一个对象框?

eclipse - Eclipse EE Helios 中的外部 Web 模块

eclipse - 在Eclipse中仅格式化选定的代码