session的取值与传值
服务器设置session后,前台获取代码的⽅法有3种:
第⼀种是直接⽤el表达式,
第⼆种是⽤JSP表达式。
第三种是⽹上说的,⽤jquerySession取值。但是测试中⽆法取到,我的判断是,jquery⽆法直接通过服务器获取session,需要服务器先将session传到客户端前台,然后jquerySession才能取值。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
⾄于在客户端前台中 直接保存session是不允许的,需要通过服务器操作。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
controller中代码
@RequestMapping(value = "/demo")
public String Demo(HttpSession session){
session.setAttribute("adminAccount","username=admin;password=123456");
System.out.println("adminAccount: " + Attribute("adminAccount"));
System.out.println("test1: " + Attribute("test1"));
System.out.println("test2: " + Attribute("test2"));
System.out.println("test3: " + Attribute("test3"));
return "demo";
}
前台代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="java.sun/jsp/jstl/core" prefix="c" %>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path; %>
<% Object Attribute("adminAccount"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html lang="zh-CN">
<head>
<title>登陆</title>
<link href="<%=basePath%>/resources/css/bootstrap.min.css" rel="stylesheet">
<link href="<%=basePath%>/resources/css/bootstrap-theme.min.css" rel="stylesheet">
<%-- <link href="<%=basePath%>/resources/css/login.css" rel="stylesheet"> --%>
<script src="<%=basePath%>/resources/js/jquery-1.11.3.min.js"></script>
<script src="<%=basePath%>/resources/js/bootstrap.min.js"></script>
<script src="<%=basePath%>/resources/kie.js"></script>
<script src="<%=basePath%>/resources/js/jquerySession.js"></script>
<script>
$(document).ready(function(){
$.session.set("test1","testcontent");
var session = $.('adminAccount');
el表达式获取值var session2 = "${adminAccount}";
var test1 = $.('test1');
<% session.setAttribute("test2", "this is test2");
%>
$("button:eq(0)").click(function(){
alert(session);
alert(session2);
alert(test1);
<% session.setAttribute("test3", "this is test3");
%>
$("ol").append("<li>show session by el:adminAccount = ${adminAccount}</li>")
$("ol").append("<li>show session by el:test1 = ${test1}</li>")
$("ol").append("<li>show session by jq:test1 = " + test1 +"</li>")
return null;
});
});
</script>
</head>
<body>
<button>show session</button>
<ol>
<li>show session by asp:adminAccount = <%=sessionValues %></li>
<li>show session by el:adminAccount = ${adminAccount}</li>
</ol>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论