MyController.java:
@Controller
public class ForemanController {
@RequestMapping({"/index", "/"})
public ModelAndView home(Model model){
Map<String, String> map = new HashMap<String, String>();
// .. fill map
return new ModelAndView("index", "map", map);
}
}
ServletInitializer.java:
public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{AppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
AppConfig.java:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.my"})
public class AppConfig {
@Bean
public VelocityConfigurer velocityConfig(){
VelocityConfigurer velocityConfig = new VelocityConfigurer();
velocityConfig.setResourceLoaderPath("/");
return velocityConfig;
}
@Bean
public VelocityLayoutViewResolver viewResolver(){
VelocityLayoutViewResolver viewResolver = new VelocityLayoutViewResolver();
viewResolver.setCache(true);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".vm");
return viewResolver;
}
}
WEB-INF/views 下的 index.vm:
<!DOCTYPE HTML>
<html>
<head>
<title>foreman</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
hello world!
</body>
</html>
我部署到 Wildfly,部署成功,使用 'localhost:8080/myapp' 访问主页,我得到Internal Server Error
:
2016-03-11 01:48:58,844 ERROR [org.apache.velocity] (default task-11) ResourceManager : unable to find resource 'layout.vm' in any resource loader.
我在我的项目中没有看到任何地方提到“布局”。这是从哪里来的?