上一篇關於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 唉!