・web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Struts2InterceptorExample</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- <listener> -->
<!-- <listener-class>com.listeners.SessionListener</listener-class> -->
<!-- </listener> -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
</web-app>
・welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Page</title>
</head>
<body>
<h3><s:actionerror/>
Welcome<br/>
<s:set var="data" value="#session.userInfo.userId" scope="request"/>
<s:textfield label="ユーザーID" type="text" value="%{#request.data}" />
<br/>
<s:property value="#session.userInfo.password"/>
</h3>
<s:form method="post" action="logout">
<s:submit value="ログアウト" />
</s:form>
</body>
</html>
・login.jsp
<!DOCTYPE HTML>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<s:actionerror/>
<s:form method="post" action="auth">
<s:textfield label="ユーザーID" type="text" name="userInfo.userId" />
<br/>
<s:password label="パスワード" type="password" name="userInfo.password" />
<s:submit value="ログイン" />
</s:form>
</body>
</html>
・struts.xml
<?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>
<constant name="struts.convention.result.path" value="/"></constant>
<!-- 任意のメソッドの実行を許可しない -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="user" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="session"
class="com.interceptors.SessionInterceptor"></interceptor>
<interceptor-stack name="sessionStack">
<interceptor-ref name="session"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="sessionStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirect">/login.action</result>
<!-- <result name="login">/login.action</result> -->
</global-results>
<!-- <action name="sessionAuth"> -->
<!-- <interceptor-ref name="sessionStack" /> -->
<!-- <result name="success">/success.jsp</result> -->
<!-- <result name="session">/sessionexpired.jsp</result> -->
<!-- </action> -->
<action name="login">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/WEB-INF/pages/login.jsp</result>
</action>
<action name="logout" class="com.actions.LogoutAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="auth" class="com.actions.LoginAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success" type="redirect">/welcome.action</result>
<result name="input">/WEB-INF/pages/login.jsp</result>
</action>
<!-- <action name="welcome" class="com.actions.WelcomeAction"> -->
<action name="welcome">
<result name="success">/WEB-INF/pages/welcome.jsp</result>
</action>
</package>
</struts>
・SessionListener.java
package com.listeners;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class SessionListener implements HttpSessionBindingListener{
private String aaa;
@Override
public void valueBound(HttpSessionBindingEvent arg0) {
// TODO 自動生成されたメソッド・スタブ
System.out.println("valueBound");
}
@Override
public void valueUnbound(HttpSessionBindingEvent arg0) {
// TODO 自動生成されたメソッド・スタブ
System.out.println("valueUnBound");
}
}
・SessionInterceptor.java
package com.interceptors;
import java.util.Map;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
@Results({
@Result(name = Action.SUCCESS, location="/WEB-INF/pages/welcome.jsp"),
})
public class SessionInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String,Object> session = invocation.getInvocationContext().getSession();
if(session.isEmpty()) {
return Action.LOGIN;
} else {
return invocation.invoke();
}
}
}
・UserInfo.java
package com.beans;
public class UserInfo {
private String userId;
private String password;
public UserInfo(){}
public UserInfo(String userId, String password) {
this.userId = userId;
this.password = password;
}
public String getUserId() {
return userId;
}
public void setUserId(String userName) {
this.userId = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
・LogoutAction.java
package com.actions;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class LogoutAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
public String execute() {
HttpSession session = ServletActionContext.getRequest().getSession(true);
session.invalidate();
return Action.LOGIN;
}
}
・LoginAction.java
package com.actions;
import javax.servlet.http.HttpSession;
import java.sql.*;
import org.apache.struts2.ServletActionContext;
import com.beans.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
/**
* ログイン処理を行うアクションメソッドです。
*
*/
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private UserInfo userInfo;
/**
* @return userInfo
*/
public UserInfo getUserInfo() {
return userInfo;
}
/**
* @param userInfo セットする userInfo
*/
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
// public String execute() {
// // ここに処理を記載するとvalidateの後で実行されてしまう。
// return Action.SUCCESS;
// }
public void validate() {
if ("admin".equals(userInfo.getUserId())) {
ServletActionContext.getRequest().getSession(true).invalidate();
HttpSession newsession = ServletActionContext.getRequest().getSession(true);
newsession.setAttribute("userInfo", new UserInfo(userInfo.getUserId(), userInfo.getPassword()));
} else {
addActionError("userid または password が違います。");
}
}
}