Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tiles side of rendering has full support for velocity

  • Loading branch information...
commit 1fb3c73d1b8544dc545729bb9adacf4125cd9934 1 parent 554bd2d
@konrad-garus authored
View
3  pom.xml
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.squirrel</groupId>
- <artifactId>money</artifactId>
+ <artifactId>svt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
@@ -24,6 +24,7 @@
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
+ <classifier>src</classifier>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
View
60 src/main/java/pl/squirrel/svt/AppConfiguration.java
@@ -1,19 +1,5 @@
package pl.squirrel.svt;
-import java.util.List;
-
-import org.apache.tiles.TilesApplicationContext;
-import org.apache.tiles.TilesContainer;
-import org.apache.tiles.context.ChainedTilesRequestContextFactory;
-import org.apache.tiles.context.TilesRequestContextFactory;
-import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
-import org.apache.tiles.factory.AbstractTilesContainerFactory;
-import org.apache.tiles.factory.BasicTilesContainerFactory;
-import org.apache.tiles.renderer.AttributeRenderer;
-import org.apache.tiles.renderer.impl.BasicRendererFactory;
-import org.apache.tiles.startup.DefaultTilesInitializer;
-import org.apache.tiles.velocity.context.VelocityTilesRequestContextFactory;
-import org.apache.tiles.velocity.renderer.VelocityAttributeRenderer;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -24,6 +10,7 @@
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityConfig;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
+import org.springframework.web.servlet.view.velocity.VelocityToolboxView;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
@Configuration
@@ -40,55 +27,22 @@ public void setApplicationContext(ApplicationContext context)
public VelocityConfig velocityConfig() {
VelocityConfigurer cfg = new VelocityConfigurer();
cfg.setResourceLoaderPath("/WEB-INF/velocity/");
- cfg.setConfigLocation(context.getResource("/WEB-INF/velocity.properties"));
+ cfg.setConfigLocation(context
+ .getResource("/WEB-INF/velocity.properties"));
return cfg;
}
@Bean
- public TilesConfigurer tilesConfigurer(){
+ public TilesConfigurer tilesConfigurer() {
TilesConfigurer cfg = new TilesConfigurer();
-
- cfg.setTilesInitializer(new DefaultTilesInitializer() {
- @Override
- protected AbstractTilesContainerFactory createContainerFactory(
- TilesApplicationContext context) {
- return new BasicTilesContainerFactory() {
-
- @Override
- protected List<TilesRequestContextFactory> getTilesRequestContextFactoriesToBeChained(
- ChainedTilesRequestContextFactory parent) {
- List<TilesRequestContextFactory> factories = super
- .getTilesRequestContextFactoriesToBeChained(parent);
- registerRequestContextFactory(
- VelocityTilesRequestContextFactory.class
- .getName(),
- factories, parent);
- return factories;
- }
-
- @Override
- protected AttributeRenderer createTemplateAttributeRenderer(
- BasicRendererFactory rendererFactory,
- TilesApplicationContext applicationContext,
- TilesRequestContextFactory contextFactory,
- TilesContainer container,
- AttributeEvaluatorFactory attributeEvaluatorFactory) {
- VelocityAttributeRenderer var = new VelocityAttributeRenderer();
- var.setApplicationContext(applicationContext);
- var.setRequestContextFactory(contextFactory);
- var.setAttributeEvaluatorFactory(attributeEvaluatorFactory);
- var.commit();
- return var;
- }
- };
- }});
+ cfg.setTilesInitializer(new VelocityTilesInitializer(velocityConfig()));
return cfg;
}
@Bean
- public ViewResolver viewRResolver() {
+ public ViewResolver viewResolver() {
VelocityViewResolver resolver = new VelocityViewResolver();
- resolver.setToolboxConfigLocation("/WEB-INF/tools.xml");
+ resolver.setViewClass(VelocityToolboxView.class);
resolver.setSuffix(".vm");
return resolver;
}
View
146 src/main/java/pl/squirrel/svt/ContextPassingVelocityAttributeRenderer.java
@@ -0,0 +1,146 @@
+package pl.squirrel.svt;
+
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.Attribute;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.impl.InvalidTemplateException;
+import org.apache.tiles.renderer.impl.AbstractTypeDetectingAttributeRenderer;
+import org.apache.tiles.servlet.context.ServletUtil;
+import org.apache.tiles.util.IteratorEnumeration;
+import org.apache.tiles.velocity.context.VelocityTilesRequestContext;
+import org.apache.tiles.velocity.renderer.VelocityAttributeRenderer;
+import org.apache.velocity.Template;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.tools.view.JeeConfig;
+import org.apache.velocity.tools.view.VelocityView;
+
+/**
+ * Largely based on VelocityAttributeRenderer, with tho workarounds:
+ *
+ * 1. Supports injecting a VelocityEngine (JIRA TILES-542)
+ *
+ * 2. Forwards context received from tiles macros to VelocityView (JIRA
+ * TILES-541)
+ *
+ * @author Konrad Garus
+ *
+ */
+public class ContextPassingVelocityAttributeRenderer extends
+ AbstractTypeDetectingAttributeRenderer {
+
+ /**
+ * The VelocityView object to use.
+ */
+ private VelocityView velocityView;
+
+ /**
+ * The initialization parameters for VelocityView.
+ */
+ private Map<String, String> params = new HashMap<String, String>();
+
+ private VelocityEngine engine;
+
+ public ContextPassingVelocityAttributeRenderer(VelocityEngine engine) {
+ this.engine = engine;
+ }
+
+ /**
+ * Sets a parameter for the internal servlet.
+ *
+ * @param key
+ * The name of the parameter.
+ * @param value
+ * The value of the parameter.
+ * @since 2.2.0
+ */
+ public void setParameter(String key, String value) {
+ params.put(key, value);
+ }
+
+ /**
+ * Commits the parameters and makes this renderer ready for the use.
+ *
+ * @since 2.2.0
+ */
+ public void commit() {
+ velocityView = new VelocityView(new TilesApplicationContextJeeConfig());
+ velocityView.setVelocityEngine(engine);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void write(Object value, Attribute attribute,
+ TilesRequestContext request) throws IOException {
+ if (value != null) {
+ if (value instanceof String) {
+ InternalContextAdapter adapter = (InternalContextAdapter) ((VelocityTilesRequestContext) request)
+ .getRequestObjects()[0];
+ Context context = adapter.getInternalUserContext();
+ Template template = velocityView.getTemplate((String) value);
+ velocityView.merge(template, context, request.getWriter());
+ } else {
+ throw new InvalidTemplateException(
+ "Cannot render a template that is not a string: "
+ + value.toString());
+ }
+ } else {
+ throw new InvalidTemplateException("Cannot render a null template");
+ }
+ }
+
+ /** {@inheritDoc} */
+ public boolean isRenderable(Object value, Attribute attribute,
+ TilesRequestContext request) {
+ if (value instanceof String) {
+ String string = (String) value;
+ return string.startsWith("/") && string.endsWith(".vm");
+ }
+ return false;
+ }
+
+ /**
+ * Implements JeeConfig to use parameters set through
+ * {@link VelocityAttributeRenderer#setParameter(String, String)}.
+ *
+ * @version $Rev: 821299 $ $Date: 2009-10-03 14:15:05 +0200 (sab, 03 ott
+ * 2009) $
+ * @since 2.2.0
+ */
+ private class TilesApplicationContextJeeConfig implements JeeConfig {
+
+ /** {@inheritDoc} */
+ public String getInitParameter(String name) {
+ return params.get(name);
+ }
+
+ /** {@inheritDoc} */
+ public String findInitParameter(String key) {
+ return params.get(key);
+ }
+
+ /** {@inheritDoc} */
+ @SuppressWarnings("unchecked")
+ public Enumeration getInitParameterNames() {
+ return new IteratorEnumeration(params.keySet().iterator());
+ }
+
+ /** {@inheritDoc} */
+ public String getName() {
+ return "Tiles Application Context JEE Config";
+ }
+
+ /** {@inheritDoc} */
+ public ServletContext getServletContext() {
+ return ServletUtil.getServletContext(applicationContext);
+ }
+ }
+
+}
View
2  src/main/java/pl/squirrel/svt/HelloWorldController.java
@@ -8,7 +8,7 @@
public class HelloWorldController {
@RequestMapping("/hello.html")
- public String velo() {
+ public String velo(ModelMap model) {
return "hello_velocity";
}
}
View
58 src/main/java/pl/squirrel/svt/VelocityTilesInitializer.java
@@ -0,0 +1,58 @@
+package pl.squirrel.svt;
+
+import java.util.List;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.context.ChainedTilesRequestContextFactory;
+import org.apache.tiles.context.TilesRequestContextFactory;
+import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
+import org.apache.tiles.factory.AbstractTilesContainerFactory;
+import org.apache.tiles.factory.BasicTilesContainerFactory;
+import org.apache.tiles.renderer.AttributeRenderer;
+import org.apache.tiles.renderer.impl.BasicRendererFactory;
+import org.apache.tiles.startup.DefaultTilesInitializer;
+import org.apache.tiles.velocity.context.VelocityTilesRequestContextFactory;
+import org.springframework.web.servlet.view.velocity.VelocityConfig;
+
+public class VelocityTilesInitializer extends DefaultTilesInitializer {
+ private VelocityConfig velocityConfig;
+
+ public VelocityTilesInitializer(VelocityConfig velocityConfig) {
+ this.velocityConfig = velocityConfig;
+ }
+
+ @Override
+ protected AbstractTilesContainerFactory createContainerFactory(
+ TilesApplicationContext context) {
+ return new BasicTilesContainerFactory() {
+
+ @Override
+ protected List<TilesRequestContextFactory> getTilesRequestContextFactoriesToBeChained(
+ ChainedTilesRequestContextFactory parent) {
+ List<TilesRequestContextFactory> factories = super
+ .getTilesRequestContextFactoriesToBeChained(parent);
+ registerRequestContextFactory(
+ VelocityTilesRequestContextFactory.class.getName(),
+ factories, parent);
+ return factories;
+ }
+
+ @Override
+ protected AttributeRenderer createTemplateAttributeRenderer(
+ BasicRendererFactory rendererFactory,
+ TilesApplicationContext applicationContext,
+ TilesRequestContextFactory contextFactory,
+ TilesContainer container,
+ AttributeEvaluatorFactory attributeEvaluatorFactory) {
+ ContextPassingVelocityAttributeRenderer var = new ContextPassingVelocityAttributeRenderer(
+ velocityConfig.getVelocityEngine());
+ var.setApplicationContext(applicationContext);
+ var.setRequestContextFactory(contextFactory);
+ var.setAttributeEvaluatorFactory(attributeEvaluatorFactory);
+ var.commit();
+ return var;
+ }
+ };
+ }
+}
View
0  src/main/webapp/WEB-INF/tools.xml
No changes.
View
2  src/main/webapp/WEB-INF/velocity/hello_velocity.vm
@@ -1,4 +1,4 @@
-#tiles_insertTemplate({"template":"/WEB-INF/velocity/template.vm"})
+#tiles_insertTemplate({"template":"template.vm"})
#tiles_putAttribute({"name":"title", "value": "Test Title"})#end
#tiles_putAttribute({"name":"body"})
#parse("body.vm")
View
1  src/main/webapp/WEB-INF/velocity/template.vm
@@ -2,5 +2,6 @@
<head><title>#tiles_insertAttribute({"name":"title"})#end</title></head>
<body>
#tiles_insertAttribute({"name":"body"})#end
+ <p>Spring macros work in tiles template, too: #springUrl("/myUrl")</p>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.