我有這樣的代碼:此代碼如何在沒有@EnableMVC的情況下工作?
package biz.tugay.springJuly18.config;
/* User: [email protected] 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[]{"/"};
}
}
和ServletConfig.class
package biz.tugay.springJuly18.config;
/* User: [email protected] 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;
}
}
最後一個控制器:
package biz.tugay.springJuly18.web;
/* User: [email protected] 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;
}
}
當我部署這Tomcat和去/測試我將被重定向到koko.jsp。那麼,爲什麼這個工作沒有@EnableWebMvc?
根據此回答here它不應該?
這裏是RootConfig.java
package biz.tugay.springJuly18.config;
/* User: [email protected] 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 {
}
什麼在'RootConfigClass'? –
@SotiriosDelimanolis對問題添加。 –