JavaSpringMVC-学生数据库。无法编辑学生文档
我已经为学生管理创建了一个java spring MVC web应用程序,我能够添加学生。但是当我点击edit student时,它显示HTTP 400——客户端发送的请求在语法上是不正确的 控制器:JavaSpringMVC-学生数据库。无法编辑学生文档,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我已经为学生管理创建了一个java spring MVC web应用程序,我能够添加学生。但是当我点击edit student时,它显示HTTP 400——客户端发送的请求在语法上是不正确的 控制器: package com.akhil.controller; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework
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";
}
}
package com.akhil.controller;
导入java.util.Map;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.validation.BindingResult;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.servlet.mvc.annotation.model和viewsolver;
导入com.akhil.model.Student;
导入com.akhil.service.StudentService;
@控制器
公共班级学生控制员{
@自动连线
私人学生服务学生服务;
@请求映射(“/”)
公共字符串设置窗体(ModelMap模型){
学生=新生();
model.addAttribute(“学生”,学生);
model.addAttribute(“studentList”,studentService.getAllStudent());
返回“学生”;
}
@RequestMapping(value=“/student.do”,method=RequestMethod.POST)
公共字符串doActions(@ModelAttribute Student Student,BindingResult,@RequestParam字符串操作,映射){
Student studentResult=新学生();
学生服务。添加(学生);
地图放置(“学生”,studentResult);
put(“studentList”,studentService.getAllStudent());
返回“学生”;
}
@RequestMapping(value=“/editstudent”,method=RequestMethod.GET)
公共字符串editstudent(@ModelAttribute Student Student,BindingResult,@RequestParam字符串操作,映射){
Student studentResult=新学生();
编辑(student.getStudentId());
地图放置(“学生”,studentResult);
put(“studentList”,studentService.getAllStudent());
返回“学生”;
}
}
视图:
学生管理
学生数据
学生证:
名字
姓
年度水平
身份证件
名字
姓
年度水平
${student.studentId}
${student.firstname}
${student.lastname}
${student.yearLevel}
|
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>
积垢
log4jConfigLocation
类路径:log4j.xml
org.springframework.web.util.Log4jConfigListener
春天
org.springframework.web.servlet.DispatcherServlet
1.
春天
/
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
现在您可以在student.jsp
然后创建一个表单来编辑这些细节。java方法editstudent()接受映射到editstudent.html的get请求?studentId=${student.studentId},我认为studentId不能转换为ModelAttribute student。您没有发送学生的其他字段,如名字或姓氏。此外,编辑应该是post或put请求,而不是get。不要传递需要编辑的对象。传递对象的id,然后使用id从数据库中获取对象。最后将该对象传递到编辑视图这是我的函数@RequestMapping(value=“/editstudent”,method=RequestMethod.get)公共字符串editstudent(@RequestParam(“studentId”)int studentId,Model Model){Student Student=studentService.getStudent(studentId);Model.addAttribute(Student);return“student1”}是student1是您的页面名。返回您需要查看的页面名。并更改此部分。'Model.addAttribute(“Student”,Student);'
<?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)
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";
}