最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Spring-Maven: Unable to load Javascript file - Stack Overflow

programmeradmin2浏览0评论

Following is the folder structure of my application,

I have created a resource folder under webapp which has resource folders for CSS and JS

In the servlet-context file I have mapped the resources to resources/inspiremetheme as below,

<mvc:resources mapping="/resources/**" location="/resources/inspiremetheme/" />

I have included in JSP page as below,

<head>
    <script type="text/javascript" src="/resources/js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="/resources/js/inspireme.js"></script>
    <link type="text/css" rel="stylesheet" href="<c:url value="resources/css/global.css"/>" />
</head>

The problem here is I am not able to load my JS files where as the CSS file is loaded properly. I was not able to understand what I have missed.

Update - 1: The file structure after unzipping is as below,

Inspireme 
   |
   |------ META-INF
   | 
   |------resources --> inspiremetheme --> css and js folders
   |
   |------ WEB-INF --> classes, jsp, lib, web.xml

Following is the folder structure of my application,

I have created a resource folder under webapp which has resource folders for CSS and JS

In the servlet-context file I have mapped the resources to resources/inspiremetheme as below,

<mvc:resources mapping="/resources/**" location="/resources/inspiremetheme/" />

I have included in JSP page as below,

<head>
    <script type="text/javascript" src="/resources/js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="/resources/js/inspireme.js"></script>
    <link type="text/css" rel="stylesheet" href="<c:url value="resources/css/global.css"/>" />
</head>

The problem here is I am not able to load my JS files where as the CSS file is loaded properly. I was not able to understand what I have missed.

Update - 1: The file structure after unzipping is as below,

Inspireme 
   |
   |------ META-INF
   | 
   |------resources --> inspiremetheme --> css and js folders
   |
   |------ WEB-INF --> classes, jsp, lib, web.xml
Share Improve this question edited Jul 3, 2014 at 13:40 El Guapo 5,7817 gold badges58 silver badges88 bronze badges asked Jul 3, 2014 at 13:28 AmarnathAmarnath 8,86510 gold badges57 silver badges84 bronze badges 7
  • Have you checked your .war file? Try unzipping (unjarring) it to see how it was built. – El Guapo Commented Jul 3, 2014 at 13:30
  • What about that "inspiremetheme" directory between "resources" and "js"? Your path doesn't include that. – Pointy Commented Jul 3, 2014 at 13:32
  • @Pointy I gave the path in the Spring context file. <mvc:resources mapping="/resources/**" location="/resources/inspiremetheme/" /> – Amarnath Commented Jul 3, 2014 at 13:34
  • Yes, but the <script> tag doesn't include it. The browser does not have any idea about your Spring configuration. – Pointy Commented Jul 3, 2014 at 13:39
  • @ElGuapo I have updated the file structure after unzipping. Please have a look. – Amarnath Commented Jul 3, 2014 at 13:41
 |  Show 2 more ments

2 Answers 2

Reset to default 4

You messed <c:url value=".."/> into script tags..

<c:url value="/resources/js/jquery-1.11.1.js" var="myUrl" />
<script type="text/javascript" src="${myUrl}"/><!-- ment --></script>

Also put a ment inside : this will prevent the jspx parser from minimizing the tag.

If parser minimize your tag you will have as result:

<script type="text/javascript" src="/WEBAPP/resources/js/jquery-1.11.1.js"/> 

and not

<script type="text/javascript" src="/WEBAPP/resources/js/jquery-1.11.1.js"></script>

This can cause some browser not working

If you decide to use Java config instead of xml, than go for the following solution.

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/jsp/", ".xml");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/inspiremetheme/css/**", "/inspiremetheme/js/**")
                .addResourceLocations("classpath:/inspiremetheme/css/", "classpath:/inspiremetheme/js/");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
发布评论

评论列表(0)

  1. 暂无评论