请各位大神帮我看一下为什么我的session取不到值,代码如下:
首先是index.jsp界面,这个界面就是模拟让用户输入一个数字,然后跳转到saveAction处理,DomainModel叫做userSave,只有一个属性 String save
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="save/save">
<input type="text" name="save.save">
<input type="submit" value="submit">
</form>
<s:debug></s:debug>
</body>
</html>
userSave Model:
package com.action;public class userSave {
public String save; public String getSave() {
return save;
} public void setSave(String save) {
this.save = save;
}
public String toString(){
return save;
}
}
saveAction:
package com.action;import java.util.ArrayList;import java.util.List;
import java.util.Map;import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;public class saveAction extends ActionSupport implements SessionAware{
private userSave save=new userSave();
List<userSave> saves;
Map<String,Object> session;
public saveAction(){
if(saves!=null){
saves.add(save);
session.put("save", "saves");
}else{
saves=new ArrayList<userSave>();
saves.add(save);
}
}
public userSave getSave() {
return save;
}
public List<userSave> getSaves() {
return saves;
}
public Map<String, Object> getSession() {
return session;
}
public void setSave(userSave save) {
this.save = save;
}
public void setSaves(List<userSave> saves) {
this.saves = saves;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public String add(){
return SUCCESS;
}
}
strtus.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.devMode" value="true" />
<package name="login" extends="struts-default" namespace="/save">
<action name="save" class="com.action.saveAction" >
<result>/show.jsp</result>
<result name="fail">/index.jsp</result>
</action>
</package>
</struts>show.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<s:property value="session.saves"></s:property>
<form action="index.jsp" method="post">
<input type="submit" value="return">
</form>
<s:debug></s:debug>
</body>
</html>
但是在这个页面观察value stack,session为空,
请各位大神指教为什么
sessionJava
首先是index.jsp界面,这个界面就是模拟让用户输入一个数字,然后跳转到saveAction处理,DomainModel叫做userSave,只有一个属性 String save
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="save/save">
<input type="text" name="save.save">
<input type="submit" value="submit">
</form>
<s:debug></s:debug>
</body>
</html>
userSave Model:
package com.action;public class userSave {
public String save; public String getSave() {
return save;
} public void setSave(String save) {
this.save = save;
}
public String toString(){
return save;
}
}
saveAction:
package com.action;import java.util.ArrayList;import java.util.List;
import java.util.Map;import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;public class saveAction extends ActionSupport implements SessionAware{
private userSave save=new userSave();
List<userSave> saves;
Map<String,Object> session;
public saveAction(){
if(saves!=null){
saves.add(save);
session.put("save", "saves");
}else{
saves=new ArrayList<userSave>();
saves.add(save);
}
}
public userSave getSave() {
return save;
}
public List<userSave> getSaves() {
return saves;
}
public Map<String, Object> getSession() {
return session;
}
public void setSave(userSave save) {
this.save = save;
}
public void setSaves(List<userSave> saves) {
this.saves = saves;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public String add(){
return SUCCESS;
}
}
strtus.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.devMode" value="true" />
<package name="login" extends="struts-default" namespace="/save">
<action name="save" class="com.action.saveAction" >
<result>/show.jsp</result>
<result name="fail">/index.jsp</result>
</action>
</package>
</struts>show.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<s:property value="session.saves"></s:property>
<form action="index.jsp" method="post">
<input type="submit" value="return">
</form>
<s:debug></s:debug>
</body>
</html>
但是在这个页面观察value stack,session为空,
请各位大神指教为什么
sessionJava
jsp页面上session可是默认的内置对象,换个名试试。