`
hongjn
  • 浏览: 55179 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

ear,war包下velocity模板找不到

阅读更多
把应用打成war包发布在Weblogic上时出现找不到velocity模板,异常:Unable to find resource '/template/simple/hidden.vm' (以目录方式发布到Weblogic上,不存在此问题)

经过排查,问题出现在资源的加载方式上,解决问题的方法是,为应用增加加载资源的方法,webwork已经提供了一个在classpath加载资源的类WebWorkResourceLoader (该类继承了velocity提供的ClasspathResourceLoader),因此只需要在velocity的配置文件中修改配置。

#默认的设置
#resource.loader = file

#file.resource.loader.description = Velocity File Resource Loader
#file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
#file.resource.loader.path = .
#file.resource.loader.cache = false
#file.resource.loader.modificationCheckInterval = 2


#修改完的
resource.loader = class
class.resource.loader.class = com.opensymphony.webwork.views.velocity.WebWorkResourceLoader


默认情况下,webwork会加载类路径下的velocity.properties,当然velocity.properties的路径也可以自定义,只需要在webwork.properties的webwork.velocity.configfile属性中配置

### Location of velocity.properties file.  defaults to velocity.properties
#webwork.velocity.configfile = velocity.properties


测试时发现配置完的velocity.properties并没有被加载,查看源代码发现原因是出在webwork的实现上,同时webwork也支持自定义的扩展,因此我们可以扩展webwork的实现。如:自定义类MyVelocityManager继承VelocityManager.

public class MyVelocityManager extends VelocityManager {

    private static final Log log = LogFactory.getLog(EcsSalesVelocityManager.class);
    private String configfile = "velocity.properties";

    @Override
    public Properties loadConfiguration(ServletContext context) {
        Properties properties = super.loadConfiguration(context);

        if (Configuration.isSet("webwork.velocity.configfile")) {
            configfile = Configuration.getString("webwork.velocity.configfile");
        }

        configfile = configfile.trim();

        InputStream inStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(configfile);
        try {
            properties.load(inStream);
        }
        catch (IOException e) {
            log.warn("Unable to load velocity configuration file '" + configfile + "'", e);
        }
        return properties;
    }
}


我们需要在webwork.properties中自定义该类的路径
#扩展的VelocityManager
webwork.velocity.manager.classname = com.opensymphony.webwork.views.velocity.ext.MyVelocityManager

分享到:
评论
1 楼 jjjssh 2014-05-04  
你好,有没有完整的demo,我想研究下,我部署的war包,总是找不到相应的vm,我尝试了各种ResourceLoader

相关推荐

Global site tag (gtag.js) - Google Analytics