Skip to content

Commit 197cf5b

Browse files
committed
Freemarker Spring Demo
1 parent 24de497 commit 197cf5b

11 files changed

Lines changed: 455 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
34+
</attributes>
35+
</classpathentry>
36+
<classpathentry kind="output" path="target/classes"/>
37+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

‎spring-freemarker-example/.project‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring4-freemarker-example</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.wst.validation.validationbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
<buildCommand>
34+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
38+
</buildSpec>
39+
<natures>
40+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
41+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
42+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
43+
<nature>org.eclipse.jdt.core.javanature</nature>
44+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
45+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
46+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
47+
</natures>
48+
</projectDescription>

‎spring-freemarker-example/pom.xml‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.freemarker.example</groupId>
5+
<artifactId>spring4-freemarker-example</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>Spring Freemarker Example</name>
9+
10+
<properties>
11+
<jdk.version>1.8</jdk.version>
12+
<spring.version>4.2.2.RELEASE</spring.version>
13+
<logback.version>1.1.3</logback.version>
14+
<jcl.slf4j.version>1.7.12</jcl.slf4j.version>
15+
<servletapi.version>3.1.0</servletapi.version>
16+
<failOnMissingWebXml>false</failOnMissingWebXml>
17+
</properties>
18+
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-webmvc</artifactId>
24+
<version>${spring.version}</version>
25+
<exclusions>
26+
<exclusion>
27+
<groupId>commons-logging</groupId>
28+
<artifactId>commons-logging</artifactId>
29+
</exclusion>
30+
</exclusions>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-context-support</artifactId>
36+
<version>${spring.version}</version>
37+
</dependency>
38+
39+
<!-- Logging -->
40+
<dependency>
41+
<groupId>org.slf4j</groupId>
42+
<artifactId>jcl-over-slf4j</artifactId>
43+
<version>${jcl.slf4j.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>ch.qos.logback</groupId>
48+
<artifactId>logback-classic</artifactId>
49+
<version>${logback.version}</version>
50+
</dependency>
51+
52+
<!-- compile only, deployed container will provide this -->
53+
<!-- Need this for config annotation -->
54+
<dependency>
55+
<groupId>javax.servlet</groupId>
56+
<artifactId>javax.servlet-api</artifactId>
57+
<version>${servletapi.version}</version>
58+
<scope>provided</scope>
59+
</dependency>
60+
61+
<!-- Freemarker -->
62+
<dependency>
63+
<groupId>org.freemarker</groupId>
64+
<artifactId>freemarker</artifactId>
65+
<version>2.3.20</version>
66+
</dependency>
67+
68+
</dependencies>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-compiler-plugin</artifactId>
75+
<version>3.3</version>
76+
<configuration>
77+
<source>${jdk.version}</source>
78+
<target>${jdk.version}</target>
79+
<failOnMissingWebXml>true</failOnMissingWebXml>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
85+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.freemarker.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
10+
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
11+
12+
@EnableWebMvc
13+
@Configuration
14+
@ComponentScan({"com.baeldung.freemarker"})
15+
public class SpringWebConfig extends WebMvcConfigurerAdapter {
16+
17+
@Override
18+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
19+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
20+
}
21+
22+
@Bean
23+
public FreeMarkerViewResolver freemarkerViewResolver() {
24+
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
25+
resolver.setCache(true);
26+
resolver.setPrefix("");
27+
resolver.setSuffix(".ftl");
28+
return resolver;
29+
}
30+
31+
@Bean
32+
public FreeMarkerConfigurer freemarkerConfig() {
33+
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
34+
freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/ftl/");
35+
return freeMarkerConfigurer;
36+
}
37+
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.freemarker.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class WebConfiguration extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getServletConfigClasses() {
9+
return new Class[] { SpringWebConfig.class };
10+
}
11+
12+
@Override
13+
protected String[] getServletMappings() {
14+
return new String[] { "/" };
15+
}
16+
17+
@Override
18+
protected Class<?>[] getRootConfigClasses() {
19+
return null;
20+
}
21+
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.freemarker.controller;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Locale;
6+
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.ui.ModelMap;
10+
import org.springframework.web.bind.annotation.ModelAttribute;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
14+
import com.baeldung.freemarker.model.Car;
15+
16+
@Controller
17+
public class SpringController {
18+
19+
private static List<Car> carList = new ArrayList<Car>();
20+
21+
@RequestMapping(value = "/", method = RequestMethod.GET)
22+
public String home(Locale locale, Model model) {
23+
return "redirect:/cars";
24+
}
25+
26+
static {
27+
carList.add(new Car("Honda", "Civic"));
28+
carList.add(new Car("Toyota", "Camry"));
29+
carList.add(new Car("Nissan", "Altima"));
30+
}
31+
32+
33+
@RequestMapping(value = "/cars", method = RequestMethod.GET)
34+
public String init(@ModelAttribute("model") ModelMap model) {
35+
model.addAttribute("carList", carList);
36+
return "index";
37+
}
38+
39+
@RequestMapping(value = "/add", method = RequestMethod.POST)
40+
public String addCar(@ModelAttribute("car") Car car) {
41+
if (null != car && null != car.getMake() && null != car.getModel()
42+
&& !car.getMake().isEmpty() && !car.getModel().isEmpty()) {
43+
carList.add(car);
44+
}
45+
return "redirect:/cars";
46+
}
47+
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.freemarker.model;
2+
3+
public class Car {
4+
5+
private String make;
6+
private String model;
7+
8+
public Car(){
9+
10+
}
11+
12+
public Car(String make, String model) {
13+
this.make = make;
14+
this.model = model;
15+
}
16+
17+
public String getMake() {
18+
return make;
19+
}
20+
21+
public void setMake(String make) {
22+
this.make = make;
23+
}
24+
25+
public String getModel() {
26+
return model;
27+
}
28+
29+
public void setModel(String model) {
30+
this.model = model;
31+
}
32+
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
3+
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
4+
5+
<!-- Appenders -->
6+
<appender name="console" class="org.apache.log4j.ConsoleAppender">
7+
<param name="Target" value="System.out" />
8+
<layout class="org.apache.log4j.PatternLayout">
9+
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
10+
</layout>
11+
</appender>
12+
13+
<!-- Application Loggers -->
14+
<logger name="com.spring.freemarker">
15+
<level value="info" />
16+
</logger>
17+
18+
<!-- 3rdparty Loggers -->
19+
<logger name="org.springframework.core">
20+
<level value="info" />
21+
</logger>
22+
23+
<logger name="org.springframework.beans">
24+
<level value="info" />
25+
</logger>
26+
27+
<logger name="org.springframework.context">
28+
<level value="info" />
29+
</logger>
30+
31+
<logger name="org.springframework.web">
32+
<level value="info" />
33+
</logger>
34+
35+
<!-- Root Logger -->
36+
<root>
37+
<priority value="warn" />
38+
<appender-ref ref="console" />
39+
</root>
40+
41+
</log4j:configuration>

0 commit comments

Comments
 (0)