struts2生成session的方法:ActionContext.getContext().getSession()

The original source code has a lot of bugs. The two sessions,one in jsp <% and the other in <s:property are different things.

I made the following changes:

success2.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1" import="java.util.*"%>
<%@ page import="com.opensymphony.xwork2.ActionContext;"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome, you have logined!</title>
</head>
<body>
	Welcome, you have logined.
	<p>
		<%
			Map session2 = ActionContext.getContext().getSession();
			out.println(session2.get("logged-in"));
			out.println("<br/>");
			out.println("nothing");
		%>
	</p>
	<br />
	<b>Session Time: </b><%=new Date(session.getLastAccessedTime())%>
	<br />
	<br />
	<a href="<%=request.getContextPath()%>/vaannila/logoutAction">Logout</a>
	<br /> from s:property
	<br />
	<s:property value="#session2.logged-in"></s:property>
	s:property
	value="ActionContext.getContext().getSession().get("logged-in")"/s:property
	<%
		out.println(ActionContext.getContext().getSession()
				.get("logged-in"));
	%>
	<s:property value="#session.getlastAccessedTime()"></s:property>

</body>
</html>
public class logoutAction extends ActionSupport {
	@Override
	public String execute() throws Exception {
		System.out
				.println("the action name in struts and href in jsp is a little bit of different!");
		Map session = ActionContext.getContext().getSession();
		System.out.println("inside logout class");
		System.out.println(session.get("logged-in"));
		session.remove("logged-in");
		return SUCCESS;
	}
}
public class loginAction1 extends ActionSupport {

	private String userId;
	private String password;

	// public Map session=ActionContext.getContext().getSession();;

	@Override
	public String execute() throws Exception {

		if ("admin".equals(userId) && "admin".equals(password)) {
			Map session = ActionContext.getContext().getSession();
			session.put("logged-in", "true");
			System.out.println("inside execute");
			System.out.println(session.get("logged-in"));
			return SUCCESS;
		} else {
			return ERROR;
		}
		// return SUCCESS;
	}

	public String logout() throws Exception {

		// Map session = ActionContext.getContext().getSession();
		// this method here is useless,it should sits in logoutAction.java
		/*
		 * System.out.println("inside logout");
		 * System.out.println(session.get("logged-in"));
		 * session.remove("logged-in"); return SUCCESS;
		 */
		return "nima";
	}
 
bug infested source: http://blog.csdn.net/weiyanghuadi/article/details/9081857

hope bug free:http://pan.baidu.com/share/link?shareid=4002435387&uk=3878681452


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值