瀏覽代碼

Beginning of Spring fortune test

Patrick Falls 12 年之前
父節點
當前提交
1af3e24776

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

@@ -0,0 +1,30 @@
+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(int id, String message)
+  {
+    this.id = id;
+    this.message = message;
+  }
+  
+  /**
+   * For our purposes, Fortunes sort by their message text. 
+   */
+  @Override
+  public int compareTo(Fortune other)
+  {
+    return message.compareTo(other.message);
+  }
+}

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

@@ -0,0 +1,41 @@
+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
+{
+
+  private static final int    DB_ROWS                = 10000;
+
+  @RequestMapping(value = "/fortune")
+  public ModelAndView fortunes()
+  {
+    
+    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);
+  }
+}

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

@@ -0,0 +1,19 @@
+<!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>

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