2017-02-03 8 views
0

私は新しいタスクを作成する必要があります。しかし、 "saveButton"をクリックすると、何も聞こえません。 URLはアクションフォーム(「@ {/ add}」)はコントローラのURLと同じですが、メソッドは呼び出しません。POSTリクエスト。 Spring、Thymeleaf

助けてください。ありがとう!

これは私のコントローラである:

@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ファイル:

<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> 

答えて

0

データがPOSTされることはありませんので、あなたは、保存ボタンをフォームには<input type="submit"ボタンまたは任意のAJAXリスナーがありません-ed

関連する問題