Java Code Examples for org.apache.velocity.context.Context#get()
The following examples show how to use
org.apache.velocity.context.Context#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VelocityLayoutView.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Overrides the normal rendering process in order to pre-process the Context, * merging it with the screen template into a single value (identified by the * value of screenContentKey). The layout template is then merged with the * modified Context in the super class. */ @Override protected void doRender(Context context, HttpServletResponse response) throws Exception { renderScreenContent(context); // Velocity context now includes any mappings that were defined // (via #set) in screen content template. // The screen template can overrule the layout by doing // #set( $layout = "MyLayout.vm" ) String layoutUrlToUse = (String) context.get(this.layoutKey); if (layoutUrlToUse != null) { if (logger.isDebugEnabled()) { logger.debug("Screen content template has requested layout [" + layoutUrlToUse + "]"); } } else { // No explicit layout URL given -> use default layout of this view. layoutUrlToUse = this.layoutUrl; } mergeTemplate(getTemplate(layoutUrlToUse), context, response); }
Example 2
Source File: VelocityLayoutView.java From scoold with Apache License 2.0 | 6 votes |
/** * Overrides the normal rendering process in order to pre-process the Context, merging it with the screen template * into a single value (identified by the value of screenContentKey). The layout template is then merged with the * modified Context in the super class. */ @Override protected void doRender(Context context, HttpServletResponse response) throws Exception { renderScreenContent(context); // Velocity context now includes any mappings that were defined // (via #set) in screen content template. // The screen template can overrule the layout by doing // #set( $layout = "MyLayout.vm" ) String layoutUrlToUse = (String) context.get(this.layoutKey); if (layoutUrlToUse != null) { if (logger.isDebugEnabled()) { logger.debug("Screen content template has requested layout [" + layoutUrlToUse + "]"); } } else { // No explicit layout URL given -> use default layout of this view. layoutUrlToUse = this.layoutUrl; } mergeTemplate(getTemplate(layoutUrlToUse), context, response); }
Example 3
Source File: VelocityLayoutView.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Overrides the normal rendering process in order to pre-process the Context, * merging it with the screen template into a single value (identified by the * value of screenContentKey). The layout template is then merged with the * modified Context in the super class. */ @Override protected void doRender(Context context, HttpServletResponse response) throws Exception { renderScreenContent(context); // Velocity context now includes any mappings that were defined // (via #set) in screen content template. // The screen template can overrule the layout by doing // #set( $layout = "MyLayout.vm" ) String layoutUrlToUse = (String) context.get(this.layoutKey); if (layoutUrlToUse != null) { if (logger.isDebugEnabled()) { logger.debug("Screen content template has requested layout [" + layoutUrlToUse + "]"); } } else { // No explicit layout URL given -> use default layout of this view. layoutUrlToUse = this.layoutUrl; } mergeTemplate(getTemplate(layoutUrlToUse), context, response); }
Example 4
Source File: VelocityMacroTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void exposeSpringMacroHelpers() throws Exception { VelocityView vv = new VelocityView() { @Override protected void mergeTemplate(Template template, Context context, HttpServletResponse response) { assertTrue(context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext); RequestContext rc = (RequestContext) context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); BindStatus status = rc.getBindStatus("tb.name"); assertEquals("name", status.getExpression()); assertEquals("juergen", status.getValue()); } }; vv.setUrl(TEMPLATE_FILE); vv.setApplicationContext(wac); vv.setExposeSpringMacroHelpers(true); Map<String, Object> model = new HashMap<String, Object>(); model.put("tb", new TestBean("juergen", 99)); vv.render(model, request, response); }
Example 5
Source File: VelocityPortalRenderEngine.java From sakai with Educational Community License v2.0 | 6 votes |
public void render(String template, PortalRenderContext rcontext, Writer out) throws Exception { if (log.isTraceEnabled()) { log.trace("Portal trace is on, dumping PortalRenderContext to log:\n" + rcontext.dump()); } Context vc = ((VelocityPortalRenderContext) rcontext).getVelocityContext(); String skin = (String) vc.get("pageCurrentSkin"); if (skin == null || skin.length() == 0) { skin = defaultSkin; } if (!defaultSkin.equals(skin)) { vengine.getTemplate("/vm/" + skin + "/macros.vm"); } vengine.mergeTemplate("/vm/" + skin + "/" + template + ".vm", ((VelocityPortalRenderContext) rcontext).getVelocityContext(), out); }
Example 6
Source File: VelocityPortalRenderEngine.java From sakai with Educational Community License v2.0 | 6 votes |
public void render(String template, PortalRenderContext rcontext, Writer out) throws Exception { if (log.isTraceEnabled()) { log.trace("Portal trace is on, dumping PortalRenderContext to log:\n" + rcontext.dump()); } Context vc = ((VelocityPortalRenderContext) rcontext).getVelocityContext(); String skin = (String) vc.get("pageCurrentSkin"); if (skin == null || skin.length() == 0) { skin = defaultSkin; } if (!defaultSkin.equals(skin)) { vengine.getTemplate("/vm/" + skin + "/macros.vm"); } vengine.mergeTemplate("/vm/" + skin + "/" + template + ".vm", ((VelocityPortalRenderContext) rcontext).getVelocityContext(), out); }
Example 7
Source File: JmsMessageHeadersExtractor.java From AuTe-Framework with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public void setHeadersFromContext(Message message, Context context) throws JMSException { Map<String, Object> headers = (Map<String, Object>) context.get(HEADERS_KEY); if (headers == null) { return; } Map<String, Object> outHeaders = (Map<String, Object>) headers.get(HEADERS_OUT_KEY); if (outHeaders == null) { return; } for (Map.Entry<String, Object> entry : outHeaders.entrySet()) { message.setObjectProperty(entry.getKey(), entry.getValue()); } }
Example 8
Source File: VelocityLoginRenderEngine.java From sakai with Educational Community License v2.0 | 5 votes |
public void render(String template, LoginRenderContext rcontext, Writer out) throws Exception { Context vc = ((VelocityLoginRenderContext) rcontext).getVelocityContext(); String skin = (String) vc.get("pageCurrentSkin"); if (skin == null || skin.length() == 0) { skin = "defaultskin"; } if (!"defaultskin".equals(skin)) { vengine.getTemplate("/vm/" + skin + "/macros.vm"); } vengine.mergeTemplate("/vm/" + skin + "/" + template + ".vm", ((VelocityLoginRenderContext) rcontext).getVelocityContext(), out); }
Example 9
Source File: VelocityLoginRenderEngine.java From sakai with Educational Community License v2.0 | 5 votes |
public void render(String template, LoginRenderContext rcontext, Writer out) throws Exception { Context vc = ((VelocityLoginRenderContext) rcontext).getVelocityContext(); String skin = (String) vc.get("pageCurrentSkin"); if (skin == null || skin.length() == 0) { skin = "defaultskin"; } if (!"defaultskin".equals(skin)) { vengine.getTemplate("/vm/" + skin + "/macros.vm"); } vengine.mergeTemplate("/vm/" + skin + "/" + template + ".vm", ((VelocityLoginRenderContext) rcontext).getVelocityContext(), out); }
Example 10
Source File: VelocityViewTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test public void testExposeHelpers() throws Exception { final String templateName = "test.vm"; WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getServletContext()).willReturn(new MockServletContext()); final Template expectedTemplate = new Template(); VelocityConfig vc = new VelocityConfig() { @Override public VelocityEngine getVelocityEngine() { return new TestVelocityEngine(templateName, expectedTemplate); } }; Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>(); configurers.put("velocityConfigurer", vc); given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers); // let it ask for locale HttpServletRequest req = mock(HttpServletRequest.class); given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null); given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver()); given(req.getLocale()).willReturn(Locale.CANADA); final HttpServletResponse expectedResponse = new MockHttpServletResponse(); VelocityView vv = new VelocityView() { @Override protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception { assertTrue(template == expectedTemplate); assertTrue(response == expectedResponse); assertEquals("myValue", context.get("myHelper")); assertTrue(context.get("math") instanceof MathTool); assertTrue(context.get("dateTool") instanceof DateTool); DateTool dateTool = (DateTool) context.get("dateTool"); assertTrue(dateTool.getLocale().equals(Locale.CANADA)); assertTrue(context.get("numberTool") instanceof NumberTool); NumberTool numberTool = (NumberTool) context.get("numberTool"); assertTrue(numberTool.getLocale().equals(Locale.CANADA)); } @Override protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception { model.put("myHelper", "myValue"); } }; vv.setUrl(templateName); vv.setApplicationContext(wac); Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>(); toolAttributes.put("math", MathTool.class); vv.setToolAttributes(toolAttributes); vv.setDateToolAttribute("dateTool"); vv.setNumberToolAttribute("numberTool"); vv.setExposeSpringMacroHelpers(false); vv.render(new HashMap<String, Object>(), req, expectedResponse); assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType()); }
Example 11
Source File: VelocityToolboxViewTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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); }
Example 12
Source File: VelocityLayoutServlet.java From velocity-tools with Apache License 2.0 | 4 votes |
/** * Overrides VelocityViewServlet.mergeTemplate to do a two-pass * render for handling layouts * @param template {@link Template} object * @param context Velocity context * @param response servlet response */ protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws IOException { // // this section is based on Tim Colson's "two pass render" // // Render the screen content StringWriter sw = new StringWriter(); template.merge(context, sw); // Add the resulting content to the context context.put(KEY_SCREEN_CONTENT, sw.toString()); // Check for an alternate layout // // we check after merging the screen template so the screen // can overrule any layout set in the request parameters // by doing #set( $layout = "MyLayout.vm" ) Object obj = context.get(KEY_LAYOUT); String layout = (obj == null) ? null : obj.toString(); if (layout == null) { // no alternate, use default layout = defaultLayout; } else { // make it a full(er) path layout = layoutDir + layout; } try { //load the layout template template = getTemplate(layout); } catch (Exception e) { getLog().error("Can't load layout \"{}\"", layout, e); // if it was an alternate layout we couldn't get... if (!layout.equals(defaultLayout)) { // try to get the default layout // if this also fails, let the exception go template = getTemplate(defaultLayout); } } // Render the layout template into the response super.mergeTemplate(template, context, response); }
Example 13
Source File: JspUtils.java From velocity-tools with Apache License 2.0 | 2 votes |
/** * Returns the latest tag encountered. * * @param context The Velocity context. * @return The latest tag. */ public static JspTag getLatestJspTag(Context context) { return (JspTag) context.get(LATEST_TAG_ATTRIBUTE_NAME); }