Commits

Stephan Oudmaijer committed 8f4073f

resource servlet and eTag support added

  • Participants
  • Parent commits 1c0d702

Comments (0)

Files changed (11)

 
     <dependencies>
         <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-validator</artifactId>
+            <version>4.3.0.Final</version>
+        </dependency>
+        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-core</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
+            <artifactId>spring-context-support</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
             <artifactId>spring-jdbc</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>${spring.version}</version>
         </dependency>

src/main/java/com/oudmaijer/administratie/config/SpringConfig.java

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.core.io.ClassPathResource;
 import org.springframework.web.servlet.View;
 import org.springframework.web.servlet.ViewResolver;
 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.ContentNegotiatingViewResolver;
 import org.springframework.web.servlet.view.UrlBasedViewResolver;
 import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
 import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
 import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
-import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
-import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
 
 import java.util.ArrayList;
 import java.util.List;
 @Configuration
 @ComponentScan("com.oudmaijer.administratie")
 @EnableWebMvc
-public class SpringConfig {
+public class SpringConfig extends WebMvcConfigurerAdapter {
 
     @Bean
     public FreeMarkerConfigurer freeMarkerConfigurer() {
-        FreeMarkerConfigurer velocityConfigurer = new FreeMarkerConfigurer();
-        velocityConfigurer.setTemplateLoaderPath("/WEB-INF/view/");
-        velocityConfigurer.setConfigLocation(new ClassPathResource("freemarker.properties"));
-        return velocityConfigurer;
+        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
+        freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/view/");
+        return freeMarkerConfigurer;
     }
 
     @Bean
         return viewResolver;
     }
 
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/view/static/");
+    }
+
     @Bean
     public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
         ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();

src/main/java/com/oudmaijer/administratie/config/SpringWebInitializer.java

 
 import org.springframework.web.WebApplicationInitializer;
 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
+import org.springframework.web.filter.ShallowEtagHeaderFilter;
 import org.springframework.web.servlet.DispatcherServlet;
 
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRegistration;
+import javax.servlet.*;
+import java.util.EnumSet;
 
 public class SpringWebInitializer implements WebApplicationInitializer {
+
     public void onStartup(ServletContext servletContext)
-        throws ServletException {
+            throws ServletException {
         AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
         mvcContext.register(SpringConfig.class);
 
+        FilterRegistration fr = servletContext.addFilter("etagFilter", new ShallowEtagHeaderFilter());
+        fr.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "dispatcher");
+
         ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext));
         dispatcher.setLoadOnStartup(1);
         dispatcher.addMapping("/*");

src/main/java/com/oudmaijer/administratie/web/InvoiceController.groovy

 package com.oudmaijer.administratie.web
 
-import org.springframework.web.bind.annotation.RequestMapping
 import com.oudmaijer.administratie.domain.invoice.Invoice
-import org.springframework.ui.Model
-import org.springframework.web.bind.annotation.ResponseBody
-import org.springframework.web.bind.annotation.RequestMethod
+import javax.validation.Valid
 import org.springframework.stereotype.Controller
-import org.springframework.web.bind.annotation.PathVariable
+import org.springframework.ui.Model
+import org.springframework.web.bind.annotation.*
 
 @Controller
 @RequestMapping("/invoice")
 
     @RequestMapping(value = "/add", method = RequestMethod.PUT)
     @ResponseBody
-    public Model addInvoice(Model model) {
+    public Model addInvoice(@RequestBody Invoice invoice, Model model) {
         model.addAttribute("result", "invoice/list");
         return model;
     }
 
     @RequestMapping(value = "/update", method = RequestMethod.POST)
     @ResponseBody
-    public Model updateInvoice(Model model) {
+    public Model updateInvoice(@Valid @RequestBody Invoice invoice, Model model) {
         model.addAttribute("result", "invoice/list");
         return model;
     }

src/main/resources/freemarker.properties

-velocimacro.library=/template/template.vm

src/main/webapp/WEB-INF/view/home.ftl

-<@template>
+<#import "lib/template.ftl" as t>
+<@t.template title="Welkom">
     <div>
         <p>Facturen</p>
         <ul>
             <li><a href="invoice/new">Nieuw</a></li>
         </ul>
     </div>
-</@template>
+</@t.template>

src/main/webapp/WEB-INF/view/invoice/list.ftl

-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>
-<body>
+<#import "../lib/template.ftl" as t>
+
 <div>
     <p>Facturen</p>
     <table>
-        #foreach($item in $invoices)
-            <tr>
-                <td>$!item.id</td>
-                <td>$!item.date</td>
-                <td>$!item.remarks</td>
-            </tr>
-        #end
+    <#foreach item in invoices>
+        <tr>
+            <td>${item.id!}</td>
+            <td>${item.date!}</td>
+            <td>${item.remarks!}</td>
+        </tr>
+    </#foreach>
     </table>
-</div>
-</body>
-</html>
+</div>

src/main/webapp/WEB-INF/view/invoice/new.ftl

-Nieuwe factuur
+<#import "../lib/template.ftl" as t>
+
+<t.template title="Nieuwe factuur">
+    Nieuwe factuur
+</t.template>

src/main/webapp/WEB-INF/view/lib/template.ftl

+<#macro template title>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head>
+    <title>${title}</title>
+</head>
+<body>
+<#nested>
+</body>
+</html>
+</#macro>

src/main/webapp/WEB-INF/view/static/js/bla.js

Empty file added.

src/main/webapp/WEB-INF/view/template/template.ftl

-#macro( template )
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>
-<body>
-$!bodyContent
-</body>
-</html>
-#end