I am using Spring mvc + maven + tomcat. When I write a java web app project in NetBeans, I periodically launch it with the F6 button (Run Project). That is, I do not make it explicitly deployed on Tomkat. With this, I want to check the changes just made. And it became a bo-oh-oh big mistake for me. It backfired. There is such a folder-file structure:

alt text

In form.jsp, I invoke my files like this:

<link rel="stylesheet" type="text/css" href="/css/css1.css"> <script type="text/javascript" src="/js/jquery-2.1.3.js"> </script> 

And of course, in the config of the spring I registered the paths, so that the spring could find static resources:

 <mvc:resources mapping="/css/**" location="/WEB-INF/css/" /> <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> 

The essence of the problem:
When I run the F6 button everything works fine. All JS, CSS are connected as it should. But when I do the mvn package and throw out the finished war in the Tomskata webapps folder, it does not find these static files. Only the bare form.jsp is opened without any CSS, JS. What to do?

The second problem : it is very strange that when you start F6, the URL path in the browser is not the same as when you start the war. If I run F6, the path is http://localhost:8080/spring-chat-test/
And if I run Closed, then http://localhost:8080/spring-chat-test-1.0/

  • Are the resources in the mavena in the build? Are they physically present in the unpacked war? Closed, I suppose, takes a version from the name of the cook. - etki
  • Unpacked .war - files CSS, JS is. In the mavena in the build did not register. maven.apache.org/plugins/maven-war-plugin/examples/... as understood from here - external resources should be added to the build, and webapp resources should be invested themselves, which is what happens - arg

2 answers 2

This is decided using jstl:

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

They add context (path) from where the web application is installed.

    Try adding the application context to JS scripts and stylesheets.

     <link rel="stylesheet" type="text/css" href="${pageContext.servletContext.contextPath}/css/css1.css"> <script type="text/javascript" src="${pageContext.servletContext.contextPath}/js/jquery-2.1.3.js"> </script> 

    Concerning the second problem. Most likely in NetBeans the context of / spring-chat-test is explicitly specified in the project settings, while in Tomcata context == the name of the folder into which the application was located (in your case, spring-chat-test-1.0). The simplest thing is to configure maven to collect war with the correct name. For example:

     <project> ... <build> <finalName>spring-chat-test</finalName> </build> </project>