Spring MVC

From Chunk Java Template Engine

(Difference between revisions)
Jump to: navigation, search
(Created page with "Chunk 2.6.4 adds some new plumbing that makes it easy to use with Spring MVC, as a drop-in replacement template engine for Freemarker or Velocity. Here's what you will need: 1....")
(Can I use Chunk Templates in my Spring MVC project?)
 
(12 intermediate revisions not shown)
Line 1: Line 1:
-
Chunk 2.6.4 adds some new plumbing that makes it easy to use with Spring MVC, as a drop-in replacement template engine for Freemarker or Velocity.
+
== Can I use Chunk Templates in my Spring MVC project? ==
 +
 
 +
Yes!
 +
 
 +
Since v2.6.4 Chunk has had bean-friendly plumbing that makes it easy to use with [http://projects.spring.io/spring-framework/ Spring MVC], as a drop-in replacement template engine for Freemarker or Velocity.
 +
 
 +
== Special features ==
 +
 
 +
MVC Framework localized messages (defined in messages.properties etc) are available via a custom tag command:
 +
 
 +
{% messages.msg.name %}
 +
 
 +
Messages can be parameterized like so:
 +
 
 +
{% messages.msg.name(`$a`,`$b`) %}
 +
 
 +
A special request context tag is available, usually {$rc} but the name is configurable.
 +
 
 +
The following request context values are available:
 +
 
 +
{$rc.uri}
 +
{$rc.context_path}
 +
{$rc.servlet_path}
 +
{$rc.scheme}
 +
{$rc.method}
 +
{$rc.server_name}
 +
{$rc.remote_addr}
 +
{$rc.remote_host}
 +
{$rc.remote_user}
 +
 
 +
== Using bean getters in a template ==
 +
 
 +
When exposing beans (or POJO member variables) in a model, keep in mind that Chunk always converts camelCase to snake_case.
 +
 
 +
So, <code>x.getFavoriteColor()</code> will be available in the template as <code>{% $x.favorite_color %}</code> -- and <code>x.isBigAndHairy()</code> must be checked like so:
 +
 
 +
{% if $x.big_and_hairy %}...{% endif %}
 +
 
 +
== Is there a sample project that I can clone? ==
 +
 
 +
Why yes, just head here:  [https://github.com/tomj74/chunk-spring-quickstart Sample Spring MVC project on GitHub]
 +
 
 +
You'll be up and running in no time.
 +
 
 +
== Getting Started ==
Here's what you will need:
Here's what you will need:
Line 5: Line 49:
1. pom.xml dependencies:
1. pom.xml dependencies:
-
    ...
+
  ...
-
    <dependencies>
+
  <dependencies>
-
      <dependency>
+
    '''<dependency>'''
-
          <groupId>org.springframework</groupId>
+
        '''<groupId>com.x5dev</groupId>'''
-
          <artifactId>spring-context</artifactId>
+
        '''<artifactId>chunk-springmvc</artifactId>'''
-
      </dependency>
+
        '''<version>0.1.0</version>'''
-
      <dependency>
+
    '''</dependency>'''
-
          <groupId>org.springframework</groupId>
+
    '''<dependency>'''
-
          <artifactId>spring-aop</artifactId>
+
        '''<groupId>com.x5dev</groupId>'''
-
          <version>4.1.5.RELEASE</version>
+
        '''<artifactId>chunk-templates</artifactId>'''
-
      </dependency>
+
        '''<version>3.2.4</version>'''
-
      <dependency>
+
    '''</dependency>'''
-
          <groupId>org.springframework</groupId>
+
    <dependency>
-
          <artifactId>spring-webmvc</artifactId>
+
        <groupId>org.springframework</groupId>
-
          <version>4.1.5.RELEASE</version>
+
        <artifactId>spring-context</artifactId>
-
      </dependency>
+
    </dependency>
-
      <dependency>
+
    <dependency>
-
          <groupId>org.springframework</groupId>
+
        <groupId>org.springframework</groupId>
-
          <artifactId>spring-web</artifactId>
+
        <artifactId>spring-aop</artifactId>
-
          <version>4.1.5.RELEASE</version>
+
        <version>4.1.5.RELEASE</version>
-
      </dependency>
+
    </dependency>
-
      <dependency>
+
    <dependency>
-
          <groupId>jstl</groupId>
+
        <groupId>org.springframework</groupId>
-
          <artifactId>jstl</artifactId>
+
        <artifactId>spring-webmvc</artifactId>
-
          <version>1.2</version>
+
        <version>4.1.5.RELEASE</version>
-
      </dependency>
+
    </dependency>
-
      <dependency>
+
    <dependency>
-
          <groupId>commons-logging</groupId>
+
        <groupId>org.springframework</groupId>
-
          <artifactId>commons-logging</artifactId>
+
        <artifactId>spring-web</artifactId>
-
          <version>1.2</version>
+
        <version>4.1.5.RELEASE</version>
-
      </dependency>
+
    </dependency>
-
      '''<dependency>'''
+
    <dependency>
-
          '''<groupId>com.x5dev</groupId>'''
+
        <groupId>jstl</groupId>
-
          '''<artifactId>chunk-templates</artifactId>'''
+
        <artifactId>jstl</artifactId>
-
          '''<version>2.6.4</version>'''
+
        <version>1.2</version>
-
      '''</dependency>'''
+
    </dependency>
-
    </dependencies>
+
    <dependency>
-
    <dependencyManagement>
+
        <groupId>commons-logging</groupId>
-
      <dependencies>
+
        <artifactId>commons-logging</artifactId>
-
          <dependency>
+
        <version>1.2</version>
-
              <groupId>org.springframework</groupId>
+
    </dependency>
-
              <artifactId>spring-context</artifactId>
+
  </dependencies>
-
              <version>4.1.5.RELEASE</version>
+
  <dependencyManagement>
-
          </dependency>
+
    <dependencies>
-
      </dependencies>
+
        <dependency>
-
    </dependencyManagement>
+
            <groupId>org.springframework</groupId>
-
    ...
+
            <artifactId>spring-context</artifactId>
 +
            <version>4.1.5.RELEASE</version>
 +
        </dependency>
 +
    </dependencies>
 +
  </dependencyManagement>
 +
  ...
2. WebContent/WEB-INF/web.xml
2. WebContent/WEB-INF/web.xml
-
  <?xml version="1.0" encoding="UTF-8"?>
+
<?xml version="1.0" encoding="UTF-8"?>
-
 
+
-
  <web-app version="2.4"
+
<web-app version="2.4"
-
      xmlns="http://java.sun.com/xml/ns/j2ee"
+
      xmlns="http://java.sun.com/xml/ns/j2ee"
-
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
+
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
-
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
+
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
-
 
+
-
      <display-name>Chunky Spring</display-name>
+
    <display-name>Chunky Spring</display-name>
-
 
+
-
      <servlet>
+
    <servlet>
-
          <servlet-name>dispatcher</servlet-name>
+
        <servlet-name>dispatcher</servlet-name>
-
          <servlet-class>
+
        <servlet-class>
-
              org.springframework.web.servlet.DispatcherServlet
+
            org.springframework.web.servlet.DispatcherServlet
-
          </servlet-class>
+
        </servlet-class>
-
          <load-on-startup>1</load-on-startup>
+
        <load-on-startup>1</load-on-startup>
-
      </servlet>
+
    </servlet>
-
 
+
-
      <servlet-mapping>
+
    <servlet-mapping>
-
          <servlet-name>dispatcher</servlet-name>
+
        <servlet-name>dispatcher</servlet-name>
-
          <url-pattern>/</url-pattern>
+
        <url-pattern>/</url-pattern>
-
      </servlet-mapping>
+
    </servlet-mapping>
-
 
+
-
      <context-param>
+
    <context-param>
-
          <param-name>contextConfigLocation</param-name>
+
        <param-name>contextConfigLocation</param-name>
-
          <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
+
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
-
      </context-param>
+
    </context-param>
-
 
+
-
      <listener>
+
    <listener>
-
          <listener-class>
+
        <listener-class>
-
              org.springframework.web.context.ContextLoaderListener
+
            org.springframework.web.context.ContextLoaderListener
-
          </listener-class>
+
        </listener-class>
-
      </listener>
+
    </listener>
-
 
+
-
  </web-app>
+
</web-app>
-
 
+
-
3. WebContent/WEB-INF/dispatcher-servlet.xml - provide a custom viewResolver and some Theme configuration options.
+
-
 
+
-
Make sure to change `com.example.myapp` to the package where your annotated controllers live.
+
-
  <?xml version="1.0" encoding="UTF-8"?>
+
3. WebContent/WEB-INF/dispatcher-servlet.xml - provide some Theme configuration and drop in a custom view class (source below) to viewResolver.
-
  <beans xmlns="http://www.springframework.org/schema/beans"
+
-
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+
-
      xmlns:context="http://www.springframework.org/schema/context"
+
-
      xsi:schemaLocation="http://www.springframework.org/schema/beans
+
-
 
+
-
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
+
-
  http://www.springframework.org/schema/context
+
-
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">
+
-
 
+
-
      <context:component-scan base-package="'''com.example.myapp'''" />
+
-
 
+
-
      <bean id="chunkTemplatesConfig" class="java.util.HashMap" scope="prototype">
+
-
  <constructor-arg>
+
-
      <map key-type="java.lang.String" value-type="java.lang.String">
+
-
      <entry key="default_extension" value="chtml" />
+
-
      <entry key="cache_minutes" value="0" />
+
-
      <entry key="layers" value="" />
+
-
      <entry key="theme_path" value="" />
+
-
      <entry key="hide_errors" value="FALSE" />
+
-
      <entry key="error_log" value="" />
+
-
      <entry key="encoding" value="UTF-8" />
+
-
      <entry key="locale" value="" />
+
-
      <entry key="filters" value="" />
+
-
      </map>
+
-
  </constructor-arg>
+
-
      </bean>
+
-
 
+
-
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+
-
          <property name="viewClass" value="com.x5.template.spring.ChunkTemplateView"/>
+
-
          <property name="prefix" value="/WEB-INF/themes/"/>
+
-
          <property name="suffix" value=".chtml"/>
+
-
          <property name="requestContextAttribute" value="rc"/>
+
-
      </bean>
+
-
  </beans>
+
-
4. Include the custom view class in your project source (this may get packaged into a maven dependency at some point if there is demand/interest).
+
Make sure to change <code>com.example.myapp</code> to the package where your annotated controllers live.
-
ChunkTemplateView.java
+
<?xml version="1.0" encoding="UTF-8"?>
-
 
+
<beans xmlns="http://www.springframework.org/schema/beans"
-
  package com.x5.template.spring;
+
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
-
 
+
    xmlns:context="http://www.springframework.org/schema/context"
-
  import org.springframework.web.servlet.support.RequestContext;
+
    xsi:schemaLocation="http://www.springframework.org/schema/beans
-
  import org.springframework.web.servlet.view.InternalResourceView;
+
-
  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
-
  import org.springframework.core.io.Resource;
+
http://www.springframework.org/schema/context
-
 
+
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
-
  import com.x5.template.Theme;
+
-
  import com.x5.template.ThemeConfig;
+
    <context:component-scan base-package="'''com.example.myapp'''" />
-
  import com.x5.template.Chunk;
+
-
 
+
    <bean id="chunkTemplatesConfig" class="java.util.HashMap" scope="prototype">
-
  import javax.servlet.http.HttpServletRequest;
+
        <constructor-arg>
-
  import javax.servlet.http.HttpServletResponse;
+
            <map key-type="java.lang.String" value-type="java.lang.String">
-
 
+
                <entry key="default_extension" value="chtml" />
-
  import java.util.HashMap;
+
                <entry key="cache_minutes" value="0" />
-
  import java.util.Map;
+
                <entry key="layers" value="" />
-
  import java.io.PrintWriter;
+
                <entry key="theme_path" value="" />
-
 
+
                <entry key="hide_errors" value="FALSE" />
-
  public class ChunkTemplateView extends InternalResourceView
+
                <entry key="error_log" value="" />
-
  {
+
                <entry key="encoding" value="UTF-8" />
-
      private static Theme theme = null;
+
                <entry key="locale" value="" />
-
 
+
                <entry key="filters" value="" />
-
      @Override
+
            </map>
-
      protected void renderMergedOutputModel(
+
        </constructor-arg>
-
          Map<String, Object> model,
+
    </bean>
-
          HttpServletRequest request,
+
-
          HttpServletResponse response
+
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
      ) throws Exception
+
        <property name="viewClass" value="com.x5.template.spring.ChunkTemplateView"/>
-
      {
+
        <property name="prefix" value="/WEB-INF/themes/"/>
-
          Resource templateFile = getApplicationContext().getResource(getUrl());
+
        <property name="suffix" value=".chtml"/>
-
 
+
        <property name="requestContextAttribute" value="rc"/>
-
          String rcKey = getRequestContextAttribute();
+
    </bean>
-
          RequestContext rc = (RequestContext)model.get(rcKey);
+
</beans>
-
 
+
-
          Theme theme = getTheme(rc, templateFile.getFile().getParent());
+
-
          Chunk chunk = theme.makeChunk(getBeanName());
+
-
          chunk.setLocale(rc.getLocale());
+
-
          chunk.setMultiple(model);
+
-
          chunk.set(rcKey, mapifyRequestContext(rc, request));
+
-
 
+
-
          PrintWriter writer = response.getWriter();
+
-
          chunk.render(writer);
+
-
          writer.flush();
+
-
          writer.close();
+
-
      }
+
-
 
+
-
      private Map<String,String> mapifyRequestContext(RequestContext rc, HttpServletRequest request)
+
-
      {
+
-
          Map<String,String> rcMap = new HashMap<String,String>();
+
-
 
+
-
          // expose some potentially useful info to the template via the {$rc} tag
+
-
          rcMap.put("uri", rc.getRequestUri());
+
-
          rcMap.put("context_path", rc.getContextPath());
+
-
          rcMap.put("servlet_path", rc.getPathToServlet());
+
-
          rcMap.put("scheme", request.getScheme());
+
-
          rcMap.put("method", request.getMethod());
+
-
          rcMap.put("server_name", request.getServerName());
+
-
          rcMap.put("remote_addr", request.getRemoteAddr());
+
-
          rcMap.put("remote_host", request.getRemoteHost());
+
-
          rcMap.put("remote_user", request.getRemoteUser());
+
-
 
+
-
          return rcMap;
+
-
      }
+
-
 
+
-
      private Theme getTheme(RequestContext rc, String path)
+
-
      {
+
-
          if (theme == null) {
+
-
              Map<String,String> params = new HashMap<String,String>();
+
-
              // If no theme path (for include/exec references) is specified
+
-
              // in the config, default to the path of the invoked template file.
+
-
              params.put(ThemeConfig.THEME_PATH, path);
+
-
 
+
-
              Map<String,String> configParams = getConfigParams();
+
-
              if (configParams != null) {
+
-
                  for (String key : configParams.keySet()) {
+
-
                      String paramName = key;
+
-
                      String paramValue = configParams.get(key);
+
-
                      // blank values are considered not-provided
+
-
                      if (paramValue != null && paramValue.trim().length() > 0) {
+
-
                          params.put(paramName, paramValue);
+
-
                      }
+
-
                  }
+
-
              }
+
-
              ThemeConfig config = new ThemeConfig(params);
+
-
              theme = new Theme(config);
+
-
          }
+
-
 
+
-
          return theme;
+
-
      }
+
-
 
+
-
      @SuppressWarnings("unchecked")
+
-
      private Map<String,String> getConfigParams()
+
-
      {
+
-
          try {
+
-
              Object config = getApplicationContext().getBean("chunkTemplatesConfig");
+
-
              return (Map<String,String>)config;
+
-
          } catch (NoSuchBeanDefinitionException e) {
+
-
              return null;
+
-
          }
+
-
      }
+
-
  }
+
-
5. Make a controller!  The template name "hello" will resolve to /WEB-INF/themes/hello.chtml and any include/exec calls will be resolved relative to there.
+
4. Make a controller!  The template name "helloworld" will resolve to /WEB-INF/themes/helloworld.chtml and any include/exec calls will be resolved relative to there.
-
  package com.example.myapp;
+
package com.example.myapp;
-
 
+
-
  import org.springframework.stereotype.Controller;
+
import org.springframework.stereotype.Controller;
-
  import org.springframework.ui.Model;
+
import org.springframework.ui.Model;
-
  import org.springframework.web.bind.annotation.RequestMapping;
+
import org.springframework.web.bind.annotation.RequestMapping;
-
  import org.springframework.web.bind.annotation.RequestParam;
+
import org.springframework.web.bind.annotation.RequestParam;
-
  import org.springframework.web.servlet.ModelAndView;
+
import org.springframework.web.servlet.ModelAndView;
-
 
+
-
  @Controller
+
@Controller
-
  public class HelloChunk
+
public class HelloChunk
-
  {
+
{
-
      String message = "Welcome to Spring MVC!";
+
    String message = "Welcome to Spring MVC!";
-
 
+
-
      @RequestMapping("/hello")
+
    @RequestMapping("/hello")
-
      public ModelAndView showMessage(
+
    public ModelAndView showMessage(
-
              @RequestParam(value = "name", required = false, defaultValue = "World") String name)
+
            @RequestParam(value = "name", required = false, defaultValue = "World") String name)
-
      {
+
    {
-
          ModelAndView mv = new ModelAndView("helloworld");
+
        ModelAndView mv = new ModelAndView("helloworld");
-
          mv.addObject("message", message);
+
        mv.addObject("message", message);
-
          mv.addObject("name", name);
+
        mv.addObject("name", name);
-
          mv.addObject("page_name", "Spring 4 MVC - Hello Chunk");
+
        mv.addObject("page_name", "Spring 4 MVC - Hello Chunk");
-
          return mv;
+
        return mv;
-
      }
+
    }
-
 
+
-
      @RequestMapping(value={"/chunk","/"})
+
    @RequestMapping(value={"/chunk","/"})
-
      public ModelAndView chunkTime()
+
    public ModelAndView chunkTime()
-
      {
+
    {
-
          ModelAndView mv = new ModelAndView("chunkworld");
+
        ModelAndView mv = new ModelAndView("chunkworld");
-
          mv.addObject("page_name", "Chunky Time");
+
        mv.addObject("page_name", "Chunky Time");
-
          return mv;
+
        return mv;
-
      }
+
    }
-
  }
+
}

Latest revision as of 07:18, 16 December 2016

Contents

[edit] Can I use Chunk Templates in my Spring MVC project?

Yes!

Since v2.6.4 Chunk has had bean-friendly plumbing that makes it easy to use with Spring MVC, as a drop-in replacement template engine for Freemarker or Velocity.

[edit] Special features

MVC Framework localized messages (defined in messages.properties etc) are available via a custom tag command:

{% messages.msg.name %}

Messages can be parameterized like so:

{% messages.msg.name(`$a`,`$b`) %}

A special request context tag is available, usually {$rc} but the name is configurable.

The following request context values are available:

{$rc.uri}
{$rc.context_path}
{$rc.servlet_path}
{$rc.scheme}
{$rc.method}
{$rc.server_name}
{$rc.remote_addr}
{$rc.remote_host}
{$rc.remote_user}

[edit] Using bean getters in a template

When exposing beans (or POJO member variables) in a model, keep in mind that Chunk always converts camelCase to snake_case.

So, x.getFavoriteColor() will be available in the template as {% $x.favorite_color %} -- and x.isBigAndHairy() must be checked like so:

{% if $x.big_and_hairy %}...{% endif %}

[edit] Is there a sample project that I can clone?

Why yes, just head here: Sample Spring MVC project on GitHub

You'll be up and running in no time.

[edit] Getting Started

Here's what you will need:

1. pom.xml dependencies:

  ...
  <dependencies>
    <dependency>
        <groupId>com.x5dev</groupId>
        <artifactId>chunk-springmvc</artifactId>
        <version>0.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.x5dev</groupId>
        <artifactId>chunk-templates</artifactId>
        <version>3.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
    </dependencies>
  </dependencyManagement>
  ...

2. WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
     xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    <display-name>Chunky Spring</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

</web-app>

3. WebContent/WEB-INF/dispatcher-servlet.xml - provide some Theme configuration and drop in a custom view class (source below) to viewResolver.

Make sure to change com.example.myapp to the package where your annotated controllers live.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.example.myapp" />

    <bean id="chunkTemplatesConfig" class="java.util.HashMap" scope="prototype">
        <constructor-arg>
            <map key-type="java.lang.String" value-type="java.lang.String">
                <entry key="default_extension" value="chtml" />
                <entry key="cache_minutes" value="0" />
                <entry key="layers" value="" />
                <entry key="theme_path" value="" />
                <entry key="hide_errors" value="FALSE" />
                <entry key="error_log" value="" />
                <entry key="encoding" value="UTF-8" />
                <entry key="locale" value="" />
                <entry key="filters" value="" />
            </map>
        </constructor-arg>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="com.x5.template.spring.ChunkTemplateView"/>
        <property name="prefix" value="/WEB-INF/themes/"/>
        <property name="suffix" value=".chtml"/>
        <property name="requestContextAttribute" value="rc"/>
    </bean>
</beans>

4. Make a controller! The template name "helloworld" will resolve to /WEB-INF/themes/helloworld.chtml and any include/exec calls will be resolved relative to there.

package com.example.myapp;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloChunk
{
    String message = "Welcome to Spring MVC!";

    @RequestMapping("/hello")
    public ModelAndView showMessage(
            @RequestParam(value = "name", required = false, defaultValue = "World") String name)
    {
        ModelAndView mv = new ModelAndView("helloworld");
        mv.addObject("message", message);
        mv.addObject("name", name);
        mv.addObject("page_name", "Spring 4 MVC - Hello Chunk");
        return mv;
    }

    @RequestMapping(value={"/chunk","/"})
    public ModelAndView chunkTime()
    {
        ModelAndView mv = new ModelAndView("chunkworld");
        mv.addObject("page_name", "Chunky Time");
        return mv;
    }
}
Personal tools