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
.
SpittrWebAppInitializer
and delete the line below them. – Ali Dehghaniresolver.setPrefix("/WebContent/WEB-INF/views/");
and the fact that your viewhome.jsp
is inwebContent/WEB-INF/views/home.jsp
. – Ali Dehghani