当前位置: 首页 > 工具软件 > PHP_MVC_REST > 使用案例 >

SpringMvc(3)--REST

欧阳俊逸
2023-12-01

1.REST简介

关于rest可以看 http://blog.csdn.net/wjh156304366/article/category/1145134

2.模拟REST风格的增删查改

UserController.java
package org.sh.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.sh.vo.User;
import org.sh.vo.UserException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Controller
@RequestMapping("/user")
@SessionAttributes("loginUser")
public class UserController {
	private static Map<String, User> users = new HashMap<String, User>();

	public UserController() {
		users.put("suhao1", new User("suhao1", "苏浩", "123", "123"));
		users.put("suhao2", new User("suhao2", "苏浩", "123", "123"));
		users.put("suhao3", new User("suhao3", "苏浩", "123", "123"));
		users.put("suhao4", new User("suhao4", "苏浩", "123", "123"));
	}

	@RequestMapping("/users")
	public String list(Model model) {
		model.addAttribute("users", users);
		return "user/list";
	}

	// 当以get提交的时候就调用此方法
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("user",new User());//防止添加的时候页面报错
		return "user/add";
	}

	// 当以post提价的时候就调用此方法,参数中的user和form中的ModelAttribute的属性要保持一样
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@Valid User user,BindingResult binding) {
		if(binding.hasErrors()){
			System.out.println("=========BindingResult============");
			return "user/add";
		}
		users.put(user.getUsername(), user);
		//加上前缀 使用客户端跳转 防止重复提交
		return InternalResourceViewResolver.REDIRECT_URL_PREFIX+"/user/users";
	}
	@RequestMapping(value="/{username}",method=RequestMethod.GET)
	public String show(@PathVariable String username,Model model){
		System.out.println("***************"+username);
		model.addAttribute("user",users.get(username));
		return "user/show";
	}
	
	@RequestMapping(value="/{username}/update",method=RequestMethod.GET)
	public String update(@PathVariable String username,Model model){
		model.addAttribute("user",users.get(username));
		return "user/update";
	}
	
	@RequestMapping(value="/{username}/update",method=RequestMethod.POST)
	public String update(@PathVariable String username,@Valid User user,BindingResult br,Model model){
		if(br.hasErrors()){
			return "user/update";
		}
		users.put(username, user);
		return "redirect:/user/users";
	}
	
	@RequestMapping(value="/{username}/delete",method=RequestMethod.GET)//tomcat不能发delete请求
	public String delete(@PathVariable String username){
		users.remove(username);
		return "redirect:/user/users";
	}
	
	@RequestMapping(value = "/login",method=RequestMethod.GET)
	public String login(){
		return "user/login";
	}
	
	@RequestMapping(value = "/login",method=RequestMethod.POST)
	public String login(String username,String password,Model model){
		if(!users.containsKey(username)){
			throw new UserException("用户名不存在");
		}
		if(!password.equals(users.get(username).getPassword())){
			throw new UserException("密码不正确");
		}
		model.addAttribute("loginUser",users.get(username));//loginUser 和sessionAttribute的名字一样
		return "redirect:/user/users";
	}
	@ExceptionHandler(value={UserException.class})
	public String handleException(Exception ex,HttpServletRequest req){
		req.setAttribute("ex", ex);
		return "error";
	}
	
	@RequestMapping("/redir")
	public String redir(RedirectAttributes ra) {
		ra.addFlashAttribute("tttt", "tttt");
		return "redirect:/user/users";
	}
	
}


在WEB-INF/jsp/user 创建出所需要的jsp页面
1.list.jsp
<body>
  <h1>${tttt }</h1>
  <a href="add"> add</a>-->${loginUser.nickname }<br/>
  	<c:forEach items="${users}" var="user">
  		<a href="${user.value.username }">${user.value.username }</a>----${user.value.nickname }---${user.value.password }---${user.value.email }
  		<a href="${user.value.username }/update">更新</a>
  		<a href="${user.value.username }/delete">删除</a><br/>
  	</c:forEach>
  </body>


2.add.jsp
<body>
  <sf:form method="post" modelAttribute="user">
	UserName:<sf:input path="username"/><sf:errors path="username"/><br/>
	Password:<sf:password path="password"/><sf:errors path="password"/><br/>
	Nickname:<sf:input path="nickname"/><sf:errors path="nickname"/><br/>
	Email: <sf:input path="email"/><sf:errors path="email"/><br/>
<input type="submit"/>
</sf:form>
  </body>
3.show.jsp
 <body>
 ${user.username }----${user.password }----${user.nickname }----${user.email }
  </body>




 类似资料: