This describes the implementation of the update operation, the update operation in the Controller class first need to get the object in the foreground through a field, the object is placed in the Controller class in the Model object, For editing on the update.jsp foreground page, and then after the update.jsp page is modified, the user object in the foreground needs to be put in the controller class and then jumps to the list.jsp display page. The code implementation for the update operation in the Controller class is as follows:
@RequestMapping (value= "/{username}/update", method=requestmethod.get) Publicstring Update (@PathVariable string Username,model Model) {Model.addattribute (Users.get (username)); return"User/update";} @RequestMapping (Value= "/{username}/update", method=requestmethod.post) Publicstring Update (@PathVariable string username, @Validated Users user,bindingresult br) {if(Br.haserrors ()) {//jump directly to the add view if there is an error return"User/update"; } users.put (username, user); return"Redirect:/user/users";}View Code
The code that corresponds to the update.jsp page and the Add Operation add.jsp page code is as follows:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="SF"URI="Http://www.springframework.org/tags/form" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><!--no action is written at this time, direct submission will be submitted to/add -<Sf:formMethod= "POST"Modelattribute= "User">Username:<Sf:inputPath= "username"/><sf:errorsPath= "username"/><BR/>Password:<Sf:passwordPath= "Password"/><sf:errorsPath= "Password"/><BR/>Nickname:<Sf:inputPath= "nickname"/><BR/>Email:<Sf:inputPath= "Email"/><sf:errorsPath= "Email"/><BR/> <inputtype= "Submit"value= "Modify User"/></Sf:form></Body></HTML>
View Code
Before you edit the page, you need to add a link to the update.jsp page that corresponds to the link that you added to the update operation on the List.jsp page, as shown in the following code:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%><%@ taglib Prefix="C"URI="Http://java.sun.com/jsp/jstl/core"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>User List</title></Head><Body><ahref= "Add">Add to</a> <C:foreachItems= "${users}"var= "Um" > <ahref= "${um.value.username}">${um.value.username}</a>----${um.value.nickname}----${um.value.password}--<ahref= "${um.value.username}/update">Modify</a>----${um.value.email}<BR/> </C:foreach></Body></HTML>View Code
Finally, after editing is complete, jump to the list page.
Springmvc Getting Started-06