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;
}
}
index.vm under WEB-INF/views:
<!DOCTYPE HTML>
<html>
<head>
<title>foreman</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
hello world!
</body>
</html>
I deploy to Wildfly, deployment successful, hit the home page with 'localhost:8080/myapp' and I get 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.
I see no mention of 'layout' anywhere in my project. Where is this coming from?
It is default behavior of VelocityLayoutViewResolver
in your bean viewResolver
to search for a template layout.vm
.
layout.vm
is expected to serve as a frame or wrapper around the views determined by your controller. That's very handy, because you don't need to bother about how a special view and your general HTML page are merged.
Please see this for example this tutorial (start at 'Creating templates') and this question for details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.