在spring mvc中,对session的操作有如下两个方法:
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public JsonResponse getSessionIncontroller
(@RequestBody String jsonReqString ,HttpSession session)
{
session.setAttribute("success" , "successfully accessed");
return jsonResponse;
}
或者就是直接通过sevrlet api去获得,如:
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public JsonResponse getSessionIncontroller
(@RequestBody String jsonReqString ,HttpServletRequest request)
{
Session session = request.getSession();
session.setAttribute("success" , "successfully accessed");
return jsonResponse;
}
也可以创建一个component为session的,然后进行封装为session的scope,然后再注入到controller中,如:
@Component
@Scope("session")
public class Profile
{
private String userName;
private String country;
.......
}
@Controller
@Scope("request")
public class UserController
{
@Autowired
private Profile profile;
@RequestMapping("/myOrders")
public String myOrders(@RequestParam("userId") int userId)
{
}
}