Issue
I try to create a simple Spring MVC and run in tomcat via eclipse ide with one simple controller:
@Controller
public class HomeController {
@RequestMapping("/home")
public String helloWorld(Model model) {
model.addAttribute("message", "home university!");
return "home";
}
}
my other configs:
webinitializer:
public class WebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MvcConfig.class, ServiceConfig.class, AspectConfig.class, DaoConfig.class, DataSourceConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispather", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
MvcSpringJavaConfig:
public class MvcConfig {
@Configuration
@ComponentScan(basePackages = "img.imaginary.controller")
@EnableWebMvc
public class MvcConfiguration {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
}
structure:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
</web-app>
my pom.xml: pom.xml
and when tomcat starts and I try to open the app in the browser, I get a 404 status and a description:
The source server has not found the current representation of the target resource or does not want to disclose its existence.
I tried to change the controller and the web.xml and other configs, but I always get this result
I can't figure out what I'm missing in order for my simple home controller to start correctly
Solution
you should add @Configuration
to MvcConfig
class. also by referring to this:
Any nested configuration classes must be declared as static.
MvcConfiguration
class must be static.
Answered By - hatef alipoor
Answer Checked By - Gilberto Lyons (JavaFixing Admin)