JSP - 如何组合过滤器和分页?

时间:2017-07-13 13:39:43

标签: html jsp spring-mvc

我正在使用Spring开发一个应用程序,而我却试图在我的页面中结合使用过滤器和分页。

独立地,我的过滤器和我的分页工作正常,我能够更改页面并过滤我的团队列表。

当我应用过滤器然后尝试更改页面时,会出现问题。我的过滤器会自动重置。

点击form链接后,有没有办法保留href信息?

我还尝试添加一个"提交按钮"对于每个页码,但没有成功。

实现此解决方案的最佳方法是什么?

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<?xml version="1.0" encoding="ISO-8859-1" ?>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>List of teams</title>
        <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/lista.css"/>
    </head>
    <body>
        <h1>List of teams</h1>
        <p>Here you can see the list of the teams, edit them, remove or update.</p>

        <form:form method="POST" action="/SpringMVCTeams/team/list">
            <table>
                <tr>
                    <td><label>Country</label></td>
                    <td>
                        <form:select path="country">
                            <form:option value="default-value" label="--Please Select" />
                            <form:options items="${countryList}" itemValue="idCountry" itemLabel="name" />
                        </form:select>          
                    </td>
                    <td><form:errors path="country" cssClass="error" /></td>
                </tr>
                <tr>
                    <td><label>Division</label></td>
                    <td>
                        <form:select path="division">
                            <form:option value="default-value" label="--Please Select" />
                            <form:options items="${divisionList}" itemValue="idDivision" itemLabel="name" />
                        </form:select>          
                    </td>
                    <td><form:errors path="division" cssClass="error" /></td>
                </tr>     
                <tr>
                    <td colspan="2">
                        <input type="submit" value="Submit"/>
                    </td>
                </tr>
            </table>

            <c:forEach begin="1" end="${paginationProducts}" var="p">
                <a href="/SpringMVCTeams/team/list/${p}">${p}</a>
            </c:forEach>


        </form:form>

        <table border="1px" cellpadding="0" cellspacing="0" >
            <thead>
                <tr>
                    <th width="10%">id</th>
                    <th width="15%">name</th>
                    <th width="10%">rating</th>
                    <th width="10%">actions</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="team" items="${teams}">
                <tr>
                    <td>${team.id}</td>
                    <td>${team.name}</td>
                    <td>${team.rating}</td>
                    <td>
                    <a href="${pageContext.request.contextPath}/team/edit/${team.id}.html">Edit</a><br/>
                    <a href="${pageContext.request.contextPath}/team/delete/${team.id}.html">Delete</a><br/>
                    </td>
                </tr>
                </c:forEach>
            </tbody>
        </table>

        <p><a href="${pageContext.request.contextPath}/index.html">Home page</a></p>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我设法做到了。

我添加了提交不同value信息的按钮。然后,在控制器中,我验证了参数action的值。

JSP页面

    <form:form method="POST" action="/SpringMVCTeams/team/list">
        <table>
            <tr>
                <td><label>Country</label></td>
                    <td>
                    <form:select path="country">
                        <form:option value="default-value" label="--Please Select" />
                        <form:options items="${countryList}" itemValue="idCountry" itemLabel="name" />
                    </form:select>          
                </td>
                    <td><form:errors path="country" cssClass="error" /></td>
            </tr>
            <tr>
                <td><label>Division</label></td>
                <td>
                    <form:select path="division">
                        <form:option value="default-value" label="--Please Select" />
                        <form:options items="${divisionList}" itemValue="idDivision" itemLabel="name" />
                    </form:select>          
                </td>
                <td><form:errors path="division" cssClass="error" /></td>
            </tr>     
            <tr>
                <td colspan="2">
                    <button type="submit" name="action" value="1" class="btn-link">Submit</button>
                </td>
            </tr>
        </table>

        <c:forEach begin="1" end="${paginationProducts}" var="p">
                <button type="submit" name="action" value="${p}" class="btn-link">${p}</button>
            </c:forEach>

    </form:form>

<强>控制器:

    @RequestMapping(value="/list" , method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listOfTeams(HttpServletRequest request, TeamList teamList) {
    String action= request.getParameter("action");

    if(action == null)
        return getlistOfTeams(teamList, 1);
    else
        return getlistOfTeams(teamList, Integer.parseInt(action));
}

有人能建议更好的解决方案吗?