/** * Set the current user to a cookie, using the Servlet API * * @param request * @param response * @return */ @RequestMapping("/login") public String login(HttpServletRequest request, HttpServletResponse response) { System.out.println("----Login verification---"); String userName = request.getParameter("userName"); String password = request.getParameter("password"); Cookie cookie = new Cookie("user", userName + "-" + password); //cookie valid time 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's support for Json:
1. Add namespace
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
2. Add tags
<!-- Support object and json conversion. --> <mvc:annotation-driven/>
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