0

My Spring release is 5.x and tomcat version is 8.5, so according to introduction, they will support the web application running without web.xml, but I got 404 error, see my project structure below:

enter image description here

I use this url to access my application, but got 404 error:
http://localhost:8080/SpringMVC/

see my code below:

RootConfig.java:

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages= {"spitter"},
               excludeFilters= {@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)}
              )
public class RootConfig {

}

WebConfig.java:

package spittr.config;

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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig implements WebMvcConfigurer{

    public ViewResolver viewResolver()
    {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) 
    {
        configurer.enable();
    }
}

SpittrWebAppInitializer.java:

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {RootConfig.class}; 
    }

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

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

}

HomeController.java:

package spittr.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
    @RequestMapping(value="/",method=RequestMethod.GET)
    public String home()
    {
        System.out.println("test");
        return "home";
    }
}

home.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>this is the Home Jsp page</title>
</head>
<body>
    <h1>Hello, Spring MVC!</h1>
</body>
</html>

from the Eclipse console, I can see output "test" that means spring context find out the controller, but seems it cannot find the jsp page, I don't know the reason, can someone tell me what's wrong with my code?

1
  • try adding @Bean annotation on top of public ViewResolver viewResolver() method. Commented Oct 28, 2017 at 11:08

1 Answer 1

1

Try adding @Bean annotation on top of WebConfig#viewResolver() method. So, the Spring Container manage your method as bean and your custom configuration will probably work.

@Bean
public ViewResolver viewResolver(){}

Indicates that a method produces a bean to be managed by the Spring container.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.