如何修复@deletemapping的bootstrapmodal

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

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

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

它与下一个ui完美结合:

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

带引导模式部件:

<div th:fragment="deleteEntry">
    <div class="modal" tabindex="-1" role="dialog" id="deleteModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Confirm deletion</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>Are you sure you want to delete this record?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <a href="" class="btn btn-primary" id="delRef">Yes, delete</a>
                </div>
            </div>
        </div>
    </div>
</div>

和js:

$('document').ready(function() {

    $('.table .btn-danger').on('click',function(event) {
        event.preventDefault();
        var href = $(this).attr('href');
        $('#deleteModal #delRef').attr('href', href);
        $('#deleteModal').modal();

    });
});

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

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

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

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.GET, "/**").hasAnyRole(Role.ADMIN.name(), Role.USER.name())
        .antMatchers(HttpMethod.GET, "/groups/delete/*").hasRole(Role.ADMIN.name())
        .antMatchers(HttpMethod.GET, "/groups/delete/").hasRole(Role.ADMIN.name())
        .antMatchers(HttpMethod.GET, "/groups/delete").hasRole(Role.ADMIN.name())
                .antMatchers(HttpMethod.POST, "/**").hasRole(Role.ADMIN.name())
                .antMatchers(HttpMethod.PUT, "/**").hasRole(Role.ADMIN.name())
                .antMatchers(HttpMethod.DELETE, "/**").hasRole(Role.ADMIN.name())
                .anyRequest().authenticated().and()
                .httpBasic();
    }
tvmytwxo

tvmytwxo1#

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

$.ajax({
    type : "DELETE",
    url : "/groups/delete",
    data: {"id": id},
    contentType: "application/json",
    dataType : 'json',
    success: function (result) {       
        console.log(result);                
    },
    error: function (e) {
        console.log(e);
    }
});

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

ckx4rj1h

ckx4rj1h2#

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

相关问题