2016-08-02 227 views
1

我已經開發出一種學生管理Web應用程序添加,編輯和使用Spring MVC的,Hibernate和mysql數據庫平臺刪除學生。Spring MVC的HTTP狀態400

添加和編輯選項正常工作,但點擊刪除它時,顯示HTTP狀態400

在控制器中的刪除功能同樣喜歡編輯唯一的區別是在URL,它呼籲在Hibernate中刪除功能。

控制器

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> 

回答

0

的 '刪除' 鏈接發送一個參數,一個錯誤的名字( 'studentIdsssss' 而不是 'studentId')。這會產生錯誤,因爲@RequestParam默認爲'required = true'。

順便說一句,請注意它更習慣揭露「副作用」的行爲(例如編輯,刪除)爲POST。但是,除此之外,如果僅僅是爲了一個簡單的演示,它可能就沒關係。

+0

謝謝。有效。但是,如果我更改刪除到POST它顯示錯誤。我需要在任何地方做任何改變。 –