当前位置: 首页 > 知识库问答 >
问题:

过帐请求。Spring,沉香树

刘阳荣
2023-03-14

我需要创建一个新任务。但当我点击“保存按钮”时,什么都没有。操作窗体(“@{/add}”)中的URL等于控制器中的URL,但方法不调用。

请帮帮忙。谢谢!

@Controller
@RequestMapping(value = "/")
public class MainPageController {

@Autowired
TaskDao taskDao;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getMainPage(Model model){

    model.addAttribute("taskList", taskDao.findAll());
    model.addAttribute("task", new Task());

    return "mainPage";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute Task task){

    System.out.println("OK"); // Just check entry 

    taskDao.save(task);

    return "mainPage";
}
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
<th:block th:replace="template/head :: meta"/>
<th:block th:replace="template/head :: css"/>
<th:block th:replace="template/head :: javascript"/>

<script type="text/javascript">
    $(document).ready(function(){
        $("#popup").click(function(){
            $("#myModal").modal('show');
        });
    });
</script>

</head>
<body>

<div class=" col-md-4">
    <h1>Tasks List</h1>
    <table
        xmlns:th="http://www.thymeleaf.org"
        class="table table-bordered table-hover table-striped">
        <thead>
            <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Description</th>
              <th>Delete</th>
            </tr>
        </thead>

        <tbody>
            <tr th:data="${task.id}" class="cursor-pointer" th:each="task : ${taskList}">
              <td th:text="${task.id}"></td>
              <td th:text="${task.name}"></td>
              <td th:text="${task.description}"></td>
              <td class="col-md-2 text-center">
                <a th:href="@{'/list/remove/' + ${task.id}}" class="btn btn-primary" th:text="#{deleteButton}"></a> </td>
            </tr>
        </tbody>
    </table>

    <a th:href="@{/}" class="btn btn-default pull-left" th:text="#{showButton}"></a>
    <a th:href="@{/#}" id="popup" class="btn btn-primary pull-right" th:text="#{addButton}"></a>
</div>

<!-- Modal HTML -->
<div id="myModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Enter task information</h4>
            </div>
            <div class="modal-body">
                <form
                    method="post"
                    th:object="${task}"
                    th:action="@{/add}">

                    <div class="form-group">
                        <label for="input1">Name</label>
                        <input type="text"
                           class="form-control"
                           placeholder="Name"
                           th:field="*{name}"
                           id="input1"/>
                    </div>

                    <div class="form-group">
                        <label for="input2">Description</label>
                        <input type="text"
                           class="form-control"
                           placeholder="Description"
                           th:field="*{description}"
                           id="input2"/>
                    </div>

                    <button type="button" class="btn btn-default" data-dismiss="modal" th:text="#{closeButton}"></button>
                    <button type="button" class="btn btn-primary pull-right" th:text="#{saveButton}"></button>
                </form>
            </div>
        </div>
    </div>
</div>

</body>
</html>

共有1个答案

东门奕
2023-03-14

您的表单中没有按钮,也没有保存按钮上的任何AJAX侦听器,因此数据永远不会post ed

 类似资料:
  • 我使用Thymeleaf模板引擎与spring和我想显示文本存储通过一个多行textarea。 在我的数据库中,多行字符串是用“\n”存储的,如下所示:“test1\ntest2\n....” 对于th:text,我得到了:“test1 test2”,没有换行符。

  • 我是thymeleaf的新手,在执行if条件时遇到了问题。我编写的代码是 问题是这段代码写完后,web页面会不断加载。谁能告诉我执行这个操作的正确方法。

  • 我使用spring boot 1.5.6版本和thymeleaf 3.0.0。 我有一个控制器,很简单: start应用程序如下: 当我运行该应用程序时,我将http://localhost:8080放在chrome地址栏中。我得到的是你好世界。所以我想spring boot&thymeleaf模板解析器可以找到index.html文件。但是根本不解析和。我通过chrome开发工具检查了页面的源代

  • 在内部,如果出现任何异常,将被重定向&这不需要通过,可以跳过此请求。 我已经实现了OncePerRequestFilter&任何请求都不会调用它。这必须在OAuth过滤器之前调用。 让我知道这个安全配置出了什么问题。

  • 我正在使用Spring Integration Filter对传入消息强制执行一些验证逻辑。验证的结果需要发回消息网关调用方,以便通知请求的具体错误。 null 配置 网关 过滤器

  • 在Controller/JAX-RSendpoint处理请求后,如何执行Spring Security过滤器? 当请求被双向处理时,一个普通的java过滤器链应该得到控制。如何使用Spring Security过滤器实现这一点?我试着给chain打电话。doFilter()开始,并在调用后添加我的逻辑。但是,在处理请求后,控件不会返回。 关于如何实现这一点,有什么建议吗?是否有可能使用Spring