Java 类org.apache.velocity.tools.view.context.ChainedContext 实例源码

项目:spring-velocity-adapter    文件:VelocityToolboxView.java   
/**
 * Overridden to create a ChainedContext, which is part of the view package
 * of Velocity Tools, as special context. ChainedContext is needed for
 * initialization of ViewTool instances.
 * @see #initTool
 */
@Override
protected Context createVelocityContext(
        Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

    // Create a ChainedContext instance.
    ChainedContext velocityContext = new ChainedContext(
            new VelocityContext(model), getVelocityEngine(), request, response, getServletContext());

    // Load a Velocity Tools toolbox, if necessary.
    if (getToolboxConfigLocation() != null) {
        ToolboxManager toolboxManager = ServletToolboxManager.getInstance(
                getServletContext(), getToolboxConfigLocation());
        Map<String, Object> toolboxContext = toolboxManager.getToolbox(velocityContext);
        velocityContext.setToolbox(toolboxContext);
    }

    return velocityContext;
}
项目:spring4-understanding    文件:VelocityToolboxView.java   
/**
 * Overridden to create a ChainedContext, which is part of the view package
 * of Velocity Tools, as special context. ChainedContext is needed for
 * initialization of ViewTool instances.
 * @see #initTool
 */
@Override
protected Context createVelocityContext(
        Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

    // Create a ChainedContext instance.
    ChainedContext velocityContext = new ChainedContext(
            new VelocityContext(model), getVelocityEngine(), request, response, getServletContext());

    // Load a Velocity Tools toolbox, if necessary.
    if (getToolboxConfigLocation() != null) {
        ToolboxManager toolboxManager = ServletToolboxManager.getInstance(
                getServletContext(), getToolboxConfigLocation());
        Map<?, ?> toolboxContext = toolboxManager.getToolbox(velocityContext);
        velocityContext.setToolbox(toolboxContext);
    }

    return velocityContext;
}
项目:class-guard    文件:VelocityToolboxView.java   
/**
 * Overridden to create a ChainedContext, which is part of the view package
 * of Velocity Tools, as special context. ChainedContext is needed for
 * initialization of ViewTool instances.
 * @see #initTool
 */
@Override
protected Context createVelocityContext(
        Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

    // Create a ChainedContext instance.
    ChainedContext velocityContext = new ChainedContext(
            new VelocityContext(model), getVelocityEngine(), request, response, getServletContext());

    // Load a Velocity Tools toolbox, if necessary.
    if (getToolboxConfigLocation() != null) {
        ToolboxManager toolboxManager = ServletToolboxManager.getInstance(
                getServletContext(), getToolboxConfigLocation());
        Map toolboxContext = toolboxManager.getToolbox(velocityContext);
        velocityContext.setToolbox(toolboxContext);
    }

    return velocityContext;
}
项目:struts2    文件:VelocityManager.java   
/**
 * <p>
 * This method is responsible for creating the standard VelocityContext used by all WW2 velocity views.  The
 * following context parameters are defined:
 * </p>
 *
 * <ul>
 * <li><strong>request</strong> - the current HttpServletRequest</li>
 * <li><strong>response</strong> - the current HttpServletResponse</li>
 * <li><strong>stack</strong> - the current {@link ValueStack}</li>
 * <li><strong>ognl</strong> - an {@link OgnlTool}</li>
 * <li><strong>struts</strong> - an instance of {@link org.apache.struts2.util.StrutsUtil}</li>
 * <li><strong>action</strong> - the current Struts action</li>
 * </ul>
 *
 * @param stack the current {@link ValueStack}
 * @param req the current HttpServletRequest
 * @param res the current HttpServletResponse
 * @return a new StrutsVelocityContext
 */
