簡體   English   中英

Spring MVC控制器方法要求GET但不要求POST

[英]Spring MVC controller method called for GET but not for POST

尊敬的同事們,我在Controller類中為URL“ / persons / search”定義了兩種方法。 一種是使用RequestMethod.Get,另一種是使用RequestMethod.POST。 具有GET請求的方法被調用,但是具有POST請求的方法未被調用。 在瀏覽器中,當我加載/ persons / search並發送GET請求時,視圖將成功返回。 但是,當我填寫搜索字段並按Submit(生成POST)時,我收到404。我試圖在該方法中打印值以檢查它是否被調用,但不打印這些值。 該方法永遠不會被調用。 請注意,幾天前它運行良好,與此同時,我剛剛實施了spring安全,我認為這應該不會造成任何危害,但是它不會起作用。 我已允許對/ persons / **的所有請求。 任何有關可能導致此問題的幫助將不勝感激。 問候

控制器:

@RequestMapping(value = "/persons/search", method = RequestMethod.GET)
public String searchPersons(Model model) {
    logger.debug("searchPersons()");
    return "users/searchPersons";

}

@RequestMapping(value = "/persons/search", method = RequestMethod.POST)
public String listPersons(@RequestParam("searchString") String searchStr, Model model) {
    logger.debug("listPersons()");
    System.out.println("Entered search");
    userService.initialize();
    institutions = userService.getInstitutions();
    model.addAttribute("persons", userService.searchPersons(searchStr));
    System.out.println("completed search");
    return "users/listPersons";
}

視圖:

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

<!DOCTYPE html>
<html lang="en">

<jsp:include page="../fragments/header.jsp" />

<body>

<div class="container">

    <c:if test="${not empty msg}">
        <div class="alert alert-${css} alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <strong>${msg}</strong>
        </div>
    </c:if>
    <h1>Search Persons</h1>

<!-- The form -->
    <spring:url value="/persons/search" var="searchPersonsActionUrl" />
    <form class="searchBar" action="${searchPersonsActionUrl}" method="POST">
      <input type="text" placeholder="Enter uid or fullname..." name="searchString">
      <button type="submit"><i class="fa fa-search"></i></button>
    </form>

</div>

<jsp:include page="../fragments/footer.jsp" />

</body>
</html>

瀏覽器的“網絡”標簽的屏幕截圖:

在此處輸入圖片說明

較新的spring安全版本默認情況下啟用csrf,有時會導致奇怪的錯誤。 嘗試在安全性配置中通過.csrf().disable() ,或在此處查看: https : .csrf().disable()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM