请求方法' POST'编辑时不支持THYMELEAF

时间:2016-12-23 17:45:32

标签: java spring-boot thymeleaf

我使用的是spring boot,java和mongodb。我试图在单击按钮时编辑表格行。我当前的脚本允许我添加和删除但我无法编辑。它链接到我的方法并显示我正在编辑的值,但是当我点击提交时,我会收到一个请求方法' POST'不受支持。

控制器示例

  //adds values that are inputted in a form
    @RequestMapping(value = "/addServer", method = RequestMethod.POST)
public String saveProduct(ApServerModel apServerModel){
    apServerService.saveApServerModel(apServerModel);
    return "redirect:editApServer";
}

//update script in controller
@RequestMapping("editApServer/update/{id}")
public String update(@PathVariable String id, Model model) {
    model.addAttribute("server", apServerService.getApServerModelById(id));
    return "update";
}

@RequestMapping("editApServer/new")
public String newServer(Model model){
    model.addAttribute("server", new ApServerModel());
    return "update";
}

HTML示例

    //index.html
        // adding works this works.
        <h2>Add AppPortServer</h2>
        <form action="/addServer" method="POST">
            Host <input type="text" id="host" name="host" /><br />
            Port <input type="text" id="port" name="port" /><br />
            <input type="submit" />
        </form>

  //update.html
 //method "post" in here is throwing me an error.
<h2>Server Details</h2>
    <div>
        <form class="form-horizontal" th:object="${server}" th:action="@{/editApServer}" method="POST">
            <input type="hidden" th:field="*{id}"/>

            <div class="form-group">
                <label class="col-sm-2 control-label">Host:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{host}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Port:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{port}"/>
                </div>
            </div>

            <div class="row">
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
        </form>
    </div>

1 个答案:

答案 0 :(得分:2)

Rest中的POST方法在addServer路径中注释,但在HTML中,form方法的操作指向editApServer

th:action="@{/editApServer}"更改为th:action="@{/addServer}"