1

I have a demo application of Spring MVC - I just follow udemy course for it.

I have created the first controller and view. All is working fine, however I have one doubt about. The pom file of application contains:

  <groupId>eu.smartgroup</groupId>
  <artifactId>spring-demo-mvc</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

So when I run application on tomcat server it starts, but it is available with url: http://localhost:8080/spring_demo_mvc_war

Is there possibility to configure application so it will be available in the root path: http://localhost:8080/ (without project name after slash)?

Edit: Here is full application.yml

server:
  servlet:
    contextPath: /

HelloController.java

package eu.test.springdemo.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/")
public class HelloController {

    @GetMapping("/")
    public String showPage() {
        return "main-menu";
    }
}

DemoAppConfig.java

package eu.test.springdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="eu.test.springdemo")
public class DemoAppConfig implements WebMvcConfigurer {

    // define a bean for ViewResolver

    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}
zleek
  • 201
  • 4
  • 16
  • Since, you are using Tomcat, I wonder if the base path is defined at the server level. See here: https://stackoverflow.com/q/5328518/9698467 – Stefan Zhelyazkov Nov 03 '20 at 20:24
  • 1
    The preferable approach is to use Spring Boot, which is the modern way of developing applications. If you absolutely can't, you have to install your application as the `ROOT` application in Tomcat. – chrylis -cautiouslyoptimistic- Nov 03 '20 at 20:29
  • Also if you are using a standalone tomcat for your war file, context path you have to configure at server level. – Ajay Kumar Nov 04 '20 at 04:12

2 Answers2

0

Add this line to your application.properties:

server.servlet.contextPath=/

I've tried it on my project and it works.

Write back is it working on your.

fr3ddie
  • 406
  • 6
  • 17
  • I have application.yml file, so I've added: ``` server: servlet: contextPath: / ``` but it is not working :( – zleek Nov 03 '20 at 19:55
  • 1
    Oh sorry you're using Spring without Boot. Wait. – fr3ddie Nov 03 '20 at 19:56
  • Code is attached to query – zleek Nov 03 '20 at 20:16
  • Okay.. is it possible to run your code without this one dependency in `pom.xml`? May in this dependency is predefined path. – fr3ddie Nov 03 '20 at 20:22
  • which dependecy do you mean ? – zleek Nov 03 '20 at 20:27
  • Does not matter, at first glance I've thought that first block of code is an dependency. Look ad comment from ` chrylis -cautiouslyoptimistic-` below your question. According to his comment `you have to install your application as the ROOT application in Tomcat`. And if you can, change to Spring Boot. ;) – fr3ddie Nov 03 '20 at 20:45
0

Check if you have a controller class that is annotated as such:

@Controller
@RequestMapping("spring_demo_mvc_war")

or

@RestController
@RequestMapping("spring_demo_mvc_war")

If that is the case, then make the string empty or remove the @RequestMapping (you can also change it to whatever else you like).

Dharman
  • 30,962
  • 25
  • 85
  • 135
Stefan Zhelyazkov
  • 2,599
  • 4
  • 16
  • 41