SpringMVC - 将POST中创建的对象传递给get方法(在同一页面上显示搜索结果)

时间:2017-10-25 13:54:05

标签: spring jsp spring-mvc controller

我是JEE的新手,我试图在我的WebService中创建搜索页面,我使用SpringMVC,我可以在我的控制器中搜索并获得结果(当我调用POST时我的观点)但我无法将包含结果的对象传递给我的控制器的GET方法。

以下是我的控制器方法:

@RequestMapping(value = "/search", method = RequestMethod.GET)
public String searchPage(ModelMap model) {
    String movie = null;
    model.addAttribute("movie", movie);
    return "search";
}


@RequestMapping(value = "/search", method=RequestMethod.POST)
public String searchMovie(@RequestParam String movie, ModelMap model) throws IOException {
    movieService.setMovieName(movie);
    BaseResultsPage<BaseMovie> searchResults = movieService.movieSearch();
    System.out.println("search :" +searchResults.page);
    model.addAttribute("search_results", searchResults);

    return "search";
}

我想在以下视图中访问该对象:

&#13;
&#13;
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta name="description" content=""/>
    <meta name="author" content=""/>
    <link rel="icon" href="../../favicon.ico"/>
    <title>Sign up</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
          crossorigin="anonymous">
    <!-- Custom styles for this template-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/app.css"/>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<header>
    <h3>Search Movies</h3>
</header>
<c:url var="loginUrl" value="/login" />
<form action="${searchUrl}" method="post">
    <div class="group">
        <input type="text" id="movie" name="movie"/>
        <span class="highlight"></span>
        <span class="bar"></span>
        <label>Movie Title</label>
    </div>
    <input type="hidden" name="${_csrf.parameterName}"  value="${_csrf.token}" />
    <div class="form-actions">
        <input class="btn btn-primary" type="submit" value="Search"/>
    </div>
</form>
<div>
    <c:if test="${seach_results == null}">
    <table style="border-collapse: collapse;" border="1" class="showResults">
        <tr>
            <td colspan="7">No Results found</td>
        </tr>
    </table>
    </c:if>
    <c:if test="${seach_results != null}">
    <table style="border-collapse: collapse;" border="1" class="showResults">
        <tr>
            <td colspan="7">Result found</td>
        </tr>
    </table>
    </c:if>
</body>
<script>
    $(document).ready(function() {
        $('input').blur(function () {
            var $this = $(this);
            if ($this.val())
                $this.addClass('used');
            else
                $this.removeClass('used');
        });
    });
</script>
</html>
&#13;
&#13;
&#13;

我希望能够在视图中访问searchResults(并检测它是否为空)。

谢谢你的帮助!