【问题标题】:Spring mvc doesnt find representation for post methodSpring mvc 找不到 post 方法的表示
【发布时间】:2020-09-14 09:43:19
【问题描述】:

在将 Thymeleaf 模板映射到 Spring 控制器时遇到问题。 顺便说一句,我运行非 Spring Boot 应用程序。

这是我的模板:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div><p><b>Search and modify garage data</b></p></div>
<div>
    <p>Available places are:</p>
    <p th:text="${places}"></p>
</div>
<h3>Select option:</h3>
<br>
<p class="header">add extra places to garage:</p>
<div>
    <form method="post" >
        <input type="text" name="numberOfPlaces" placeholder="add places">
    </form>
</div>
<br>
<p class="header">book chosen place for the specific date:</p>
<div>
    <form method="post" action="setDate">
        <input type="text"  name="id_to_book" placeholder="enter the id of Place you want to book:">
        <input type="text" name="date_to_set" placeholder="enter the date to book(YYYY-MM-DD):">
        <button type="submit" placeholder="submit">submit</button>
    </form>
</div>
<br>
<div>
    <button class="button" onclick="document.location='/'">Go to main menu </button>
</div>
</body>
</html>

和控制器:

@Controller
@RequestMapping("/place_menu")
public class PlaceMenuController {

    @Autowired
    private PlaceController placeController;


    @GetMapping
    public String showPlaces(ModelMap model){
     model.put( "places", this.placeController.getPlaces() ) ;
    return "place_menu";
    }

    @PostMapping()
    public String add(@RequestParam(required = false) Integer numberOfPlaces , ModelMap model){
        this.placeController.addPlaces( numberOfPlaces  );
        model.put( "places" ,this.placeController.getPlaces())    ;
      return "place_menu";
    }
    @PostMapping ("/setDate")
    public String setDate(@RequestParam(required = false) UUID id_to_book, @RequestParam(required = false) LocalDate date_to_set, ModelMap model) {
        this.placeController.setPlaceForDate( id_to_book,date_to_set);
        model.put( "places" ,this.placeController.getPlaces())    ;
        return  "place_menu";
    }
}

“setDate”形式的提交按钮导致 404 错误:

描述 源服务器没有找到目标资源的当前表示或不愿意透露存在。

同时,第一个表单可以正常工作,我可以添加新实体并表示它们。我相信我在用控制器的方法绑定帖子表单时有一些错误,但在哪里没有胶水。

【问题讨论】:

    标签: spring model-view-controller thymeleaf


    【解决方案1】:

    在表单中添加完整路径后已修复:

    place_menu/setDate。 虽然我还没有意识到类注解的价值,因为我仍然需要手动将它添加到映射中

    【讨论】:

    • 方法级映射附加到类级映射。所以你的表单需要th:action="@{/place_menu/setDate}"是正常的。
    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2011-10-07
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    相关资源
    最近更新 更多