Spring-Beispiel um eine View mit Controller erweitert
authorKai Moritz <kai@coolibri.de>
Thu, 2 Aug 2012 06:52:28 +0000 (08:52 +0200)
committerKai Moritz <kai@coolibri.de>
Thu, 2 Aug 2012 09:31:23 +0000 (11:31 +0200)
examples/pom.xml
examples/spring/pom.xml
examples/spring/src/main/java/de/halbekunst/juplo/examples/spring/SpringController.java [new file with mode: 0644]
examples/spring/src/main/resources/config.xml
examples/spring/src/main/webapp/WEB-INF/views/index.jsp

index cf45de5..1905f52 100644 (file)
@@ -28,7 +28,6 @@
       <groupId>${pom.parent.groupId}</groupId>
       <artifactId>juplo-cachecontrol</artifactId>
       <version>${pom.parent.version}</version>
-      <scope>runtime</scope>
     </dependency>
 
     <!-- JSP-Stuff... -->
index 90010cc..b6924e4 100644 (file)
   <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>
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 (file)
index 0000000..2ce8f4e
--- /dev/null
@@ -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<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
index b7a4892..f77073c 100644 (file)
@@ -2,9 +2,11 @@
 <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>
 
index 020f6ee..f86ca57 100644 (file)
@@ -9,6 +9,7 @@
     <h1>Simple Spring-MVC Examples</h1>
     <ul>
       <li><a href="/spring-page.html">Simple Spring-View</a></li>
+      <li><a href="/spring-controller.html">Simple Spring-Controller</a></li>
     </ul>
     <p>This page was delivered via SPRING!</p>
     <h2>Note:</h2>