【spring mvc】spring mvc对servlet api的支持

spring mvc对servlet api的支持,如下:

可以在参数里面直接写request、response、session  等信息,和自动将实体转换为json(不灵活、不建议使用),用起来超级方便了。

@Controller
@RequestMapping("/user")
public class UserController {

	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response){
		System.out.println("----登录验证---");
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		Cookie cookie=new Cookie("user",userName+"-"+password);
		cookie.setMaxAge(1*60*60*24*7);
		User currentUser=new User(userName,password);
		response.addCookie(cookie);
		HttpSession session=request.getSession();
		session.setAttribute("currentUser", currentUser);
		return "redirect:/main.jsp";
	}
	
	@RequestMapping("/login2")
	public String login2(HttpServletRequest request){
		return "redirect:/main.jsp";
	}
	
	@RequestMapping("/login3")
	public String login3(HttpSession session){
		return "redirect:/main.jsp";
	}
	
	@RequestMapping("/ajax")
	public @ResponseBody User ajax(){
		User user=new User("zhangsan","123");
		return user;
	}
}

你可能感兴趣的:(javaEE框架和组件等)