0
votes

I am following "Spring in action - Craig Walls" book and encountered the below error message. Lot of the issues were mentioned in relation to web.xml. I am using Java config and not web.xml.

Controller in spitter.web package:

package spitter.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(){
        return "home";
    }
}

Dispatcher servlet configuration in spittr.config package:

package spittr.config;

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

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

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

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

Rootconfig in same package:

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:

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("spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WebContent/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

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

I am using Maven to resolving dependencies and Tomcat 9 within eclipse to run.

Sep 17, 2016 4:46:48 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 5007 ms Sep 17, 2016 4:46:49 PM org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name 'dispatcher'

My view home.jsp is in WebContent/WEB-INF/views/home.jsp.

1
Make sure that all the spring dependencies are added to the classpath...Ravindra Devadiga
Uncomment those commented lines in the SpittrWebAppInitializer and delete the line below them.Ali Dehghani
Also I'm sure about the resolver.setPrefix("/WebContent/WEB-INF/views/"); and the fact that your view home.jsp is in webContent/WEB-INF/views/home.jsp.Ali Dehghani
what is the url you tried to access?kuhajeyan
@Sekar, resolver.setPrefix("/WebContent/WEB-INF/views/"); and webContent/WEB-INF/views/home.jsp, there is some difference. webContent is not same as WebContent. Try to have a uniform name. It is case sensitiveharshavmb

1 Answers

0
votes

Thanks for your help. I made the below changes and it is working now.

I move the web.config to the same package as my constructor. Then created the war file and deployed in Apache Tomcat. Now i am able to access the website.