Spring MVC的 - 如何调用有不同的对应注释相同的URL

问题描述 投票:0回答:2

我已成立了一个控制器有两个方法。 URL映射是完全一样的,唯一的区别是所述映射注释方法。一是@PostMapping和其他@DeleteMapping写的顺序。然而,当我试图调用@DeleteMapping法,@PostMapping方法被调用。

请注意,即使在这两个URL形式变量是不同的,它们有相同的价值观。他们只是从不同的HTML页面。

现在的问题是如何调用所需的方法各一次。

@PostMapping方法

@Secured({"ROLE_ADMIN", "ROLE_STUDENT"})
@PostMapping("students/{username}/internships/{id}")
public String addInternship(Model model, @PathVariable("username") String username, @PathVariable("id") int id) {

    /* DOES SOME STUFF
    Student student = userService.getUser(username).getStudent();
    Internship internship = userService.getInternship(id);

    StudentInternship studentInternship = new StudentInternship(internship, student, "Sent");

    internship.setNumberOfPositions(internship.getNumberOfPositions() - 1);
    userService.updateInternship(internship);

    student.setApplicationNumber(student.getApplicationNumber() + 1);
    userService.updateStudent(student);

    userService.addStudentInternship(studentInternship);
    */

    return "redirect:/internships";
}

@DeleteMapping方法

@Secured({"ROLE_ADMIN", "ROLE_STUDENT"})
@DeleteMapping("students/{username}/internships/{id}")
public String removeInternship(Model model, @PathVariable("username") String username, @PathVariable("id") int id) {

    /* DOES SOME STUFF
    Student student = userService.getUser(username).getStudent();
    Internship internship = userService.getInternship(id);

    int studentInternshipID = userService.getStudentInternshipByParams(student, internship).getId();

    internship.setNumberOfPositions(internship.getNumberOfPositions() + 1);
    userService.updateInternship(internship);

    student.setApplicationNumber(student.getApplicationNumber() - 1);
    userService.updateStudent(student);

    userService.removeStudentInternship(studentInternshipID);
    */

    return "redirect:/students/" + username + "/internships";
}

HTML

<form:form action="${pageContext.request.contextPath}/students/${username}/internships/${tempInternship.id}" method="POST">
    <input type="submit" value="Request" ${disabled}/>
</form:form>

<form:form action="${pageContext.request.contextPath}/students/${tempStudentInternship.student.username}/internships/${tempStudentInternship.internship.id}" method="DELETE">
    <input type="submit" value="Dismiss" />
</form:form>
java html hibernate spring-mvc java-ee
2个回答
2
投票

浏览器只支持GETPOST作为http请求方法。解决的办法是与POST方法发送表单,并注入称为_method与你期望的方法作为一种价值相同的HTML表单里面的隐藏字段,你的情况在这里,它只是DELETE。对于POST的情况下,只写你的形式如常。

例如:

<form:form action="${pageContext.request.contextPath}/students/${tempStudentInternship.student.username}/internships/${tempStudentInternship.internship.id}" method="POST">
    <input type="hidden" name="_method" value="DELETE"/>
    <input type="submit" value="Dismiss" />
</form:form>

请看看this answer用于创建的Spring bean,然后应用spring:form HTML表单里面所提到的形式属性。


0
投票

只有GET和POST是从FORM允许的。你需要使用AJAX来指定其他类型的请求。嗨,你需要使用JavaScript和XMLHttpRequest的,以指定请求的类型。

下面是一个例子,我从互联网上随意了:

// Delete a user
var url = "http://localhost:8080/api/v1/users";
var xhr = new XMLHttpRequest();
xhr.open("DELETE", url+'/12', true);
xhr.onload = function () {
    var users = JSON.parse(xhr.responseText);
    if (xhr.readyState == 4 && xhr.status == "200") {
        console.table(users);
    } else {
        console.error(users);
    }
}
xhr.send(null);
© www.soinside.com 2019 - 2024. All rights reserved.