Spring MVC
From Chunk Java Template Engine
Tom McClure (Talk | contribs) |
Tom McClure (Talk | contribs) (→Can I use Chunk Templates in my Spring MVC project?) |
||
(10 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
- | Chunk | + | == 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> | |
- | + | '''<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 | 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. | 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 | + | Make sure to change <code>com.example.myapp</code> 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; | |
- | + | } | |
- | + | } |
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; } }