我正在尝试从CrudRepository中删除一个主题,但是我尝试创建的复选框没有显示出来。我有一个Add方法,它工作得很好,但是remove方法就不行了。
用于导航和标题片段的hello.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org/"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head th:fragment="head">
<title> Lunch Planner Beta </title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>
</head>
<body>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<nav th:fragment="navigation">
<a href="/api/topics">List</a> |
<a href="/addTopic">Add</a> |
<a th:href="/removeTopic">Remove</a>
</nav>
<form th:action="@{/home}" method="get">
<input type="submit" value="Home"/>
</form>
<br/>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign OUT"/>
</form>
</body>
</html>
TopicController.java的一部分
@RequestMapping(method = RequestMethod.POST, value = "/remove", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
@Transactional
public ModelAndView deleteTopic(Long id, HttpServletResponse response, Errors errors){
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
model.addObject("topic",topicService.getTopic(id));
topicService.deleteTopic(id);
return new ModelAndView("home");
}
HTML:removeTopic.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="hello :: head">
</head>
<body class="container">
<nav th:replace="hello :: navigation"></nav>
<form th:action="@{/api/remove}" th:object="${topic}" method="post">
<div th:each="topic: ${topic}" class="checkbox">
<input type="radio" name="id" th:value="${topics.id}" th:id="${topics.id}"/>
<label th:for="${topics.id}" th:text="${topics.category}"></label>
<br/>
</div>
<input type="submit" value="Remove Topic"/>
</form>
</body>
</html>
下面是正在发生的事情:
发布于 2017-09-13 21:51:29
您的问题在循环中,您正在使用相同的对象来迭代自身
th:each="topic: ${topic}"
您应该有类似以下内容的内容
<div th:each="item: ${topics}" class="checkbox">
<input type="radio" name="id" th:value="${item.id}" th:id="${item.id}"/>
<label th:for="${item.id}" th:text="${item.category}"></label>
<br/> <!-- Do not use this tag to add vertical space, use css classes-->
</div>
发布于 2017-09-14 20:04:39
多亏了https://stackoverflow.com/users/2757561/cralfaro,我们才能修复控制器和removeTopic.html之间的连接
问题是hello.html在
<a th:href="/removeTopic">Remove</a>
这个应该是这样的:
<a th:href="@{/api/removeTopic}">Remove</a>
https://stackoverflow.com/questions/46199366
复制相似问题