如何修复@deletemapping的bootstrapmodal

l7wslrjt  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(308)

我在控制器中有下一个方法:

  1. @GetMapping("/delete")
  2. public String deleteUser(int id) {
  3. groupService.deleteById(id);
  4. return REDIRECT_PAGE;
  5. }

它与下一个ui完美结合:

  1. <a th:href="@{/groups/delete/(id=${group.id})}" class="btn btn-danger">Delete</a>

带引导模式部件:

  1. <div th:fragment="deleteEntry">
  2. <div class="modal" tabindex="-1" role="dialog" id="deleteModal">
  3. <div class="modal-dialog" role="document">
  4. <div class="modal-content">
  5. <div class="modal-header">
  6. <h5 class="modal-title">Confirm deletion</h5>
  7. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  8. <span aria-hidden="true">&times;</span>
  9. </button>
  10. </div>
  11. <div class="modal-body">
  12. <p>Are you sure you want to delete this record?</p>
  13. </div>
  14. <div class="modal-footer">
  15. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  16. <a href="" class="btn btn-primary" id="delRef">Yes, delete</a>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </div>

和js:

  1. $('document').ready(function() {
  2. $('.table .btn-danger').on('click',function(event) {
  3. event.preventDefault();
  4. var href = $(this).attr('href');
  5. $('#deleteModal #delRef').attr('href', href);
  6. $('#deleteModal').modal();
  7. });
  8. });

但是现在我想用controller中的@deletemapping更改@getmapping(开始学习Spring Security ),以及我所拥有的-

  1. Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

我需要修什么?提前谢谢。
upd:好吧,如果不可能的话,如何添加到springsecurity配置规则,只允许“admin”删除?我尝试下一个,但不起作用-“用户”可以删除条目:

  1. @Override
  2. protected void configure(HttpSecurity http) throws Exception {
  3. http.csrf().disable()
  4. .authorizeRequests()
  5. .antMatchers(HttpMethod.GET, "/**").hasAnyRole(Role.ADMIN.name(), Role.USER.name())
  6. .antMatchers(HttpMethod.GET, "/groups/delete/*").hasRole(Role.ADMIN.name())
  7. .antMatchers(HttpMethod.GET, "/groups/delete/").hasRole(Role.ADMIN.name())
  8. .antMatchers(HttpMethod.GET, "/groups/delete").hasRole(Role.ADMIN.name())
  9. .antMatchers(HttpMethod.POST, "/**").hasRole(Role.ADMIN.name())
  10. .antMatchers(HttpMethod.PUT, "/**").hasRole(Role.ADMIN.name())
  11. .antMatchers(HttpMethod.DELETE, "/**").hasRole(Role.ADMIN.name())
  12. .anyRequest().authenticated().and()
  13. .httpBasic();
  14. }
tvmytwxo

tvmytwxo1#

如果你想使用 @DeleteMapping 注解,我认为您需要使用ajax发出请求,否则它应该保留 @GetMapping .
如果要使用@requeustparam:

  1. $.ajax({
  2. type : "DELETE",
  3. url : "/groups/delete",
  4. data: {"id": id},
  5. contentType: "application/json",
  6. dataType : 'json',
  7. success: function (result) {
  8. console.log(result);
  9. },
  10. error: function (e) {
  11. console.log(e);
  12. }
  13. });

浏览器只支持get和post as http请求方法。
如果你想解决这个问题,请看这里 @PostMapping .

展开查看全部
ckx4rj1h

ckx4rj1h2#

看起来您仍在发送get请求而不是delete,但是没有更多的get端点,这导致了错误。

相关问题