login.jsp
Html 2017
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>Insert title here</title>
</head>
<body>
<form action="loginUser" method="POST">
UserName:<input type="text" name="userName" /><br />
Password:<input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
<s:if test="hasActionMessages()">
<div class="welcome">
<s:actionmessage /> //message addActionMessage
</div>
</s:if>
</body>
</html>
home.jsp
Html 2017
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>Insert title here</title>
</head>
<body>
Welcome! <s:property value="%{#session.loginID}"/>
<br/><a href="logOut">LogOut</a>
</body>
</html>
LoginAction.java
Html 2017
package demo.Interceptor;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements SessionAware {
private String userName;
private String password;
private Map<String, Object> session = ActionContext.getContext().getSession();
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
public Map<String, Object> getSession() {
return session;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
if ("aaa".equals(userName) && "12345".equals(password)) {
session.put("loginID", userName);
System.out.println("Login success");
return SUCCESS;
} else {
addActionMessage("Username can't be blanked");
System.out.println("Login fail");
return LOGIN;
}
}
public String logOut() {
System.out.println("LogOut");
session.remove("loginID");
addActionMessage("You have been Successfully Logged Out");
return SUCCESS;
}
public String home() {
System.out.println("Welcome home");
return SUCCESS;
}
}
LoginInterceptor.java
Html 2017
package demo.Interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String, Object> session = invocation.getInvocationContext().getSession();
String loginId = (String) session.get("loginID");
if (loginId == null) {
//Neu null session thi khong cho vao home.jsp
return Action.LOGIN;
} else {
//Neu co session thi cho vao home.jsp
return invocation.invoke();
}
}
}
struts.xml
Html 2017
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor class="demo.Interceptor.LoginInterceptor" name="loginInterceptor">
</interceptor>
<interceptor-stack name="loginStack">
<interceptor-ref name="loginInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- login action -->
<action name="loginUser" class="demo.Interceptor.LoginAction" method="login">
<result name="success" type="redirect">homeAction</result>
<result name="login">login.jsp</result>
</action>
<!-- home link action -->
<action name="homeAction" class="demo.Interceptor.LoginAction" method="home">
<interceptor-ref name="loginStack" />
<result name="login">login.jsp</result>
<result name="success">home.jsp</result>
</action>
<!-- logout action -->
<action name="logOut" class="demo.Interceptor.LoginAction" method="logOut">
<result name="success">login.jsp</result>
</action>
</package>
</struts>
web.xml
Html 2017
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>DemoInterceptor</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
cho mình hỏi ad đã logout r thì làm s mà nhấn nút back vẫn còn hiện giá trị thế kia
ReplyDelete