首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Spring和Html从存储库中删除元素

使用Spring和Html从存储库中删除元素
EN

Stack Overflow用户
提问于 2017-09-13 21:49:02
回答 2查看 64关注 0票数 0

我正在尝试从CrudRepository中删除一个主题,但是我尝试创建的复选框没有显示出来。我有一个Add方法,它工作得很好,但是remove方法就不行了。

用于导航和标题片段的hello.html

代码语言:javascript
复制
<!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的一部分

代码语言:javascript
复制
@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

代码语言:javascript
复制
<!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>

下面是正在发生的事情:

EN

回答 2

Stack Overflow用户

发布于 2017-09-13 21:51:29

您的问题在循环中,您正在使用相同的对象来迭代自身

代码语言:javascript
复制
th:each="topic: ${topic}"

您应该有类似以下内容的内容

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2017-09-14 20:04:39

多亏了https://stackoverflow.com/users/2757561/cralfaro,我们才能修复控制器和removeTopic.html之间的连接

问题是hello.html

代码语言:javascript
复制
<a th:href="/removeTopic">Remove</a>

这个应该是这样的:

代码语言:javascript
复制
<a th:href="@{/api/removeTopic}">Remove</a>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46199366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档