简体   繁体   中英

Deploy Spring MVC app to run at root in Tomcat8

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM