struts2中action里怎么获取session的值,小弟我菜

 我来答
就烦条0o
2016-08-01 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46485
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
java:

package com.lil.test_ch05_02.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RedirectTest extends ActionSupport {
private String username;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username=username;
}

public String execute() throws Exception {
Map user=(Map)ActionContext.getContext().getSession();
user.put("tmp", "zhangsan");

return SUCCESS;
}
}

Webroot下jsp:Test01.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'Test01.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="redirect.action" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>

This is test01 page. <br>
init, no value, tmp=<s:property value="#session.tmp" />
</body>
</html>

Test02.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'Test02.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>
This is test02 page. <br>
get value from pg1, tmp=<s:property value="#session.tmp" />
</body>
</html>

struts.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>
<package name="default" extends="struts-default">
<action name="redirect" class="com.lil.test_ch05_02.action.RedirectTest">
<result type="redirect">/Test02.jsp?username=${username}</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式