diff --git a/spring-freemarker-example/.classpath b/spring-freemarker-example/.classpath new file mode 100644 index 0000000000..5c3ac53820 --- /dev/null +++ b/spring-freemarker-example/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-freemarker-example/.gitignore b/spring-freemarker-example/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/spring-freemarker-example/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/spring-freemarker-example/.project b/spring-freemarker-example/.project new file mode 100644 index 0000000000..34e2c1620c --- /dev/null +++ b/spring-freemarker-example/.project @@ -0,0 +1,48 @@ + + + spring4-freemarker-example + + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/spring-freemarker-example/pom.xml b/spring-freemarker-example/pom.xml new file mode 100644 index 0000000000..a064d7f57b --- /dev/null +++ b/spring-freemarker-example/pom.xml @@ -0,0 +1,85 @@ + + 4.0.0 + com.freemarker.example + spring4-freemarker-example + war + 1.0-SNAPSHOT + Spring Freemarker Example + + + 1.8 + 4.2.2.RELEASE + 1.1.3 + 1.7.12 + 3.1.0 + false + + + + + + org.springframework + spring-webmvc + ${spring.version} + + + commons-logging + commons-logging + + + + + + org.springframework + spring-context-support + ${spring.version} + + + + + org.slf4j + jcl-over-slf4j + ${jcl.slf4j.version} + + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + javax.servlet + javax.servlet-api + ${servletapi.version} + provided + + + + + org.freemarker + freemarker + 2.3.20 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${jdk.version} + ${jdk.version} + true + + + + + + \ No newline at end of file diff --git a/spring-freemarker-example/src/main/java/com/baeldung/freemarker/config/SpringWebConfig.java b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/config/SpringWebConfig.java new file mode 100644 index 0000000000..77f0712aca --- /dev/null +++ b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/config/SpringWebConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.freemarker.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; +import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; + +@EnableWebMvc +@Configuration +@ComponentScan({"com.baeldung.freemarker"}) +public class SpringWebConfig extends WebMvcConfigurerAdapter { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Bean + public FreeMarkerViewResolver freemarkerViewResolver() { + FreeMarkerViewResolver resolver = new FreeMarkerViewResolver(); + resolver.setCache(true); + resolver.setPrefix(""); + resolver.setSuffix(".ftl"); + return resolver; + } + + @Bean + public FreeMarkerConfigurer freemarkerConfig() { + FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); + freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/ftl/"); + return freeMarkerConfigurer; + } + +} \ No newline at end of file diff --git a/spring-freemarker-example/src/main/java/com/baeldung/freemarker/config/WebConfiguration.java b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/config/WebConfiguration.java new file mode 100644 index 0000000000..3d5ab964e7 --- /dev/null +++ b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/config/WebConfiguration.java @@ -0,0 +1,22 @@ +package com.baeldung.freemarker.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfiguration extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getServletConfigClasses() { + return new Class[] { SpringWebConfig.class }; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/" }; + } + + @Override + protected Class[] getRootConfigClasses() { + return null; + } + +} \ No newline at end of file diff --git a/spring-freemarker-example/src/main/java/com/baeldung/freemarker/controller/SpringController.java b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/controller/SpringController.java new file mode 100644 index 0000000000..b911a4975b --- /dev/null +++ b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/controller/SpringController.java @@ -0,0 +1,48 @@ +package com.baeldung.freemarker.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import com.baeldung.freemarker.model.Car; + +@Controller +public class SpringController { + + private static List carList = new ArrayList(); + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String home(Locale locale, Model model) { + return "redirect:/cars"; + } + + static { + carList.add(new Car("Honda", "Civic")); + carList.add(new Car("Toyota", "Camry")); + carList.add(new Car("Nissan", "Altima")); + } + + + @RequestMapping(value = "/cars", method = RequestMethod.GET) + public String init(@ModelAttribute("model") ModelMap model) { + model.addAttribute("carList", carList); + return "index"; + } + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public String addCar(@ModelAttribute("car") Car car) { + if (null != car && null != car.getMake() && null != car.getModel() + && !car.getMake().isEmpty() && !car.getModel().isEmpty()) { + carList.add(car); + } + return "redirect:/cars"; + } + +} diff --git a/spring-freemarker-example/src/main/java/com/baeldung/freemarker/model/Car.java b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/model/Car.java new file mode 100644 index 0000000000..e400d87608 --- /dev/null +++ b/spring-freemarker-example/src/main/java/com/baeldung/freemarker/model/Car.java @@ -0,0 +1,33 @@ +package com.baeldung.freemarker.model; + +public class Car { + + private String make; + private String model; + + public Car(){ + + } + + public Car(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + +} diff --git a/spring-freemarker-example/src/main/resources/log4j.xml b/spring-freemarker-example/src/main/resources/log4j.xml new file mode 100644 index 0000000000..ae7d4b5ca4 --- /dev/null +++ b/spring-freemarker-example/src/main/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-freemarker-example/src/main/webapp/WEB-INF/views/ftl/index.ftl b/spring-freemarker-example/src/main/webapp/WEB-INF/views/ftl/index.ftl new file mode 100644 index 0000000000..899c9b9271 --- /dev/null +++ b/spring-freemarker-example/src/main/webapp/WEB-INF/views/ftl/index.ftl @@ -0,0 +1,61 @@ + + FreeMarker Spring MVC Hello World + + + + + + +
+ +
+ Add Car +
+ Make :
+ Model:
+ +
+
+ +
+ + + + + <#list model["carList"] as car> + + + + +
Make Model
${car.make} ${car.model}
+ +
+ + \ No newline at end of file diff --git a/spring-freemarker-example/src/test/resources/log4j.xml b/spring-freemarker-example/src/test/resources/log4j.xml new file mode 100644 index 0000000000..9b1538d31a --- /dev/null +++ b/spring-freemarker-example/src/test/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +