I am simply creating a CRUD Application in Spring MVC. I want to edit Student Details. I have created one form which is used to add student. How can I use the same form to populate the student details for editing it ?
Controller
@RequestMapping(value="/add", method = RequestMethod.GET)
public String addStudent(@RequestParam("studentName") String name,@RequestParam("studentId") String studId){
System.out.println("Student Id : "+ studId);
System.out.println("Student "+name+" added");
list.add(name);
return "redirect:get";
}
@RequestMapping(value="/edit/${index}", method = RequestMethod.GET)
public String editStudent(@PathVariable("index") int index, Model model){
System.out.println("Edit Student with Index " + index);
model.addAttribute("studentId",index);
model.addAttribute("studentName",list.get(index));
return "student";
}
Form
<c:url value="/students/add" var="addStudentAction"></c:url>
<form action="${addStudentAction}" method="get">
<input type="hidden" name="studentId">
<input type="text" name="studentName"></input>
<input type="submit" name="submit" value="Add Student" />
</form>
I want to set studentId and studentName in form fields which are set in model in editStudent method.