I have this code:
package biz.tugay.springJuly18.config;
/* User: koray@tugay.biz Date: 18/07/15 Time: 15:09 */
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@SuppressWarnings(value = "unused")
public class MyWebAppInnit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfigClass.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ServletConfigClass.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
and the ServletConfig.class
package biz.tugay.springJuly18.config;
/* User: koray@tugay.biz Date: 18/07/15 Time: 15:10 */
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.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages = "biz.tugay.springJuly18.web")
public class ServletConfigClass {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
}
and finally a Controller:
package biz.tugay.springJuly18.web;
/* User: koray@tugay.biz Date: 18/07/15 Time: 12:53 */
import biz.tugay.springJuly18.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyWeb {
@Autowired
private MyService myService;
@RequestMapping("/test")
public ModelAndView helloWorld() {
ModelAndView mv = new ModelAndView();
mv.addObject("message", myService.sayHello());
mv.setViewName("koko");
return mv;
}
}
When I deploy this to Tomcat and go to /test I will be redirected to koko.jsp . So why does this work without @EnableWebMvc?
According to this answer here it should not?
Here is RootConfig.java
package biz.tugay.springJuly18.config;
/* User: koray@tugay.biz Date: 18/07/15 Time: 15:10 */
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "biz.tugay.springJuly18.service")
public class RootConfigClass {
}