jsp中的绝对URL

时间:2009-07-24 03:14:28

标签: jsp url jsf

在我的jsps中包含css,js和图像时,我遇到了解决相对网址的问题。刷新页面或单击后退按钮时会更改URL。我想问题的一个解决方案是使用绝对URL包含外部文件。但我无法找到如何使用相对URL的引用并使用它。有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:11)

这也可以工作,可能更容易阅读并具有一些额外的好处,例如正确的转义和可选的参数包含

  

<link href="<c:url value="/style/style.css"/>" rel="stylesheet" type="text/css" />

答案 1 :(得分:7)

这是你要找的吗?

${pageContext.servletContext.contextPath}

然后在你的jsp:

<link rel="stylesheet" href="${pageContext.servletContext.contextPath}/css/page.css"   media="all"    type="text/css" />

答案 2 :(得分:1)

回答了这个古老的问题,因为使用反向代理的绝对URL对javascript很有用。同样也适用于href。但是,在我们的例子中,反向代理处理href网址,因此相对路径就可以了。

要获取原始问题的绝对网址,请尝试以下操作:

        local block = script.Parent
        local debounce = true

block.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
    if humanoid and debounce == true then
        debounce = false
        block.Transparency = 0.1
        wait(0.2)
        block.Transparency = 0.2
        block.CanCollide = true
        wait(0.2)
        block.Transparency = 0.3
        block.CanCollide = true
        wait(0.2)
        block.Transparency = 0.4
        block.CanCollide = true
        wait(0.2)
        block.Transparency = 0.5
        block.CanCollide = true
        wait(0.2)
        block.Transparency = 0.6
        block.CanCollide = true
        wait(0.2)
        block.Transparency = 0.7
        block.CanCollide = false
        wait(0.2)
        block.Transparency = 0.8
        block.CanCollide = false
        wait(0.2)
        block.Transparency = 0.9
        block.CanCollide = false
        wait(0.2)
        block.Transparency = 1
        block.CanCollide = false
        wait(3)
        block.Transparency = 0
        block.CanCollide = true
        debounce = true

replace函数采用requestURL,即:http:/mydomain.com/context-path/index.html

并替换requestURI:/context-path/index.html

仅包含上下文路径:/ context-path

结果在:http:/mydomain.com/context-path

然后附加所需的资源:/style/style.css