地址: http://struts2.group.iteye.com/group/wiki/1654-18-through-session-login-logout
在本章我们将通过Session创建一个login/logout程序.该程序会对用户进行身份验证.每当你运行的时候它将获取用户id和密码(用户名/密码都是"admin").当两个字段都是正确的,显示欢迎页面.
在struts.xml中创建Action映射.这就是要添加到struts.xml中的代码 :
- <action name="login" class="net.roseindia.loginAction" >
- <result name="success" type="dispatcher">/pages/uiTags/Success.jsp</result>
- <result name="error" type="redirect">/pages/uiTags/Login.jsp</result>
- </action>
- <action name="logout" class="net.roseindia.logoutAction" >
- <result name="success" type="redirect">/pages/uiTags/checkLogin.jsp</result>
- </action>
<action name="login" class="net.roseindia.loginAction" > <result name="success" type="dispatcher">/pages/uiTags/Success.jsp</result> <result name="error" type="redirect">/pages/uiTags/Login.jsp</result></action><action name="logout" class="net.roseindia.logoutAction" > <result name="success" type="redirect">/pages/uiTags/checkLogin.jsp</result></action>
创建一个Action类来处理login请求.struts2框架提供了一个基础的ActionSupport类,它实现了常用的框架接口.在我们的Action类中(loginAction.java)我们继承了ActionSupport类.
这就是"loginAction"的代码 :
- package net.roseindia;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ActionContext;
- import java.util.*;
- public class loginAction extends ActionSupport {
- private String userId;
- private String password;
- public String execute() throws Exception{
- if ("admin".equals(userId) && "admin".equals(password)) {
- Map session = ActionContext.getContext().getSession();
- session.put("logged-in","true");
- return SUCCESS;
- }
- else{
- return ERROR;
- }
- }
- public String logout() throws Exception {
- Map session = ActionContext.getContext().getSession();
- session.remove("logged-in");
- return SUCCESS;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getUserId() {
- return userId;
- }
- public void setUserId(String userId) {
- this.userId = userId;
- }
- }
package net.roseindia;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ActionContext;import java.util.*;public class loginAction extends ActionSupport { private String userId; private String password; public String execute() throws Exception{ if ("admin".equals(userId) && "admin".equals(password)) { Map session = ActionContext.getContext().getSession(); session.put("logged-in","true"); return SUCCESS; } else{ return ERROR; } } public String logout() throws Exception { Map session = ActionContext.getContext().getSession(); session.remove("logged-in"); return SUCCESS; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; }}
同样,创建一个Action类来处理登出操作.该类(logoutAction)继承了ActionSupport
这是logoutAction的代码
- package net.roseindia;
- import javax.servlet.http.HttpSession;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ActionContext;
- import java.util.*;
- public class logoutAction extends ActionSupport {
- public String execute() throws Exception {
- Map session = ActionContext.getContext().getSession();
- session.remove("logged-in");
- return SUCCESS;
- }
- }
package net.roseindia;import javax.servlet.http.HttpSession;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ActionContext;import java.util.*;public class logoutAction extends ActionSupport { public String execute() throws Exception { Map session = ActionContext.getContext().getSession(); session.remove("logged-in"); return SUCCESS; }}
创建Login表单:该程序的用户界面由login表单组成(Login.jsp). "Login.jsp"向用户显示了登录页面.
这是login.jsp文件的代码 :
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%@ page language="java" contentType="text/html"%>
- <html>
- <head>
- <title>Insert Data here!</title>
- <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
- type="text/css"/>
- </head>
- <body>
- <s:form action="/roseindia/login.action" method="POST">
- <s:textfield name="userId" label="Login Id"/><br>
- <s:password name="password" label="Password"/><br>
- <s:submit value="Login" align="center"/>
- </s:form>
- </body>
- </html>
<%@ taglib prefix="s" uri="/struts-tags" %><%@ page language="java" contentType="text/html"%><html> <head> <title>Insert Data here!</title> <link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/> </head> <body> <s:form action="/roseindia/login.action" method="POST"> <s:textfield name="userId" label="Login Id"/><br> <s:password name="password" label="Password"/><br> <s:submit value="Login" align="center"/> </s:form> </body></html>
当用户得到了成功的身份验证,"Success.jsp"页面显示的登录的成功信息(Welcome, you have logged-in.)和session(Session Time: Wed Aug 01 11:26:38 GMT+05:30 2007 and Logout ).如果你点击"Logout",将再次显示login页面.
这是Success.jsp文件的代码 :
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%@ page language="java" contentType="text/html" import="java.util.*"%>
- <jsp:include page="/struts2tags/pages/uiTags/loginCheck.jsp" />
- <html>
- <head>
- <title>Welcome, you have logined!</title>
- </head>
- <body>
- Welcome, you have logined. <br />
- <b>Session Time: </b><%=new Date(session.getLastAccessedTime())%>
- <br /><br />
- <a href="<%= request.getContextPath() %>/roseindia/logout.action">Logout</a>
- <br />
- </body>
- </html>
<%@ taglib prefix="s" uri="/struts-tags" %><%@ page language="java" contentType="text/html" import="java.util.*"%><jsp:include page="/struts2tags/pages/uiTags/loginCheck.jsp" /><html> <head> <title>Welcome, you have logined!</title> </head> <body> Welcome, you have logined. <br /> <b>Session Time: </b><%=new Date(session.getLastAccessedTime())%> <br /><br /> <a href="<%= request.getContextPath() %>/roseindia/logout.action">Logout</a> <br /> </body></html>
该页面登出合法用户.
checkLogin.jsp
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%@ page language="java" contentType="text/html" import="java.util.*"%>
- <html>
- <head>
- <title>Check validate!</title>
- </head>
- <body>
- <s:if test="#session.login != 'admin'">
- <jsp:forward page="/pages/uiTags/Login.jsp" />
- </s:if>
- </body>
- </html>
<%@ taglib prefix="s" uri="/struts-tags" %><%@ page language="java" contentType="text/html" import="java.util.*"%><html> <head> <title>Check validate!</title> </head> <body> <s:if test="#session.login != 'admin'"> <jsp:forward page="/pages/uiTags/Login.jsp" /> </s:if> </body></html>
输出 :
运行程序获得登录页面 :
在login页面输入错误的用户id和密码 :
你将得到下面的输出 :
输入正确的用户id和密码 :
你将获得如下输出 :
点击"Logout"后,你将获得如下输出 :