Browse Source

Merge branch 'fortune'

Patrick Falls 12 years ago
parent
commit
5be3c5affb

+ 2 - 1
spring/benchmark_config

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

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

@@ -0,0 +1,45 @@
+package hello.domain;
+
+import java.util.*;
+
+import javax.persistence.*;
+
+@Entity
+public class Fortune
+  implements Comparable<Fortune>
+{
+  @Id
+  @GeneratedValue(strategy = GenerationType.IDENTITY)
+  public int id;
+  public String message;
+  
+  public Fortune()
+  {
+
+  }
+  
+  public Fortune(int id, String message)
+  {
+    this.id = id;
+    this.message = message;
+  }
+  
+  public int getId()
+  {
+    return this.id;
+  }
+
+  public String getMessage()
+  {
+    return this.message;
+  }
+
+  /**
+   * For our purposes, Fortunes sort by their message text. 
+   */
+  @Override
+  public int compareTo(Fortune other)
+  {
+    return message.compareTo(other.message);
+  }
+}

+ 44 - 0
spring/src/main/java/hello/web/HelloFortuneController.java

@@ -0,0 +1,44 @@
+package hello.web;
+
+import hello.domain.*;
+
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+import javax.servlet.http.*;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+
+import org.springframework.http.*;
+import org.springframework.http.converter.*;
+import org.springframework.http.converter.json.*;
+import org.springframework.http.server.*;
+import org.springframework.stereotype.*;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.servlet.ModelAndView;
+
+@Controller
+public class HelloFortuneController
+{
+
+  @RequestMapping(value = "/fortunes")
+  public ModelAndView fortunes()
+  {
+    
+    final Session session = HibernateUtil.getSessionFactory().openSession();
+    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>
 
         <mapping class="hello.domain.World" />
+        <mapping class="hello.domain.Fortune" />
     </session-factory>
 </hibernate-configuration>

+ 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>

+ 5 - 0
spring/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

@@ -16,5 +16,10 @@
  
     <!-- Enables the Spring MVC @Controller programming model -->
     <mvc:annotation-driven />
+    
+    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+          <property name="prefix" value="/WEB-INF/jsp/" />
+          <property name="suffix" value=".jsp" />
+       </bean>
  
 </beans>