SpringMVC entry (four) a simple user management example - CRUD

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Here to write about how to handle a single user information through the REST format.

1 access to personal information
1.1 in the Controller accession:
	//{username}Is the path variable- path parameters
	@RequestMapping(value="/{username}",method=RequestMethod.GET)
	public String show(@PathVariable String username,Model model){ //@PathVariable refers to the use of path value as a parameter, there is{username}
		model.addAttribute(users.get(username));
		return "user/show";
	}


1.2 in the WEB-INF/jsp/user directory to create JSP file show.jsp
  <body>
	Username:${user.username}<br/>
	Password:${user.password}<br/>
	Nickname:${user.nickname}<br/>
	Email:${user.email}<br/>
  </body>


1.3 to modify the list.jsp file, adding link information
---<a href="user/${um.value.username}">${um.value.nickname}</a>


The 2 update personal information
2.1 added in Controller:
	@RequestMapping(value="/{username}/update",method=RequestMethod.GET)
	public String update(@PathVariable String username,Model model){
		model.addAttribute(users.get(username));
		return "user/update";
	}
	
	@RequestMapping(value="/{username}/update",method=RequestMethod.POST)
	public String update(@Validated User user,@PathVariable String username, BindingResult result){
		//Here if the database, according to the username to get the object, and then update the database. 
		users.put(username, user);
		return "redirect:/user/users";
	}


2.2 add update.jsp referenced above, path.
  <body>
	 	<sf:form method="post" modelAttribute="user">
   		User name: <sf:input path="username"/><sf:errors path="username"/><br/>
   		The user password: <sf:input path="password"/><sf:errors path="password"/><br/>
   		Nickname: <sf:input path="nickname"/> <br/>
   		The user's mailbox: <sf:input path="email"/><sf:errors path="email"/><br/>
   		 <input type="submit" value="Modify user"/>
   	</sf:form>
  </body>


2.3 in the list.jsp to add modify user link
---<a href="user/${um.value.username}/update">Modify</a>



3 delete user

3.1 added in Controller:
@RequestMapping(value="/{username}/delete",method=RequestMethod.GET)
	public String delete(@PathVariable String username){
		users.remove(username);
		return "redirect:/user/users";
	}


3.2 in the list.jsp to add delete link.
---<a href="user/${um.value.username}/delete">Delete</a>




This crud completed.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Beatrix at December 09, 2013 - 10:58 PM