问题描述
在jsp页面中,我正在使用输入字段
<input type="text" name="basic" id="basic" >
在控制器中
@RequestMapping(value="/palySlip/add",method=RequestMethod.POST)
public String addData(@ModelAttribute("empPaySlip") EmployeePlaySlip e){
return "redirect:/employeepayin";
}
并将值设置为模型类
public class EmployeePlaySlip {
private double basic;
public double getBasic() {
return basic;
}
public void setBasic(double basic) {
this.basic = basic;
}
}
但是在发送数据时出现错误
HTTP状态400-类型状态报告消息描述客户端发送的请求在语法上不正确。
1楼
首先,您需要将一个对象设置为模型属性。
@RequestMapping(value="/employeepaying",method=RequestMethod.POST)
public ModelAndView showData(){
ModelAndView mav = new ModelAndView("employeepayingPage");
mav.addObject(empPaySlip, new EmployeePlaySlip ());
return mav;
}
将Spring taglib添加到您的JSP
:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
创建提交表单:
<form:form method="post" modelAttribute="empPaySlip" action="/palySlip/add">
<form:input path="basic" />
<input type="submit" value="Submit" />
</form:form>
然后获取您的模型属性。
@RequestMapping(value="/palySlip/add", method=RequestMethod.POST)
public String addData(@ModelAttribute("empPaySlip") EmployeePlaySlip e){
// Do whatever you want with your object
return "redirect:/employeepaying";
}
2楼
在您的@Controller中,首先通过Get方法将jsp页面表单与Model Attribute链接起来
@RequestMapping(value= "/studenteducation", method= RequestMethod.GET)
public String setupUpdateEducationForm(Model model,@ModelAttribute("education") Student student){
student = studentService.getStudent(getStudentName());
model.addAttribute("education", student); //adding saved student data into model
return "studenteducation";
}
还有一个Post方法将提交您的jsp表单
@RequestMapping(value= "/studenteducation", method= RequestMethod.POST)
public String studentEducationForm(Model model, @ModelAttribute("education") Student student, BindingResult result){
if(result.hasErrors()){
return "studenteducation";
}
student.setStudentId(getStudentName()); // inserting primary key
studentService.updateStudent(student); // updating student table
return "redirect:/";
}
注意@ModelAttribute名称,它应该与jsp modelAttribute名称相同。
现在,在jsp页面中,使用modelAttribute =“ education” action =“ / studenteducation”和带有您的列名的表单输入即可解决问题。
<form:form method="post" modelAttribute="education" action="/studenteducation">
<form:input path="your column name" />
....
<input type="submit" value="Submit" />