0

我在我的 Spring 3 MVC 应用程序中使用 Apache Tiles 2,布局是:左侧的菜单和右侧的正文。

布局.jsp

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

菜单.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 添加一个“显示更多历史”按钮以显示下一个 10。

问题是,当用户执行以下操作时:

  1. 点击历史链接,显示0-9条历史,
  2. 点击“显示更多历史”显示10到19,
  3. 点击账户链接返回账户页面,
  4. 再次单击历史链接,history.jsp 显示的不是 10 到 19,而是显示 0-9。

如何使 history.jsp 显示上次访问的历史记录,而不是从头开始显示。

我对Spring很陌生,欢迎所有建议。谢谢。

4

1 回答 1

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);

   }
于 2012-05-03T11:33:12.510 回答