I am writing a Spring MVC web application. I am attempting to configure Spring with Java based configuration, however Spring is unable to detect my controllers without adding additional XML configuration.
My front controller class looks like this:
package no.magnus.controller;
public class FrontController extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {MvcConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
My MVC configuration class:
package no.magnus.config;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"no.magnus"})
public class MvcConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setSuffix(".jsp");
return vr;
}
}
A simple home controller:
package no.magnus.controller;
@Controller
public class HomeController {
@RequestMapping("/home")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}
Attempting to access url http://localhost:8080/home i get a 404 error. If i add XML based configuration to web.xml as well as xml servlet configuration, the home controller is detected and index.jsp is returned.
subset web.xml:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<ctx:annotation-config/>
<ctx:component-scan base-package="no.magnus"/>
</beans>
http://localhost:8080/home index.jsp returned successfully!
My expectations are that Spring MVC shall be able to detect Spring MVC configuration from java code, making above XML configuration redundant. Please clarify wheter my understanding is mistaken or not. '
Thanks!
EDIT1:
Applying code changes suggested by Chris. New issue present. index.jsp is still not returned using only java config.

├───java
│ └───no
│ └───magnus
│ ├───config
│ │ MvcConfig.java
│ │
│ └───controller
│ FrontController.java
│ HomeController.java
│
└───webapp
│ result.jsp
│
└───WEB-INF
│ web.xml
│
└───jsp
index.jsp
HomeControllerlocated in a sub-package of"no.magnus"?DispatcherServletto load your java based configuration instead of your XML one nothing will happen. So either stick with XML - Java combo or properly configure yourDispatcherServlet.