<packaging>war</packaging>
<name>Juplo - Examples: Spring-MVC</name>
+ <dependencies>
+
+ <!-- Spring -->
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>${springframework.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-web</artifactId>
+ <version>${springframework.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-webmvc</artifactId>
+ <version>${springframework.version}</version>
+ </dependency>
+
+ </dependencies>
+
</project>
--- /dev/null
+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<Date,String> requests = new TreeMap<Date,String>();
+ private Map<String,Date> accessTimes = new HashMap<String,Date>();
+
+
+ @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
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+ 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">
<!-- Activates the AspectJ-Weaver -->
<context:component-scan base-package="de.halbekunst"/>
<constructor-arg value="3600"/>
</bean>
- <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
+ <!-- Configures the CacheControlInterceptor -->
+ <mvc:interceptors>
+ <bean class="de.halbekunst.juplo.cachecontrol.CacheControlInterceptor"/>
+ </mvc:interceptors>
+
+ <bean id="defaultAnnotationlHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="defaultHandler" value="urlFilenameViewController"/>
</bean>