1

I am learning Spring MVC from a book called "Spring in Action". However. I am getting 404 error when I hit the correct controller. I am using annotations rather than xml configurations, so it is hard to find from web. You can see the very simple classes and configs I am using

SpittrWebInitializer.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[] {"/"};
    }
}

RootConfig.java

package spittr.config;

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

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

WebConfig.java

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

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"spittr.web"})
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    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();
    }
} 

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 = "/home", method = RequestMethod.GET)
    private String home() {
        return "home";
    }
}

and finally under resources/WEB-INF/views/ package I have one jsp called home.jsp which is like;

<html>
<body>
<h1>Spring 4.0.2 MVC web service</h1>

<h3>Welcome!!!</h3>
</body>
</html>

The can hit the controller, so I know they are initialized without any problem, however, context is not able to find the proper jsp. I appreciate for your help. Thanks

quartaela
  • 2,579
  • 16
  • 63
  • 99
  • Please Copy the URL you are entering. Try with select the project click rigth and click in run. (This is for looking the URL the project have as startup) – Ignacio Chiazzo Sep 06 '15 at 00:04
  • I am entering `localhost:8080/spitter/home`. spitter is the artifactId defined in pom.xml file. and I know I hit the controller because I can debug it. – quartaela Sep 06 '15 at 00:08

1 Answers1

4

JSP location is not correct WEB-INF location is under src/main/webapp not under resources

SwEngin
  • 91
  • 1
  • 3
  • perfect! I thought there is no difference between resources/ and webapp. What is difference then ? – quartaela Sep 06 '15 at 01:09
  • resources is for application resources configs and such webapp is for webapp resources views and serving static resources like css and images take a look at maven standard directory layout here https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – SwEngin Sep 06 '15 at 01:16