JAVA-15787 Created new di-modules and server-modules
- Moved spring-freemarker to spring-web-modules
This commit is contained in:
+38
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.freemarker.controller;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.baeldung.freemarker.method.LastCharMethod;
|
||||
import freemarker.template.DefaultObjectWrapperBuilder;
|
||||
import freemarker.template.Version;
|
||||
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<Car> carList = new ArrayList<Car>();
|
||||
|
||||
@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";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/commons", method = RequestMethod.GET)
|
||||
public String showCommonsPage(Model model) {
|
||||
model.addAttribute("statuses", Arrays.asList("200 OK", "404 Not Found", "500 Internal Server Error"));
|
||||
model.addAttribute("lastChar", new LastCharMethod());
|
||||
model.addAttribute("random", new Random());
|
||||
model.addAttribute("statics", new DefaultObjectWrapperBuilder(new Version("2.3.28")).build().getStaticModels());
|
||||
return "commons";
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.freemarker.method;
|
||||
|
||||
import freemarker.template.TemplateMethodModelEx;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LastCharMethod implements TemplateMethodModelEx {
|
||||
public Object exec(List arguments) throws TemplateModelException {
|
||||
if (arguments.size() != 1 || StringUtils.isEmpty(arguments.get(0)))
|
||||
throw new TemplateModelException("Wrong arguments!");
|
||||
String argument = arguments.get(0).toString();
|
||||
return argument.charAt(argument.length() - 1);
|
||||
}
|
||||
}
|
||||
+33
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,28 @@
|
||||
<p>Testing is a property exists: ${student???c}</p>
|
||||
<#if status??>
|
||||
<p>Property value: ${status.reason}</p>
|
||||
<#else>
|
||||
<p>Missing property</p>
|
||||
</#if>
|
||||
|
||||
<p>Iterating a sequence:</p>
|
||||
<#list statuses>
|
||||
<ul>
|
||||
<#items as status>
|
||||
<li>${status}</li>
|
||||
</#items>
|
||||
</ul>
|
||||
<#else>
|
||||
<p>No statuses available</p>
|
||||
</#list>
|
||||
|
||||
<p>Using static methods</p>
|
||||
<#assign MathUtils=statics['java.lang.Math']>
|
||||
<p>PI value: ${MathUtils.PI}</p>
|
||||
<p>2*10 is: ${MathUtils.pow(2, 10)}</p>
|
||||
|
||||
<p>Using bean method</p>
|
||||
<p>Random value: ${random.nextInt()}</p>
|
||||
|
||||
<p>Use custom method</p>
|
||||
<p>Last char example: ${lastChar('mystring')}</p>
|
||||
@@ -0,0 +1,61 @@
|
||||
<html>
|
||||
<head><title> FreeMarker Spring MVC Hello World</title>
|
||||
|
||||
<style>
|
||||
body, input {
|
||||
font-family: Calibri, Arial;
|
||||
margin: 0px;
|
||||
padding: 0px;
|
||||
}
|
||||
#header h2 {
|
||||
color: white;
|
||||
background-color: #3275A8;
|
||||
height: 50px;
|
||||
padding: 5px 0 0 5px;
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.datatable {margin-bottom:5px;border:1px solid #eee;border-collapse:collapse;width:400px;max-width:100%;font-family:Calibri}
|
||||
.datatable th {padding:3px;border:1px solid #888;height:30px;background-color:#B2D487;text-align:center;vertical-align:middle;color:#444444}
|
||||
.datatable tr {border:1px solid #888}
|
||||
.datatable tr.odd {background-color:#eee}
|
||||
.datatable td {padding:2px;border:1px solid #888}
|
||||
#content { padding 5px; margin: 5px; text-align: center}
|
||||
|
||||
fieldset { width: 300px; padding: 5px; margin-bottom: 0px; }
|
||||
legend { font-weight: bold; }
|
||||
</style>
|
||||
|
||||
<body>
|
||||
<div id="header">
|
||||
<H2>
|
||||
FreeMarker Spring MVC Hello World
|
||||
</H2>
|
||||
</div>
|
||||
|
||||
<div id="content">
|
||||
|
||||
<fieldset>
|
||||
<legend>Add Car</legend>
|
||||
<form name="car" action="add" method="post">
|
||||
Make : <input type="text" name="make" /> <br/>
|
||||
Model: <input type="text" name="model" /> <br/>
|
||||
<input type="submit" value=" Save " />
|
||||
</form>
|
||||
</fieldset>
|
||||
|
||||
<br/>
|
||||
<table class="datatable">
|
||||
<tr>
|
||||
<th>Make</th> <th>Model</th>
|
||||
</tr>
|
||||
<#list model["carList"] as car>
|
||||
<tr>
|
||||
<td>${car.make}</td> <td>${car.model}</td>
|
||||
</tr>
|
||||
</#list>
|
||||
</table>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
|
||||
<display-name>spring4-freemarker-example</display-name>
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.html</welcome-file>
|
||||
<welcome-file>index.htm</welcome-file>
|
||||
<welcome-file>index.jsp</welcome-file>
|
||||
<welcome-file>default.html</welcome-file>
|
||||
<welcome-file>default.htm</welcome-file>
|
||||
<welcome-file>default.jsp</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import com.baeldung.freemarker.config.SpringWebConfig;
|
||||
import com.baeldung.freemarker.config.WebConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SpringWebConfig.class, WebConfiguration.class })
|
||||
@WebAppConfiguration
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user