2012-05-03 40 views
0

我在Spring 3 MVC應用程序中使用Apache Tiles 2,佈局是:左側菜單和右側主體。如何更新Spring MVC和Apache Tiles中的JSP

layout.jsp

<table> 
    <tr> 
    <td height="250"><tiles:insertAttribute name="menu" /></td> 
    <td width="350"><tiles:insertAttribute name="body" /></td> 
    </tr> 
</table> 

引入了menu.jsp

<div><ul> 
<li><a href="account.html">account</a></li> 
<li><a href="history.html">history</a></li></ul></div> 

history.jsp的身體

<c:forEach var="history" items="${histories}"><p><c:out value="${history}"></c:out></p></c:forEach> 

我也有歷史上的控制器

@Controller 
public class HistoryController { 

@RequestMapping("/history") 
public ModelAndView showHistory() { 

    ArrayList <String> histories = read from DB. 
    return new ModelAndView("history","histories",histories); 

    } 
} 

因此,每當我單擊菜單上的歷史鏈接時,showHistory()都會被調用。

但有一個更復雜的情況。歷史數據庫有數百個條目,因此我們決定僅在history.jsp第一次顯示時顯示前10個,然後向history.jsp添加「show more history」按鈕以通過添加另一個控制器顯示下一個10。

的問題是,當用戶執行以下操作:

  1. 點擊歷史鏈路,它示出了0-9歷史,
  2. 點擊 「表現出更多的歷史」 來顯示10〜19,
  3. 點擊賬戶鏈接返回賬戶頁面,
  4. 再次點擊歷史鏈接,而不是history.jsp顯示10到19,它顯示0-9。

如何讓history.jsp顯示上次訪問的歷史記錄,而不是從頭開始顯示。

我對春天很新,歡迎提出所有建議。 謝謝。

回答

0

你要做的是在會話中存儲最後一次請求的範圍。如果用戶沒有指定範圍(在請求中),則使用存儲在其上的會話。

這個東西

@RequestMapping("/history") 
public ModelAndView showHistory(@RequestParam(value="startIndex", defaultValue="-1") Integer startIndex, HttpSession session) { 
    Integer start = Integer.valueOf(0); 
    if (startIndex == null || startIndex.intValue() < 0) { 
     // get from session 
     Integer sessionStartIndex = (Integer) session.getAttribute("startIndex"); 
     if (sessionStartIndex != null) { 
      start = sessionStartIndex; 
     } 
    } else { 
     start = startIndex; 
    } 
    session.setAttribute("startIndex", start); 
    ArrayList <String> histories = read from DB, starting with start. 
    return new ModelAndView("history","histories",histories); 

    } 
+0

感謝狀,會議將選項。只要說清楚,在這種情況下,我只是在會話中存儲指針,如果需要存儲更多數據,更好的方法是什麼?一個網頁的HTML代碼? – user200340

+0

在會話中存儲數據(任何數據)通常沒有問題。只要您意識到這些限制。例如,如果在會話中存儲大量或不可序列化的數據,則應關閉會話序列化。另外,請牢記內存消耗。 – pap