【问题标题】:ERROR [org.apache.velocity] ResourceManager : unable to find resource 'layout.vm' in any resource loader错误 [org.apache.velocity] ResourceManager:无法在任何资源加载器中找到资源“layout.vm”
【发布时间】:2016-03-11 09:38:35
【问题描述】:

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.

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

【问题讨论】:

    标签: java spring spring-mvc velocity resource-loading


    【解决方案1】:

    在您的 bean viewResolverVelocityLayoutViewResolver 的默认行为是搜索模板 layout.vm

    layout.vm 应作为控制器确定的视图的框架或包装器。这非常方便,因为您无需担心如何合并特殊视图和常规 HTML 页面。

    请参阅此tutorial(从“创建模板”开始)和此question 了解详细信息。

    【讨论】:

    • 谢谢,我希望官方文档中的重要约定能够更加清晰。我没有在速度站点上看到 layout.vm 明确定义。
    • 这里有点误解 - 这不是原始的 Velocity 而是 Spring。这是doc
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多