public Context createContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
    Context result = null;
    VelocityContext[] chainedContexts = prepareChainedContexts(req, res, stack.getContext());
    StrutsVelocityContext context = new StrutsVelocityContext(chainedContexts, stack);
    Map standardMap = ContextUtil.getStandardContext(stack, req, res);
    for (Iterator iterator = standardMap.entrySet().iterator(); iterator.hasNext();) {
        Map.Entry entry = (Map.Entry) iterator.next();
        context.put((String) entry.getKey(), entry.getValue());
    }
    context.put(STRUTS, new VelocityStrutsUtil(velocityEngine, context, stack, req, res));


    ServletContext ctx = null;
    try {
        ctx = ServletActionContext.getServletContext();
    } catch (NullPointerException npe) {
        // in case this was used outside the lifecycle of struts servlet
        LOG.debug("internal toolbox context ignored");
    }

    if (toolboxManager != null && ctx != null) {
        ChainedContext chained = new ChainedContext(context, velocityEngine, req, res, ctx);
        chained.setToolbox(toolboxManager.getToolbox(chained));
        result = chained;
    } else {
        result = context;
    }

    req.setAttribute(KEY_VELOCITY_STRUTS_CONTEXT, result);
    return result;
}
项目:spring4-understanding    文件:VelocityToolboxViewTests.java   
@Test
public void testVelocityToolboxView() throws Exception {
    final String templateName = "test.vm";

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    final Template expectedTemplate = new Template();
    VelocityConfig vc = new VelocityConfig() {
        @Override
        public VelocityEngine getVelocityEngine() {
            return new TestVelocityEngine(templateName, expectedTemplate);
        }
    };
    wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);

    final HttpServletRequest expectedRequest = new MockHttpServletRequest();
    final HttpServletResponse expectedResponse = new MockHttpServletResponse();

    VelocityToolboxView vv = new VelocityToolboxView() {
        @Override
        protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
            assertTrue(template == expectedTemplate);
            assertTrue(response == expectedResponse);
            assertTrue(context instanceof ChainedContext);

            assertEquals("this is foo.", context.get("foo"));
            assertTrue(context.get("map") instanceof HashMap<?,?>);
            assertTrue(context.get("date") instanceof DateTool);
            assertTrue(context.get("math") instanceof MathTool);

            assertTrue(context.get("link") instanceof LinkTool);
            LinkTool linkTool = (LinkTool) context.get("link");
            assertNotNull(linkTool.getContextURL());

            assertTrue(context.get("link2") instanceof LinkTool);
            LinkTool linkTool2 = (LinkTool) context.get("link2");
            assertNotNull(linkTool2.getContextURL());
        }
    };

    vv.setUrl(templateName);
    vv.setApplicationContext(wac);
    Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>();
    toolAttributes.put("math", MathTool.class);
    toolAttributes.put("link2", LinkTool.class);
    vv.setToolAttributes(toolAttributes);
    vv.setToolboxConfigLocation("org/springframework/web/servlet/view/velocity/toolbox.xml");
    vv.setExposeSpringMacroHelpers(false);

    vv.render(new HashMap<String,Object>(), expectedRequest, expectedResponse);
}
项目:class-guard    文件:VelocityToolboxViewTests.java   
@Test
public void testVelocityToolboxView() throws Exception {
    final String templateName = "test.vm";

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    final Template expectedTemplate = new Template();
    VelocityConfig vc = new VelocityConfig() {
        @Override
        public VelocityEngine getVelocityEngine() {
            return new TestVelocityEngine(templateName, expectedTemplate);
        }
    };
    wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);

    final HttpServletRequest expectedRequest = new MockHttpServletRequest();
    final HttpServletResponse expectedResponse = new MockHttpServletResponse();

    VelocityToolboxView vv = new VelocityToolboxView() {
        @Override
        protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
            assertTrue(template == expectedTemplate);
            assertTrue(response == expectedResponse);
            assertTrue(context instanceof ChainedContext);

            assertEquals("this is foo.", context.get("foo"));
            assertTrue(context.get("map") instanceof HashMap<?,?>);
            assertTrue(context.get("date") instanceof DateTool);
            assertTrue(context.get("math") instanceof MathTool);

            assertTrue(context.get("link") instanceof LinkTool);
            LinkTool linkTool = (LinkTool) context.get("link");
            assertNotNull(linkTool.getContextURL());

            assertTrue(context.get("link2") instanceof LinkTool);
            LinkTool linkTool2 = (LinkTool) context.get("link2");
            assertNotNull(linkTool2.getContextURL());
        }
    };

    vv.setUrl(templateName);
    vv.setApplicationContext(wac);
    @SuppressWarnings("unchecked")
    Map<String, Class> toolAttributes = new HashMap<String, Class>();
    toolAttributes.put("math", MathTool.class);
    toolAttributes.put("link2", LinkTool.class);
    vv.setToolAttributes(toolAttributes);
    vv.setToolboxConfigLocation("org/springframework/web/servlet/view/velocity/toolbox.xml");
    vv.setExposeSpringMacroHelpers(false);

    vv.render(new HashMap<String,Object>(), expectedRequest, expectedResponse);
}