2013-11-23 52 views
1

比方说,我有这样的控制器:Spring MVC的 - 分页和请求方法

import org.java.wsg.service.ClientsService; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.support.PagedListHolder; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 

import javax.servlet.http.HttpServletRequest; 

@Controller 
public class ClientsController { 

    @Autowired 
    ClientsService clientsService; 

    @RequestMapping(value = "/clients.htm") 
    public String clientsPage(HttpServletRequest request, Model model) { 

     PagedListHolder pagedList = new PagedListHolder(clientsService.getClients()); 
     pagedList.setPageSize(50); 
     request.getSession().setAttribute("clientsList", pagedList); 

     model.addAttribute("clients", pagedList.getPageList()); 

     return "clients"; 
    } 

    @RequestMapping(value = "/clientsNavigate.htm", method = RequestMethod.POST) 
    public String clientsNavigateToPage(HttpServletRequest request, Model model, 
             @RequestParam String action) { 

     PagedListHolder pagedList = (PagedListHolder) request.getSession().getAttribute("clientsList"); 

     if (action.equals("next")) { 
      pagedList.nextPage(); 

     } else if (action.equals("previous")) { 
      pagedList.previousPage(); 
     } 

     model.addAttribute("clients", pagedList.getPageList()); 

     return "clients"; 
    } 

    @RequestMapping(value = "/clients/{id}.htm", method = RequestMethod.GET) 
    public String clientById(Model model, @PathVariable Integer id, HttpServletRequest request) { 

     PagedListHolder pagedList = new PagedListHolder(clientsService.getClientById(id)); 
     pagedList.setPageSize(50); 
     request.getSession().setAttribute("clientsList", pagedList); 

     model.addAttribute("clients", pagedList.getPageList()); 

     return "clients"; 
    } 
} 

编辑: 这里是一个clients.jsp页:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Clients</title> 
    <link rel="stylesheet" type="text/css" 
      href="<c:url value='/resources/css/bootstrap.css'/>"/> 
    <script type="text/javascript" src="<c:url value="/resources/js/bootstrap.js" />"> 

    </script> 

    <script type="text/javascript" 
      src="<c:url value="/resources/js/bootstrap.min.js" />"> 

    </script> 

</head> 
<body> 
<div class="container-fluid"> 
    <div class="row-fluid"> 

     <div class="span10 offset1"> 
      <center> 
       <h2>Clients</h2> 
      </center> 

      <table class="table table-hover"> 
       <tr> 
        <th> 
         <center>Id</center> 
        </th> 
        <th> 
         <center>Ip</center> 
        </th> 
        <th> 
         <center>Connections</center> 
        </th> 
        <th> 
         <center>GUID</center> 
        </th> 
        <th> 
         <center>Name</center> 
        </th> 
        <th> 
         <center>Group</center> 
        </th> 
        <th> 
         <center>Time added</center> 
        </th> 
        <th> 
         <center>Last seen</center> 
        </th> 
       </tr> 
       <c:forEach items="${clients}" var="c"> 
        <tr> 
         <td><c:out value="${c.id}"/></td> 
         <td><c:out value="${c.ip}"/></td> 
         <td><c:out value="${c.connections}"/></td> 
         <td><c:out value="${c.guid}"/></td> 
         <td><a href="${pageContext.request.contextPath}/clients/${c.id}.htm"><c:out 
           value="${c.name}"/></a> 
         </td> 
         <td><c:out value="${c.group}"/></td> 
         <td><c:out value="${c.timeAdd}"/></td> 
         <td><c:out value="${c.timeEdit}"/></td> 

        </tr> 
       </c:forEach> 
      </table> 
      <form:form method="post" action="clientsNavigate.htm"> 
       <input type="submit" name="action" value="previous" class="btn btn-info" /> 
       <input type="submit" name="action" value="next" class="btn btn-info" /> 

      </form:form> 
     </div> 
    </div> 
</div> 
</body> 
</html> 

正如你可以看到,第一种方法( clientsPage)获取ie Client对象列表并将它们作为属性传递给clients.jsp页面。第二种方法(clientsNavigateToPage)用于处理clients.jsp页面中的表单请求(带有两个按钮的简单表单(NextPrevious))。第三种方法用于获取多个具有某个id的客户端。但我面临的问题是在处理我的/clients/{id}.htm请求后,我无法处理我的/clientsNavigate.htm请求。

它说: HTTP Status 405 - Request method 'POST' not supported

如何处理这个问题?

+0

请注明您的HTML(或任何用于发送的形式)的代码。 –

+0

@MichałRybak完成。 –

+0

你检查过表单'action'是否正确,即该请求被发送到正确的控制器? 尝试从您的映射中移除'method = RequestMethod.POST'。虽然这似乎是正确的,但让我们检查一下是否有帮助。 –

回答

2

因为其他每一件事看起来不像一个错误,我觉得剩下的唯一问题是形式的URL。

为了您的链接您可以使用此模式:

<a href="${pageContext.request.contextPath}/clients/${c.id}.htm">...</a> 

但对于网页资产净值您使用此相对URL

<form:form method="post" 
      action="clientsNavigate.htm" 

所以我认为这将是值得一试:

<form:form method="post" 
      action="${pageContext.request.contextPath}/clientsNavigate.htm" ... 

顺便说一句:我会建议使用c:url(没有的contextPath但斜线开头),而不是

<c:url var="navFormUrl" value= "/clientsNavigate.htm" /> 
<form:form method="post" action="${navFormUrl}" ... 
+0

OP在评论中说,请求被发送到正确的控制器,所以我认为问题在其他地方。 –

+0

这两种方法都奏效!非常感谢你的男人! –

+1

@MichałRybak:“我多久告诉过你,当你消除了不可能的时候,无论如何,不​​管可能性如何,都必须是事实?”福尔摩斯,由柯南道尔爵士。 ;-) - 根据我在堆栈溢出方面的经验,我不得不说,如果你有一个程序和一个OP,并且告诉你一些不同的东西,大部分时间代码是不会说谎的。 – Ralph