Browse Source

Spring fortune test

Patrick Falls 12 years ago
parent
commit
e3af87a294

+ 2 - 1
spring/benchmark_config

@@ -6,8 +6,9 @@
       "json_url": "/spring/json",
       "json_url": "/spring/json",
       "db_url": "/spring/db",
       "db_url": "/spring/db",
       "query_url": "/spring/db?queries=",
       "query_url": "/spring/db?queries=",
+      "fortune_url": "/spring/fortunes",
       "port": 8080,
       "port": 8080,
       "sort": 22
       "sort": 22
     }
     }
   }]
   }]
-}
+}

+ 15 - 0
spring/src/main/java/hello/domain/Fortune.java

@@ -13,12 +13,27 @@ public class Fortune
   public int id;
   public int id;
   public String message;
   public String message;
   
   
+  public Fortune()
+  {
+
+  }
+  
   public Fortune(int id, String message)
   public Fortune(int id, String message)
   {
   {
     this.id = id;
     this.id = id;
     this.message = message;
     this.message = message;
   }
   }
   
   
+  public int getId()
+  {
+    return this.id;
+  }
+
+  public String getMessage()
+  {
+    return this.message;
+  }
+
   /**
   /**
    * For our purposes, Fortunes sort by their message text. 
    * For our purposes, Fortunes sort by their message text. 
    */
    */

+ 13 - 10
spring/src/main/java/hello/web/HelloFortuneController.java

@@ -23,19 +23,22 @@ import org.springframework.web.servlet.ModelAndView;
 public class HelloFortuneController
 public class HelloFortuneController
 {
 {
 
 
-  private static final int    DB_ROWS                = 10000;
-
-  @RequestMapping(value = "/fortune")
+  @RequestMapping(value = "/fortunes")
   public ModelAndView fortunes()
   public ModelAndView fortunes()
   {
   {
     
     
     final Session session = HibernateUtil.getSessionFactory().openSession();
     final Session session = HibernateUtil.getSessionFactory().openSession();
-    List<Fortune> fortunes = (List<Fortune>)session.createCriteria(Fortune.class).list()
-    session.close();
-    
-    fortunes.add(new Fortune(0, "Additional fortune added at request time."));
-    Collections.sort(fortunes);
-
-    return new ModelAndView("fortunes", "command", fortunes);
+    try
+    {
+      List fortunes = new ArrayList(session.createCriteria(Fortune.class).list());
+      fortunes.add(new Fortune(0, "Additional fortune added at request time."));
+      Collections.sort(fortunes);
+
+      return new ModelAndView("fortunes", "fortunes", fortunes);
+    }
+    finally
+    {
+      session.close();
+    }
   }
   }
 }
 }

+ 1 - 0
spring/src/main/resources/hibernate.cfg.xml

@@ -14,5 +14,6 @@
         <property name="hibernate.current_session_context_class">thread</property>
         <property name="hibernate.current_session_context_class">thread</property>
 
 
         <mapping class="hello.domain.World" />
         <mapping class="hello.domain.World" />
+        <mapping class="hello.domain.Fortune" />
     </session-factory>
     </session-factory>
 </hibernate-configuration>
 </hibernate-configuration>

+ 0 - 19
spring/src/main/webapp/WEB-INF/jsp/fortune.jsp

@@ -1,19 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
-<title>Fortunes</title>
-</head>
-<body>
-<table>
-<tr>
-<th>id</th>
-<th>message</th>
-</tr>
-<c:forEach var="o" items="${fortunes}">
-<tr>
-<td>${o.id}</td>
-<td>${o.message}</td>
-</tr>
-</c:if>
-</table></body>
-</html>

+ 22 - 0
spring/src/main/webapp/WEB-INF/jsp/fortunes.jsp

@@ -0,0 +1,22 @@
+<%@page contentType="text/html" pageEncoding="UTF-8"%>
+<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
+<!DOCTYPE html>
+<html>
+<head>
+<title>Fortunes</title>
+</head>
+<body>
+<table>
+<tr>
+<th>id</th>
+<th>message</th>
+</tr>
+<c:forEach var="o" items="${fortunes}">
+<tr>
+<td>${o.getId()}</td>
+<td><spring:escapeBody htmlEscape="true">${o.getMessage()}</spring:escapeBody></body></td>
+</tr>
+</c:forEach>
+</table>
+</html>