javascript - Thymeleaf - 尝试为每个图像创建一个删除按钮

标签 javascript html thymeleaf

所以,我想为每个可用图像创建一个删除按钮。问题是当我单击删除按钮时,它甚至没有转到 Controller

Controller

@Autowired
private PropertyClientService propertyClientService;

@GetMapping(value = {"/property/list"})
public ModelAndView list() {
    return new ModelAndView("property/list.html");
}

@GetMapping(value = {"/property/edit/{id}"})
public String edit(@PathVariable Long id, Model model) {
    model.addAttribute("property", propertyClientService.get(id));
    return "property/edit";
}

 @DeleteMapping(value = {"/property/deleteimage/{id}"}, params = "action=deleteImage")
public String deleteImage(
        @PathVariable Long id) {
    propertyClientService.deleteImage(id);
    return "redirect:/admin/property/list";
}

html 与 thymeleaf

                        <form class="form form-horizontal" name="editProperty"
                          th:action="@{/admin/property/update/}+${property.id}" th:object="${property}"
                          method="post">
                           <div class="form-group">
                            <label class="col-sm-4 control-label">
                                Photo:
                            </label>
                            <div class="col-sm-8" id="photo" th:each="image : *{images}">
                                <img th:src="${image.url}"/>
                                <a th:href="|@{/admin/property/deleteimage/}+${id}|" class="btn btn-danger" value="deleteImage">Delete</a>
                                <!--<button type="submit" name="action" value="deleteImage">Delete Image</button>-->
                            </div>

                            <a href="javascript:void(0)" onclick="addMoreFiles('addPhoto')" class="btn btn-default">Add
                                Photo</a>

                            <div class="col-sm-10 addPhoto">
                                <input type="file" name="addPhoto" onchange="readURL(this);"/><br/>
                            </div>
                            </div>
                    </form>

错误

There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported

我是编程世界的新手。如果代码不整洁或存在愚蠢的错误或我提出问题的方式不好,请原谅。请用简单的话指导我,谢谢:)

最佳答案

您的 Controller 中正在等待删除请求,但您的链接并不满意:

<a th:href="|@{/admin/property/deleteimage/}+${id}|" class="btn btn-danger" value="deleteImage">Delete</a>它产生一个 GET 请求。

你可以做两件事:

  1. 您可以将映射更改为 GetMapping。
  2. 您可以使用 JS 函数发送表单,并且在发送时可以将方法更改为 DELETE。

关于javascript - Thymeleaf - 尝试为每个图像创建一个删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55811186/

相关文章:

html - sidenav 内容不对齐

javascript - 无法读取 null 的属性 'addEventListener'(JavaScript Sails 应用程序)

html - float 2 个 div 后剩余的中心

html - 将内联 block 脊柱内的文本与父容器高度垂直对齐(以百分比表示)

spring-boot - Thymeleaf 读取替换内的属性

spring - 混合 html 和 json 模板时如何在 thymeleaf 中正确设置内容类型

javascript - 如何使用 jquery 在 html 上显示 ajax 列表响应?

javascript - 使用 JSON 对象问题

javascript - 如何根据当前时间禁用时隙

javascript - 按 HTML SELECT 对象过滤 Json 功能