0

I am trying to deploy a Spring MVC application at root in Tomcat8. At the moment, the only way I can access my app is at my_domain_name.com/my_webapp, where my_webapp is the name of my war file; whereas I would like to access it at just my_domain_name.com.

I know there are other similar questions on this topic but I've tried everything and only renaming my war file to ROOT.war worked, but this is not ideal for me as I want to run multiple apps from the same server.

My server.xml includes:

<Context path="" docBase="my_webapp" debug="0" reloadable="true">
</Context>
<Host name="my_domain_name.com"  appBase="my_webapp" 
    unpackWARs="true" autoDeploy="true">
    <Valve className="org.apache.catalina.valves.AccessLogValve" 
        directory="logs" prefix="my_webapp_access_log" suffix=".txt"  
        pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

I have a ROOT.xml file in the conf directory:

<Context 
  docBase="my_webapp" 
  path="" 
  reloadable="true"/>

My context.xml under META-INF in my_webapp:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/"/>

Configuration file AppInitializer.java in my_webapp:

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext rootContext = new 
            AnnotationConfigWebApplicationContext();

        rootContext.register(AppConfig.class);

        container.addListener(new ContextLoaderListener(rootContext));

        AnnotationConfigWebApplicationContext dispatcherContext = 
            new AnnotationConfigWebApplicationContext();


        ServletRegistration.Dynamic dispatcher = 
            container.addServlet("dispatcher", new 
                DispatcherServlet(dispatcherContext));

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

Configuration file DefaultView in my_webapp:

public class DefaultView extends WebMvcConfigurerAdapter{

    @Override
    public void addViewControllers( ViewControllerRegistry registry ) {

        registry.addViewController( "/" ).setViewName(
            "forward:/home.html" );
        registry.setOrder( Ordered.HIGHEST_PRECEDENCE );

        super.addViewControllers( registry );
    }
}

I think that's all the relevant files, but if you think it may be something else let me know. I don't know if there maybe something wrong with my Spring MVC configuration and/or Tomcat configuration. Many thanks in advance.

Edit: this question is not a duplicate - I am not asking how to deploy multiple webapps from different contexts as such. I would like my webapp to be deployed properly at root, so I should be just entering my www.myapp.com instead of www.myapp.com/myapp. I want to do this without naming my war file to ROOT.war - I have tried adding tags but this does not seem to work. I have also created a ROOT.xml and this is also not working. Quite clearly not a duplication of the question.

Rufus
  • 3
  • 3
  • How should tomcat know which application to call, if you want to have all applications to have no prefix? Therefore the first segment of the URI specifies the application. – Stimpson Cat Apr 28 '17 at 09:25
  • Can I not just put multiple elements in my server.xml linking a war file to a specific request? – Rufus Apr 28 '17 at 09:28
  • Possible duplicate of [Is it possible to have one appBase served by multiple context paths in Tomcat?](http://stackoverflow.com/questions/112480/is-it-possible-to-have-one-appbase-served-by-multiple-context-paths-in-tomcat) – Vipin CP Apr 28 '17 at 09:46
  • Not really. My primary question is regarding deploying my file as root without using the ROOT.war solution, which I've tried by adding tags to my server.xml and does not work (see code above). Multiple apps, as far as I'm aware can be added by repeating tags for each domain, specifying which war file you want Tomcat to serve. – Rufus Apr 28 '17 at 09:52

1 Answers1

1

I think what you have done is correct. As you said there are 2 options for this option

1) Rename the war.

2) Overwrite the root context

How to set the context path of a web application in Tomcat 7.0

Deploying my application at the root in Tomcat

Community
  • 1
  • 1
Tharsan Sivakumar
  • 6,351
  • 3
  • 19
  • 28