javascript - 为什么我的 Controller 中的保存功能不起作用?

标签 javascript java spring spring-boot thymeleaf

我正在使用 springboot 和 thymeleaf 进行简单的处理,但是当我尝试使用 save 函数进行更新或创建时,我丢失了对象的数据。我单击“新建”和“更新”按钮,使用对象充电的数据正确打开模式,但是当我单击“保存”时,我丢失了对象。

我相信我的问题出在形式上,但我不知道它是什么。我已尝试使用 postman ,但当我单击“保存”或“更新”时没有收到请求

索引:

    <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script th:src="@{js/main.js}" src="../static/js/main.js"></script>
  </body>
</head>
<body>

    <div class="container">
        <div class="jumbotron">
            <h1>Paginación con SpringBoot</h1>
        </div>  
        <button class="btn btn-success nBtn">New</button>
        <div class="card">
            <div class="card-block">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>COUNTRY</th>
                            <th>CAPITAL</th>
                            <th>ACTION</th>
                        </tr>   
                    </thead>
                    <tbody>
                        <tr th:each="country :${data.content}">
                            <td th:text="${country.id}"></td>
                            <td th:text="${country.name}"></td>
                            <td th:text="${country.capital}"></td>
                            <td>
                                <a th:href="@{/delete/(id=${country.id})}" class="btn btn-danger dBtn">Delete</a>
                                <a th:href="@{/findOne/(id=${country.id})}" class="btn btn-primary eBtn">Edit</a>
                            </td>
                        </tr>   
                    </tbody>
                </table>
                <hr>
            </div>
            <div>
                <ul class="nav nav-pills  nav-justified">
                    <li class="nav-item" th:each="i :${#numbers.sequence(0,data.totalPages-1)}">
                        <a th:href="@{/(page=${i})}" th:text="${i}" class="nav-link" th:classappend="${currentPage}==${i}?'active':''"></a>
                    </li>   
                </ul>
            </div>
        </div>


        <div class="myForm">
     <form th:action="@{/save}" th:object="${country}" method="post">
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Update or Create</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                  <div class="form-group">
                    <label for="id" class="col-form-label">Id:</label>
                    <input type="text" class="form-control" id="id" name="id" th:value="*{''}" disabled/>
                  </div>
                  <div class="form-group">
                    <label for="name" class="col-form-label">Name:</label>
                    <input type="text" class="form-control" id="name" name="name" th:value="*{''}"/>
                  </div>
                  <div class="form-group">
                    <label for="capital" class="col-form-label">Capital:</label>
                    <input type="text" class="form-control" id="capital" name="capital" th:value="*{''}"/>
                  </div>                      
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <input type="submit" class="btn btn-primary" data-dismiss="modal" value="Save"/>
              </div>
            </div>
          </div>
        </div>
        </form>
    </div>
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
              <p class="alert alert-danger">Are you sure you want to delete this?</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <a href="" class="btn btn-danger" id="delRef">Delete</a>
              </div>
            </div>
          </div>
        </div>
    </div>


</body>
</html>

Controller :

        @Controller
public class CountryController {

    @Autowired
    private CountryRepository countryRepository;

    @SuppressWarnings("deprecation")
    @GetMapping("/")
    public String showPage(Model model, @RequestParam(defaultValue="0") int page) {
        model.addAttribute("data", countryRepository.findAll(new PageRequest(page,4)));
        model.addAttribute("currentPage", page);
        return "index";
    }

    @PostMapping("/save")
    public String save(Country country) {
        countryRepository.save(country);
        return "redirect:/";
    }

    @GetMapping("/delete")
    public String deleteCountry(Integer id) {
        countryRepository.deleteById(id);;
        return "redirect:/";
    }

    @GetMapping("/findOne")
    @ResponseBody
    public Optional<Country> FindOne(Integer id) {
        return  countryRepository.findById(id);


    }
}

js:

    $(document).ready(function(){
$('.nBtn, .table .eBtn').on('click', function(event){
    event.preventDefault();
    var href = $(this).attr('href');
    var text = $(this).text();
    if(text=='Edit'){
        $.get(href,function(country,status){
            $('.myForm #id').val(country.id);
            $('.myForm #name').val(country.name);
            $('.myForm #capital').val(country.capital);
        });
        $('.myForm #exampleModal').modal();
    }else{
        $('.myForm #id').val('');
        $('.myForm #name').val('');
        $('.myForm #capital').val('');
        $('.myForm #exampleModal').modal();
    }
});
$('.table .dBtn').on('click', function(event){
    event.preventDefault();
    var href = $(this).attr('href');
    $('#myModal #delRef').attr('href',href);
    $('#myModal').modal();
});});

类别:

@Entity
public class Country {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String capital;


    public Country(String name, String capital) {
        this.name = name;
        this.capital = capital;
    }

    public Country() {
    }


    @Override
    public String toString() {
        return "Country [id=" + id + ", name=" + name + ", capital=" + capital + "]";
    }


    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCapital() {
        return capital;
    }
    public void setCapital(String capital) {
        this.capital = capital;
    }
}

存储库:

public interface CountryRepository extends JpaRepository<Country,Integer>{

}

最佳答案

保存方法上可能缺少@RequestBody注释

@PostMapping("/save")
public String save(@RequestBody Country country) {
    countryRepository.save(country);
    return "redirect:/";
}}

关于javascript - 为什么我的 Controller 中的保存功能不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948369/

相关文章:

javascript - 如何在不知道其 ID 的情况下从 JavaScript 中选择 HTML 元素?

javascript - 粘性导航栏无法在视差滚动中使用透视 css 属性

java - 没有名为 'entityManagerFactory' 的可用 bean,但已设置entityManagerFactoryRef

java - 在 Java 中实习字符串

java - 我的对象应该保存对象还是对象 ID?

java - Mule:不同组件的接口(interface)绑定(bind),如何避免重复?

java - 在 jsp 中显示 JasperReport 生成的 html 报告的最佳方式是什么?

脚本之前的 javafx WebView setMember()

javascript - 如何打乱对象数组

java - 在 JAVAFX 中将新添加的子级调整为父级的大小