使用基本标记时的相对URL斜杠 [英] Relative URL slash when using base tag

查看:97
本文介绍了使用基本标记时的相对URL斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当将HTML基本标记与相对URL组合使用时,它是否可以使用?漂亮的标准,以斜线结束基础href?

我问的原因是因为我经常需要用JSP内的上下文根(例如表单动作)来表达事物,以便表单被路由到适当的servlet,因为我已经在web.xml中映射了它。

大部分时间我会每次都使用 $ {pageContext.request.contextPath} ,直到我学会了关于基本标签。然而,我猜测,由于在JSP中,斜杠被服务器解释为Web应用程序的根,所以我不能拥有我的基础,例如:

 < base href =/ foo/> 

然后在表单中有:

 < form action =/ extension> 
...
< / form>

由于这会导致表单被提交到 mydomain.com/extension 而不是 mydomain.com/foo/extension 。我是否正确理解这一点,既然斜线有特殊含义,您通常需要用反斜杠结束基础HREF以获得所需效果?



谢谢

解决方案



浏览器不会简单地连接您的基 href 属性和相对URL。
相反,< base> 标签设置文档基址,然后用于动态生成新路径。



您可以在WHATWG HTML规范中看到一个有趣的例子:

 < base href =http://www.example.com/news/index.html> 
< p>访问< a href =archives.html>存档< / a>。< / p>

请注意, href 属性甚至以一个文件名。



这意味着如果你没有在你的基础 href 属性上加一个斜线, foo 将被视为路径的最后一个易变部分,并且将基本上被删除。举例来说:

 < base href =http://www.example.com/news/index.html /> 
< img src =ball.png/> <! - GET http://www.example.com/news/ball.png - >






 < ; base href =http://www.example.com/news//> 
< img src =ball.png/> <! - GET http://www.example.com/news/ball.png - >






 < ; base href =http://www.example.com/news/> 
< img src =ball.png/> <! - GET http://www.example.com/ball.png - >






 < ; base href =http://www.example.com//> 
< img src =ball.png/> <! - GET http://www.example.com/ball.png - >






 < ;  - 如果从http://www.example.com提供服务: - > 
< base href =//>
< img src =ball.png/> <! - GET http://www.example.com/ball.png - >


I'm trying to learn more about the way paths are interpreted with a JSP.

When using an HTML base tag in combination with a relative URL, is it pretty standard to end the base href with a slash?

The reason I am asking is because I often need to express things in terms of the context root within a JSP (for a form action for example), so that the form is routed to the appropriate servlet as I've mapped it in web.xml.

Most of the time I would just use ${pageContext.request.contextPath} every time, until I learned about the base tag.

However, I'm guessing that since in a JSP the slash is interpreted by the server as the root of the web application, I can't have my base be, for example:

<base href="/foo"/>

and then in the form have:

<form action="/extension">
...
</form>

Since this would cause the form to be submitted to mydomain.com/extension instead of mydomain.com/foo/extension. Am I understanding this correctly, that since slash has special meaning, you generally need to end the base HREF with a backslash to get the desired effect?

Thanks

解决方案

Not really.

Browsers do not simply concatenate your base href attributes and relative URLs. Instead, <base> tag sets the Document base URL which is then used to dynamically generate a new path.

You can see an interesting example in the WHATWG HTML specification:

<base href="http://www.example.com/news/index.html">
<p>Visit the <a href="archives.html">archives</a>.</p>

Note that the href attribute even ends with a filename.

This means that if you do not put a trailing slash to your base href attribute, foo will be treated as the last, volatile part of the path and will essentially be removed. By way of example:

<base href="http://www.example.com/news/index.html" />
<img src="ball.png" /> <!-- GET http://www.example.com/news/ball.png -->


<base href="http://www.example.com/news/" />
<img src="ball.png" /> <!-- GET http://www.example.com/news/ball.png -->


<base href="http://www.example.com/news" />
<img src="ball.png" /> <!-- GET http://www.example.com/ball.png -->


<base href="http://www.example.com/" />
<img src="ball.png" /> <!-- GET http://www.example.com/ball.png -->


<!-- If served from http://www.example.com: -->
<base href="/" />
<img src="ball.png" /> <!-- GET http://www.example.com/ball.png  -->

这篇关于使用基本标记时的相对URL斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