gpt4 book ai didi

Java spring MVC - 学生数据库。无法编辑学生文档

转载 作者:行者123 更新时间:2023-11-30 07:15:05 25 4
gpt4 key购买 nike

我创建了一个用于学生管理的 java spring MVC Web 应用程序,我可以添加学生 .但是当我点击“编辑学生”时,它显示 HTTP 400 - 客户端发送的请求在语法上不正确。

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.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;
importorg.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(@ModelAttribute Student student, BindingResult result, @RequestParam String action, Map<String, Object> map){

Student studentResult = new Student();
studentService.edit(student.getStudentId());
map.put("student",studentResult);
map.put("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" />
</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>

Web.xml

  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>CRUDWebAppMavenized</display-name>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

最佳答案

@RequestMapping(value="/editstudent", method=RequestMethod.GET)
public String editstudent(@ModelAttribute Student student,BindingResult result, @RequestParam String action, Map<String, Object> map)

这种方法是错误的,您无法从 GET 请求中获取 studentObject,因为您正在尝试编辑对象,所以我建议使用 POST 请求删除方法参数内的 @modelAttribute 注释该注释从表单内的字段形成一个对象。如果 BindingResult 无法从传入请求形成对象,则会收集 errors。由于您没有提交表单,因此您不需要其中任何一个。

但是您可以使用从 url 获取的 id 从数据库中获取学生对象

public String editstudent(@RequestParam("id") int id, Model model){
student studentobj = studentservice.get(id);//assuming that this loads the object from database;
model.addAttribute("student",studentobj);
return "student";
}

现在您可以在 student.jsp 中表示学生详细信息然后有一个表单来编辑这些详细信息。

关于Java spring MVC - 学生数据库。无法编辑学生文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38549563/

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