0

I'v started my journey with Spring Framework, can't get 1 simple thing done tho.

I start my server and go to localhost:8080/ - I got HTTP status 404 and I need to add project name to actually see my home page, so url is like localhost:8080/projectname - now it shows me home.jsp

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.company.projectname")
public class ApplicationConfig {

    @Bean
     ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

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

        return viewResolver;
    }
}

public class MvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {ApplicationConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}


@Controller
public class HomeController {

    @GetMapping("/")
    public String home(){
        return "home";
    }
}

Code is just a basic configuration, on top of that there is Spring Security configuration, but I really dont think that is relevant here.

user9309329
  • 313
  • 3
  • 13

1 Answers1

-1

Actually, you're right, your configuration has nothing to do with that prefix to context path. You're probably using tomcat or some similar external container and it sets up prefix since it can host multiple different webapps at the same time.

If you're using tomcat - the easiest way is just to rename your projectname.war file inside webapps directory to ROOT.war and restart the container. Then you'll be able to access it like localhost:8080/.

Another way is to configure context path for your application withing $CATALINA_HOME/conf/server.xml:

<Context path="" docBase="projectname"></Context>

But in this case with default settings your application will be deployed twice, so don't forget to turn autodeploy feature off in the same file.

Bohdan Levchenko
  • 3,411
  • 2
  • 24
  • 28