首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用速度,出现"java.lang.NullPointerException“

问使用速度,出现"java.lang.NullPointerException“
EN

Stack Overflow用户
提问于 2013-03-09 13:27:52
回答 1查看 3.6K关注 0票数 1

(version:velocity-1.7,共用-集合-3.2)

我使用速度解析vm模板,并在request范围内设置值,在jsp中显示。

我的应用程序work normaly a period of time,但当用户访问一段时间后,jsp显示错误,无法访问

这里是log

代码语言:javascript
复制
Mar 8, 2013 11:40:54 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet spring threw exception
java.lang.NullPointerException
    at org.apache.commons.collections.ExtendedProperties.clearProperty(ExtendedProperties.java:797)
    at org.apache.commons.collections.ExtendedProperties.setProperty(ExtendedProperties.java:722)
    at org.apache.commons.collections.ExtendedProperties.combine(ExtendedProperties.java:783)
    at org.apache.velocity.runtime.RuntimeInstance.setProperties(RuntimeInstance.java:657)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:645)
    at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:226)
    at org.apache.velocity.app.Velocity.init(Velocity.java:97)
    at com.feilong.tools.velocity.VelocityUtil.parseVMTemplateWithClasspathResourceLoader(VelocityUtil.java:67)
    at com.feilong.taglib.display.pager.PagerUtil.getPagerContent(PagerUtil.java:107)
    at com.feilong.taglib.display.pager.PagerTag.writeContent(PagerTag.java:48)
    at com.feilong.taglib.display.pager.PagerTag.writeContent(PagerTag.java:13)
    at com.feilong.taglib.base.AbstractCommonTag.doStartTag(AbstractCommonTag.java:19)
    at org.apache.jsp.pages.product.product_005flist_jsp._jspx_meth_feilongDisplay_005fpager_005f0(product_005flist_jsp.java:762)
    at org.apache.jsp.pages.product.product_005flist_jsp._jspx_meth_c_005fotherwise_005f1(product_005flist_jsp.java:433)
    at org.apache.jsp.pages.product.product_005flist_jsp._jspx_meth_c_005fchoose_005f1(product_005flist_jsp.java:243)
    at org.apache.jsp.pages.product.product_005flist_jsp._jspService(product_005flist_jsp.java:116)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

这里是我的util代码:(两种公共方法:有时用于解析vm模板,有时用于解析字符串)

代码语言:javascript
复制
/**
 * VelocityUtil
 * 
 * @author feilong
  */
public final class VelocityUtil{

    private static String   RUNTIME_LOG_LOG4J_LOGGER        = "feilongVelocityLogger";

    private static String   RUNTIME_LOG_LOG4J_LOGGER_LEVEL  = "debug";

    public static String parseVMTemplateWithClasspathResourceLoader(String templateInClassPath,Map<String, Object> contextKeyValues){
        String resource_loader = "class";
        Properties properties = new Properties();
        properties.put(Velocity.RESOURCE_LOADER, resource_loader);
        properties.put(resource_loader + ".resource.loader.class", ClasspathResourceLoader.class.getName());
        properties.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
        properties.put(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER, RUNTIME_LOG_LOG4J_LOGGER);
        properties.put(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER_LEVEL, RUNTIME_LOG_LOG4J_LOGGER_LEVEL);
        properties.put(Velocity.INPUT_ENCODING, CharsetType.UTF8);
        properties.put(Velocity.OUTPUT_ENCODING, CharsetType.UTF8);

        Velocity.init(properties);
        return parseVMTemplateAfterInitVelocity(templateInClassPath, contextKeyValues);
    }


    public static String parseVMContentWithStringResourceLoader(String vmContent,Map<String, Object> contextKeyValues){
        String resource_loader = "string";
        Properties properties = new Properties();
        properties.put(Velocity.RESOURCE_LOADER, resource_loader);
        properties.put(resource_loader + ".resource.loader.class", StringResourceLoader.class.getName());
        properties.put(Velocity.INPUT_ENCODING, CharsetType.UTF8);
        properties.put(Velocity.OUTPUT_ENCODING, CharsetType.UTF8);

        Velocity.init(properties);

        String templateName = "feilongStringVelocity";
        StringResourceRepository stringResourceRepository = StringResourceLoader.getRepository();
        stringResourceRepository.putStringResource(templateName, vmContent);
        return parseVMTemplateAfterInitVelocity(templateName, contextKeyValues);
    }


    private static String parseVMTemplateAfterInitVelocity(String templateName,Map<String, Object> contextKeyValues){
        Template template = Velocity.getTemplate(templateName, CharsetType.UTF8);
        VelocityContext velocityContext = new VelocityContext();
        if (null != contextKeyValues){
            for (Map.Entry<String, Object> entry : contextKeyValues.entrySet()){
                velocityContext.put(entry.getKey(), entry.getValue());
            }
        }
        Writer writer = new StringWriter();
        template.merge(velocityContext, writer);
        try{
            writer.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
        return writer.toString();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-04 21:37:46

如果您每次呈现jsp时都初始化速度(看起来您是从您发布的代码中得到的),这可能是个问题。

我以前曾遇到过这个问题,并通过初始化VelocityEngine一次并将其存储以供以后使用来解决。

摘自这里 (来自nabble.com上的速度论坛的“威尔玻璃-侯赛因-2”):

这是一般的模式。在应用程序启动时,初始化一个VelocityEngine。藏在某个地方。每次需要处理模板、获取模板、创建和填充上下文时,都要使用速度引擎合并模板(与上下文)。

在上面的代码中,您可以尝试将Velocity.init调用移动到另一个方法(例如,初始化),该方法仅在utils类中首次使用方法时才被调用。在调用初始化一次之后,将其存储在布尔值中,不要再次调用初始化。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15311204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档