第三章 Spring MVC Servlet API与Json的支持

阅读更多
Spring MVC 对 Servlet API 的支持:


	/**
	 * 将当前用户设置到cookie,使用Servlet API
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@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有效时间
		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";
	}




Spring MVC 对Json的支持:

1、添加命名空间






2、增加标签



 




3、添加jackson jar

jackson-annotations-2.2.1.jar
jackson-core-2.2.1.jar
jackson-core-asl-1.8.8.jar
jackson-databind-2.2.1.jar
jackson-mapper-asl-1.8.8.jar
jackson-module-jaxb-annotations-2.2.1.jar



访问路径:
http://localhost:8080/SpringMvc0201/login.jsp
  • jackson.zip (1.7 MB)
  • 下载次数: 0
  • SpringMvc0201.zip (9.6 MB)
  • 下载次数: 0

你可能感兴趣的:(spring,mvc)