2.配置文件:
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
3.配置web.xml
4.编程测试:
package boce.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
/**
* Created by gjp on 2017/7/20.
*/
@Controller
@RequestMapping("/session/")
public class SessionController {
Logger logger = LoggerFactory.getLogger(SessionController.class);
@RequestMapping(value = "setinfo")
public ModelAndView sessionInfo(){
ModelAndView mav = new ModelAndView("thymeleaf/setsession");
ServletRequestAttributes servletRequestAttributes =(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = servletRequestAttributes.getRequest().getSession();
String value ="boce.com";
String time1 = Calendar.getInstance().getTimeInMillis()+"";
session.setAttribute("userId",value+time1);
session.setAttribute("time1",time1);
logger.info("session value="+value+time1);
return mav;
}
@RequestMapping(value = "getinfo")
public ModelAndView getInfo(){
ModelAndView mav = new ModelAndView("thymeleaf/getsession");
ServletRequestAttributes servletRequestAttributes =(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = servletRequestAttributes.getRequest().getSession();
String userId = (String) session.getAttribute("userId");
String time = (String)session.getAttribute("time");
mav.addObject("userId",userId);
String sessionId = session.getId();
long createTime = session.getCreationTime();
long lastAccessedTime = session.getLastAccessedTime();
int maxInactive = session.getMaxInactiveInterval();
StringBuffer stringBuffer = new StringBuffer(64);
stringBuffer.append("sessionId:").append(sessionId);
stringBuffer.append(";createTime:").append(createTime);
stringBuffer.append(";lastAccessdTime:").append(lastAccessedTime);
stringBuffer.append("maxInactive:").append(maxInactive);
logger.info(stringBuffer.toString());
mav.addObject("time1",time);
mav.addObject("good","good hello world!");
logger.info("session value="+userId+";time1:"+time);
return mav;
}
}
测试结果:
.......
2017-07-21 11:23:22 INFO 11 [boce.common.mvcinterceptor.MVCRequestInterceptor] =preHandle end**********************
2017-07-21 11:23:22 INFO 33 [boce.demo.controller.SessionController] session value=boce.com1500607402364
............
2017-07-21 11:24:35 INFO 56 [boce.demo.controller.SessionController] sessionId:42b9e5d6-2a9f-4c0e-ae9e-2eb2ad1119cc;createTime:1500607429438;lastAccessdTime:1500607468057maxInactive:1800
2017-07-21 11:24:35 INFO 60 [boce.demo.controller.SessionController] session value=boce.com1500607429438;time1:null