由于路径差异java,spring mvc,velocity找不到JS / CSS文件 [英] JS/ CSS files not found due to path differences java, spring mvc, velocity

查看:229
本文介绍了由于路径差异java,spring mvc,velocity找不到JS / CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的目录结构:

webapp
 - resources
   - custom.js
   - base.css
 - WEB-INF
 - views
   - layout.vm
   - top.vm
   - footer.vm
   - index.vm
   - FolderA
     - restricted.vm

我的layout.vm是:

My layout.vm is:

#parse('top.vm')
$screen_content
#parse('footer.vm')

我的top.vm包括这些JS和CSS文件:

My top.vm consists include these JS and CSS files:

<link href="resources/base.css" rel="stylesheet">
<script src="resources/custom.js"></script>

现在,当我访问 http://www.example.com:8080/index 页面,我看到找到了JS和CSS文件。
但是当我访问 http://www.example.com:8080/FolderA/restricted ,找不到JS和CSS文件。当它在顶部我改变它时它起作用

Now when my I access http://www.example.com:8080/index page, I see the JS and CSS files getting found. But when I visit http://www.example.com:8080/FolderA/restricted, JS and CSS files are not found. It works when in top I change it

然后索引页面坏了。

我使用spring控制器返回适当的视图。
例如以下命令返回索引页:

I am using spring controllers to return the appropriate views. For e.g. The following returns the index page:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {       
    return "index";
}

这将返回受限制的视图:

and this returns the restricted view:

@Controller
@RequestMapping(value = "/FolderA")
public class SomeController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String secured(Model model) {

        return "FolderA/restricted";
    }

有关如何解决此问题的任何建议吗?

Any suggestions as to how I can fix this issue?

另一方面,我在使用常规jsp文件时遇到了这个问题,但我设法使用< c:url ..>来修复它。 。我不知道这如何解决速度问题。

On a side note, I was facing this issue when using regular jsp files as well, but I managed to fix it using <c:url ..>. I dont know how this can solved in velocity.

推荐答案

那是因为您声明的资源是相对于用于访问的网址风景。
您必须将所有静态资源URL更改为绝对
您可以使用标签

thats because the resources you have declared are relative to the url used for accessing the view. You have to change all the static resource urls to absolute You can do this using tag

从此更改

<script src="resources/custom.js"></script>

<script src="<c:url value="/resources/custom.js"/>"></script>

这应该在HTML中产生类似的东西

this should generate something like this in the HTML

<script src="<c:url value="http://www.example.com:8080/FolderA/resources/custom.js"/>">
</script>

注意
但为此,你应该加上
%@ taglib uri =http://java.sun.com/jsp/jstl/coreprefix =c%> 到您的jsp
并包含 jst标签库进入webapp

Note But for this to work you should add %@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> to your jsp and include jst tag library into the webapp

编辑 - -----

对于JS和CSS文件中引用的路径,有一个解决方法。使用基本标记
定义HTML中的绝对路径标记需要使用

For the paths referred inside in JS and CSS files , there is a work around.Use base tag to define the absolute path tags in the HTML need to use

<base href="http://www.example.com:8080/FolderA/">

这篇关于由于路径差异java,spring mvc,velocity找不到JS / CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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