Spring MVC 使用Session

来源:互联网 发布:c语言非递归二叉树深度 编辑:程序博客网 时间:2024/05/17 08:54

对于如何使用ajax交互并返回json,参考:
http://blog.csdn.net/yixiaoping/article/details/45281721

HTML

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>    <input type="button" value="设置" id="set" />    <input type="button" value="获取" id="get" /></body><script type="text/javascript"    src="/LearnSpringMVCDemo/js/jquery/jquery.min.js"></script><script type="text/javascript">    $("#set").click(function() {        $.ajax({            url : "/LearnSpringMVCDemo/session/set2",            type : "post",            dataType : "json",            contentType : "application/json; charset=utf-8",            success : function(data) {                alert(data.msg);            }        });    });    $("#get").click(function() {        $.ajax({            url : "/LearnSpringMVCDemo/session/get2",            type : "post",            dataType : "json",            contentType : "application/json; charset=utf-8",            success : function(data) {                alert(data.msg);            }        });    });</script></html>

Controller

package com.learndemo.learn;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.SessionAttributes;@Controller@SessionAttributes(types = String.class, value = "session2")@RequestMapping(value = "/session")public class LearnSession {    @RequestMapping(value = "/begin")    public String begin() {        return "learn/session";    }    /**     * 方法一:直接使用HttpSession进行设置和获取     *      * @param session     * @return     */    @ResponseBody    @RequestMapping(value = "/set1", method = RequestMethod.POST)    public Map<String, String> setSession1(HttpSession session) {        session.setAttribute("session1", "session1");        Map<String, String> map = new HashMap<String, String>();        map.put("msg", "设置完毕!");        return map;    }    @ResponseBody    @RequestMapping(value = "/get1", method = RequestMethod.POST)    public Map<String, Object> getSession1(HttpSession session) {        Map<String, Object> map = new HashMap<String, Object>();        map.put("msg", session.getAttribute("session1"));        return map;    }    /**     * 方法二,使用@SessionAttributes和@ModelAttribute进行设置和获取     * 设置:添加@SessionAttributes注解,并且里边的value,对应的使用Model进行设置。     * 并且@SessionAttributes 允许设置多个value和对应的type,例如:     * types = {String.class,Object.class},value = {"session1","session2"}     * 使用@ModelAttribute获取对应的session值,并且value值必须相同     * @param model     * @return     */    @ResponseBody    @RequestMapping(value = "/set2", method = RequestMethod.POST)    public Map<String, String> setSession2(Model model) {        model.addAttribute("session2", "session2");        Map<String, String> map = new HashMap<String, String>();        map.put("msg", "设置完毕!");        return map;    }    @ResponseBody    @RequestMapping(value = "/get2", method = RequestMethod.POST)    public Map<String, String> getSession2(@ModelAttribute(value = "session2") String session2) {        Map<String, String> map = new HashMap<String, String>();        map.put("msg", session2);        return map;    }}
0 0
原创粉丝点击