0 0

springMVC+velocity Unable to find resource30

applicationContext.xml
 <!-- 配置velocity引擎 -->
    <bean id="velocityConfigurer"
          class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/templates/" /><!-- 模板存放的路径 -->
        <property name="configLocation" value="classpath:velocity.properties" />
    </bean>

    <!-- 配置视图的显示 -->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <property name="prefix" value="/" /><!-- 视图文件的前缀,即存放的路径 -->
        <property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
        <property name="toolboxConfigLocation" value="/WEB-INF/tools.xml" /><!--toolbox配置文件路径-->
        <property name="dateToolAttribute" value="date" /><!--日期函数名称-->
        <property name="numberToolAttribute" value="number" /><!--数字函数名称-->
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
        <property name="exposeRequestAttributes" value="true" /><!--是否开放request属性-->
        <property name="requestContextAttribute" value="req"/><!--request属性引用名称-->
        <!--指定layout文件-->
        <property name="layoutUrl" value="layout/default.vm"/>
    </bean>


velocity.properties (配置了很多,只列有关路径的)
input.encoding=UTF-8
output.encoding=UTF-8
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


java类使用
public class VelocityUtil {

    private static final String DEFAULT_CHARSET = "UTF-8";

    public static String parseVm(String target, VelocityContext context) {
        StringWriter writer = new StringWriter(1024);
        if (null == context)
            context = new VelocityContext();
        Velocity.mergeTemplate(target, DEFAULT_CHARSET, context, writer);
        return writer.toString();
    }

    public static String parseVm(String target, Map<String, Object> map) {
        StringWriter writer = new StringWriter(1024);
        VelocityContext context = new VelocityContext();
        if (null != map) {
            for (String key : map.keySet()) {
                context.put(key, map.get(key));
            }
        }
        return parseVm(target, context);
    }
}


使用
@RequestMapping(value = "/test2.rpc", method = RequestMethod.POST)
    @ResponseBody
    public String test3(HttpServletRequest req, HttpServletResponse res)
            throws Exception{

        try {
            String content = VelocityUtil.parseVm("/error.vm", new VelocityContext());
            return AjaxUtils.makeJson(res, false, content);
        } catch (Exception e) {
            logger.info("---------------------" + AjaxUtils.makeExceptionJson(res, e));
            return AjaxUtils.makeExceptionJson(res, e);
        }
    }


异常信息如下:

Unable to find resource '/index.vm'


我调试过,发现它使用的配置,不是我在springMVC里配置好的,而是一个初始化的配置,所以我想问一下,我如果想手动渲染一个vm,得到一段html,在springMVC里我应该怎么做呢。还是说我哪里配置有问题,麻烦讲下。

2014年8月28日 21:07
目前还没有答案

相关推荐

Global site tag (gtag.js) - Google Analytics