From 50ced775c8833024f6afe9139e6299e985279e17 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Thu, 2 Aug 2012 08:52:28 +0200 Subject: [PATCH] Spring-Beispiel um eine View mit Controller erweitert --- examples/pom.xml | 1 - examples/spring/pom.xml | 21 ++++++ .../examples/spring/SpringController.java | 65 +++++++++++++++++++ examples/spring/src/main/resources/config.xml | 11 +++- .../src/main/webapp/WEB-INF/views/index.jsp | 1 + 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 examples/spring/src/main/java/de/halbekunst/juplo/examples/spring/SpringController.java diff --git a/examples/pom.xml b/examples/pom.xml index cf45de55..1905f525 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -28,7 +28,6 @@ ${pom.parent.groupId} juplo-cachecontrol ${pom.parent.version} - runtime diff --git a/examples/spring/pom.xml b/examples/spring/pom.xml index 90010cce..b6924e41 100644 --- a/examples/spring/pom.xml +++ b/examples/spring/pom.xml @@ -14,4 +14,25 @@ war Juplo - Examples: Spring-MVC + + + + + org.springframework + spring-context + ${springframework.version} + + + org.springframework + spring-web + ${springframework.version} + + + org.springframework + spring-webmvc + ${springframework.version} + + + + diff --git a/examples/spring/src/main/java/de/halbekunst/juplo/examples/spring/SpringController.java b/examples/spring/src/main/java/de/halbekunst/juplo/examples/spring/SpringController.java new file mode 100644 index 00000000..2ce8f4e3 --- /dev/null +++ b/examples/spring/src/main/java/de/halbekunst/juplo/examples/spring/SpringController.java @@ -0,0 +1,65 @@ +package de.halbekunst.juplo.examples.spring; + +import de.halbekunst.juplo.cachecontrol.annotations.CacheSeconds; +import de.halbekunst.juplo.cachecontrol.annotations.Cacheable; +import de.halbekunst.juplo.cachecontrol.annotations.LastModified; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; +import javax.servlet.http.HttpServletRequest; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + + +/** + * Simple Spring-MVC Controller + * @author kai + */ +@Controller +@Cacheable(eager=true) +public class SpringController +{ + public static final String ACCESS_TIME = SpringController.class.getCanonicalName() + ".ACCESS_TIME"; + public static final Integer DEFAULT_MAX_AGE = 60; + + private Date lastModified = new Date(); + private Map requests = new TreeMap(); + private Map accessTimes = new HashMap(); + + + @CacheSeconds + public int cacheSeconds(HttpServletRequest request) { + String maxAge = request.getParameter("max-age"); + if (maxAge != null && maxAge.matches("^\\s*\\d+\\s*$")) + return Integer.parseInt(maxAge); + else + return DEFAULT_MAX_AGE; + } + + @LastModified + public long lastModified(HttpServletRequest request) { + String query = request.getQueryString(); + query = query == null ? "" : query; + Date accessTime = accessTimes.get(query); + if (accessTime == null || !accessTime.equals(lastModified)) + /** Neuer Zugriff! */ + accessTime = new Date(); + request.setAttribute(ACCESS_TIME, accessTime); + return accessTime.getTime(); + } + + @RequestMapping("/spring-controller.html") + public ModelAndView process(HttpServletRequest request) + { + lastModified = (Date)request.getAttribute(ACCESS_TIME); + String query = request.getQueryString(); + query = query == null ? "" : query; + requests.put(lastModified, query); + accessTimes.put(query, lastModified); + ModelAndView mav = new ModelAndView("controller-view"); + mav.addObject("requests", requests); + return mav; + } +} \ No newline at end of file diff --git a/examples/spring/src/main/resources/config.xml b/examples/spring/src/main/resources/config.xml index b7a4892e..f77073c1 100644 --- a/examples/spring/src/main/resources/config.xml +++ b/examples/spring/src/main/resources/config.xml @@ -2,9 +2,11 @@ + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd + http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> @@ -26,7 +28,12 @@ - + + + + + + diff --git a/examples/spring/src/main/webapp/WEB-INF/views/index.jsp b/examples/spring/src/main/webapp/WEB-INF/views/index.jsp index 020f6ee7..f86ca57f 100644 --- a/examples/spring/src/main/webapp/WEB-INF/views/index.jsp +++ b/examples/spring/src/main/webapp/WEB-INF/views/index.jsp @@ -9,6 +9,7 @@

Simple Spring-MVC Examples

This page was delivered via SPRING!

Note:

-- 2.20.1