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

时间:2016-07-24 07:03:09

标签: java spring hibernate spring-mvc

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

控制器:

  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>

1 个答案:

答案 0 :(得分:1)

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

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

但您可以使用id

中的url从数据库中获取学生对象
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内代表学生详细信息 然后有一个表格来编辑这些细节。