duanli5662 2015-10-25 15:21
浏览 40
已采纳

具有命名参数的Martini路由无法加载静态文件

I wrote my first Go application with Martini. I have route with named parameter:

m := martini.Classic()

staticOptions := martini.StaticOptions{Prefix: "assets"}
m.Use(martini.Static("assets", staticOptions))

m.Get("/edit/:id", editHandler)
m.Run()

The editHandler renders edit template with bootstrap stylesheet and script which lie in assets/css and assets/js folders accordingly.

<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css">
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>

But when I open edit page in my browser my static files don't load, because browser makes requests to edit/assets/css/bootstrap.min.css and edit/assets/js/bootstrap.min.js. How can I strip any route prefix?('edit', 'show' and others)

  • 写回答

1条回答 默认 最新

  • duanjiao5082 2015-10-25 15:49
    关注

    The links in your supplied html snippet are relative links. This means that when the browser resolves the URL it appends the given path to the current host and path ie, http://<hostname_and_port>/edit + assets/css/bootstrap.min.css.

    You can use a root path instead of a relative path to ensure that when the browser resolves urls it appends the given path to the root of the host no matter what the full URL path is. This is done by putting a forward slash at the beginning of your path. In the following example I have added a forward slash to the src and href attribute paths.

    <link rel="stylesheet" type="text/css" href="/assets/css/bootstrap.min.css">
    <script type="text/javascript" src="/assets/js/bootstrap.min.js"></script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行