0

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.

我在我的项目中没有看到任何地方提到“布局”。这是从哪里来的?

4

1 回答 1

0

VelocityLayoutViewResolver在您的 beanviewResolver中搜索模板是默认行为layout.vm

layout.vm预计将用作控制器确定的视图周围的框架或包装器。这非常方便,因为您无需担心特殊视图和一般 HTML 页面是如何合并的。

请参阅本教程(从“创建模板”开始)和这个问题的详细信息。

于 2016-03-11T10:25:54.017 回答