gpt4 book ai didi

java - Spring MVC HTTP 状态 400

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:17 25 4
gpt4 key购买 nike

我开发了一个学生管理 Web 应用程序,使用 Spring MVC、Hibernate 和 MYSQL 数据库来添加、编辑和删除学生。

添加和编辑选项工作正常,但单击删除时显示 HTTP 状态 400。

Controller 中的删除功能与编辑相同,唯一的区别在于url,它在hibernate中调用删除功能。

Controller

package com.akhil.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;

import com.akhil.model.Student;
import com.akhil.service.StudentService;

@Controller
public class StudentController {
@Autowired
private StudentService studentService;

@RequestMapping("/")
public String setupForm(ModelMap model){
Student student = new Student();
model.addAttribute("student", student);
model.addAttribute("studentList", studentService.getAllStudent());
return "student";
}
@RequestMapping(value="/student.do", method=RequestMethod.POST)
public String doActions(@ModelAttribute Student student, BindingResult result, @RequestParam String action, Map<String, Object> map){
Student studentResult = new Student();
studentService.add(student);
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
@RequestMapping(value="/editstudent", method=RequestMethod.GET)
public String editstudent(@RequestParam("studentId") int studentID, Model model) {

Student student = studentService.getStudent(studentID);
model.addAttribute(student);
return "student1";
}
@RequestMapping(value="/updatestudent.do", method=RequestMethod.POST)
public String updatestudent(@ModelAttribute Student student, BindingResult result, @RequestParam String action, Map<String, Object> map){
Student studentResult = new Student();
studentService.edit(student);
//map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
@RequestMapping(value="/deletestudent", method=RequestMethod.GET)
public String deletestudent(@RequestParam("studentId") int studentID, Model model) {
Student studentResult = new Student();
studentService.delete(studentID);
model.addAttribute("student", studentResult);
model.addAttribute("studentList", studentService.getAllStudent());
return "student";
}

}

查看

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/jsp/includes.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Management</title>
</head>
<body>
<h1>Students Data</h1>
<form:form action="student.do" method="POST" commandName="student">
<table>
<tr>
<td><form:label path="studentId">Student ID:</form:label></td>
<td><form:input path="studentId" value="${Student.studentID}" /> </td>
</tr>
<tr>
<td>First name</td>
<td><form:input path="firstname" value="${Student.firstname}"/></td>
</tr>
<tr>
<td>Last name</td>
<td><form:input path="lastname" value="${Student.lastname}" /></td>
</tr>
<tr>
<td>Year Level</td>
<td><form:input path="yearLevel" value="${Student.yearLevel}" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="action" value="Add" />
<input type="submit" name="action" value="Edit" />
<input type="submit" name="action" value="Delete" />
<input type="submit" name="action" value="Search" />
</td>
</tr>
</table>
</form:form>
<br>
<table border="1">
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Year level</th>
</tr>
<c:forEach items="${studentList}" var="student">
<tr>
<td>${student.studentId}</td>
<td>${student.firstname}</td>
<td>${student.lastname}</td>
<td>${student.yearLevel}</td>
<td align="center"><a href="editstudent.html? studentId=${student.studentId}">Edit</a> | <a href="deletestudent.html?studentIds=${student.studentId}">Delete</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>

最佳答案

“删除”链接发送的参数名称错误(“studentIdsssss”而不是“studentId”)。这会产生错误,因为默认情况下 @RequestParam 为“required=true”。

顺便说一句,请注意,更习惯的做法是将“副作用”操作(例如编辑、删除)公开为 POST。但这不是重点,如果只是为了一个简单的演示,可能还可以。

关于java - Spring MVC HTTP 状态 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38727129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com