2

I am a Spring newbie and am putting together a Spring web-app (not Spring-boot - how much difference does this make?). Deployment is on a Tomcat 7 server.

The app is up and running. My problem is that is only accessible via the standard URL:

http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html

The following do not work: http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/

even though my web.xml lists index.html as the welcome page.

An additional symptom of the problem is that all sorts of links within the application need to be specified as relative, rather than with a prepended '/'.

But even these urls are not what I really want.

I would rather specify

http://mycompany.com:8081/cwing

and have it bring up the index.html.

Following the lead of Add context path to Spring Boot application

I created an application.xml file in src/main/resources with the following content:

server.contextPath=/cwing
server.port=8081

This doesn't work. http://mycompany.com:8081/cwing brings up a blank page, with a 404 error on the server, as does http://mycompany.com:8081/cwing/index.html.

I must be missing something. What else is needed to get this to work as I want it to work?

UPDATE: as asked, here is the web.xml (irrelevant details removed).

    <?xml version="1.0"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
              version="3.0">
      <display-name>cwing</display-name>

        <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- The definition of the Root Spring Container shared by all Servlets 
            and Filters -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/spring/context.xml
            /WEB-INF/spring/datasource.xml
            /WEB-INF/spring/security.xml
            </param-value>
        </context-param>

        <servlet>
            <servlet-name>d</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/spring/context.xml
                    /WEB-INF/spring/datasource.xml
                    /WEB-INF/spring/security.xml
                </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
            <async-supported>true</async-supported>
        </servlet>

        <servlet-mapping>
            <servlet-name>d</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

        <!-- Disables Servlet Container welcome file handling. Needed for compatibility 
            with Servlet 3.0 and Tomcat 7.0 -->
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>

        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>

        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping> 
...
</web-app>
Community
  • 1
  • 1
Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • which server you are using? – Nikolay Rusev Jun 23 '15 at 14:15
  • I'm using Tomcat 7 and have edited original post to say so. – Steve Cohen Jun 23 '15 at 14:17
  • If you are directly deploying on tomcat, it will take the version name as indicated in your pom.xml. If you want to deploy it without that, change name of your war to ROOT.war, and paste the war in webapps folder, remove the ROOT folder, restart tomcat. – We are Borg Jun 23 '15 at 14:24
  • What is the problem with renaming the war?? If not, then you will have to rely on an IDE as far as I know to set the Context Path to / or whatever you want. – We are Borg Jun 23 '15 at 14:31
  • I'd rather retain that versioning info. – Steve Cohen Jun 23 '15 at 14:34
  • You can look at this [link](http://stackoverflow.com/questions/31000216/spring-boot-gradle-tomcat-setting-context-path-to-something-other-than-the-war) its about setting context path to other than the .war name. Using Gradle [Cargo plugin](https://github.com/bmuschko/gradle-cargo-plugin) – dok Jun 09 '16 at 12:32

1 Answers1

1

This is not related to Spring MVC but to specific server, by default Tomcat gets your WAR name as context root. If you want to change this, you can always rename your WAR file or I recommends to change your pom.xml to build your WAR file with final name, here is how to do this:

<build>
  <finalName>finalName</finalName>
 . . .
</build>
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
  • Well, I don't think that's the full answer. See above. Without renaming the war, note that I said that http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html works but http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT does not. Renaming the war will not help that situation, and I'd rather NOT rename the war, but modify the Tomcat default. How would I do that? – Steve Cohen Jun 23 '15 at 14:27
  • ohhh i got it what you want...let me think – Nikolay Rusev Jun 23 '15 at 14:29
  • add your web.xml or java web config to your question – Nikolay Rusev Jun 23 '15 at 14:31
  • may be you are missing `welcome-files-list` see : http://www.javatpoint.com/welcome-file-list – Nikolay Rusev Jun 23 '15 at 14:38
  • That's not it, see web.xml above now. – Steve Cohen Jun 23 '15 at 14:41
  • yeah, but you have spring security in your project. Did you whitelist your index.html in security config? and what error code return `mycompany.com:8081/cwing-0.0.3-SNAPSHOT` ? – Nikolay Rusev Jun 23 '15 at 14:46
  • I did have it whitelisted if by that you mean but that didn't help. I also tried commenting the security.xml from web.xml altogether but when I did not no urls, not even mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html worked after that. – Steve Cohen Jun 23 '15 at 15:32