上一篇关于session的问题经过我的验证,发现有些情况没有描述或者现象有出入,所以这次一切以这次的为准来谈论。
下面先把我的代码贴上
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Checkdome</display-name>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="Checkdome" extends="struts-default" namespace="/">
<action name="login" class="com.Action.test">
<result name="success">/login.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
222222222222222222222222222
<form action="<%=request.getContextPath()%>/login.action" method="get">
<input type="submit" value="this ok">
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>var a = '<s:property value="#session.ses"/>';
<span style="white-space:pre"> </span>var b = '<s:property value="#request.req"/>';
<span style="white-space:pre"> </span>var c = <%=session.getAttribute("ses")%>;
</script>
</head>
<body>
<h3>session:</h3>
<input type="text" name="div1">
<s:property value="#session.ses"/>
<h3>request</h3>
<input type="text" name="div2">
<h3>EL</h3>
<input type="text" name="div3">
</body>
</html>
test.java
/**
*
*/
/**
* @author Administrator
*
*/
package com.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class test extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
HttpServletRequest req = ServletActionContext.getRequest();
HttpSession session = req.getSession();
req.setAttribute( "req" , "req" );
session.setAttribute( "ses" , "session" );
System.out.print("333333333333333333333333333333333");
return SUCCESS;
}
}
这就是全部代码,下面开始讲述验证步骤:
1.输入http://localhost:8880/Checkdome/login.action
如图所示,JS中的'<s:property value="#session.ses"/>'并没有取到session,JSP中的<s:property value="#session.ses"/>也没有取得session,这一点就和上篇说的出入了。
var c = <%=session.getAttribute("ses")%>也可以获取session值
但是'<s:property value="#request.req"/>'取得值。
刷新页面画面显示:
2.输入http://localhost:8880/Checkdome/
点击按钮后显示结果:
session值取到了,这一次通过两种不同的方式,却获得了不同的结果。
这是关于获取session的一次验证,得到的问题就是
1.为什么S标签获取不到session值,刷新后就能获得?
2.为什么S标签获取的到request值?
图片被吃了,无法上传。
o(︶︿︶)o 唉!