jsp:
簡單的一個登陸頁面。
<%--
Created by IntelliJ IDEA.
User: bin
Date: 2020-07-02
Time: 13:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="login.action" method="post">
賬號:<input name="user.name" type="text"><br>
密碼:<input name="user.password" type="text"><br>
<input type="submit" value="註冊">
</form>
</body>
</html>
登陸成功後才能訪問的頁面。就是測試能不能訪問的。
<html>
<body>
<h2>shout!</h2>
</body>
</html>
登陸成功的頁面,一樣時測試的。
<%@page isELIgnored="false" pageEncoding="UTF-8" %>
<html>
<body>
<h2>bins
!</h2>
歡迎回來${user.name}
</body>
</html>
action:
登陸的邏輯方法,簡單的判斷
package com.bin.action;
import com.bin.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.util.Map;
public class LoginAction extends ActionSupport {
public User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login(){
System.out.println("進來了");
if ("bin".equals(user.getName())&&"bin".equals(user.getPassword())){
System.out.println("登陸成功");
Map<String, Object> sessionMap = ActionContext.getContext().getSession();
sessionMap.put("user",user);
return SUCCESS;
}else {
return ERROR;
}
}
public String registr(){
return SUCCESS;
}
public String sss(){
return SUCCESS;
}
}
Interceptor:
自定義的攔截器
package com.bin.interceptor;
import com.bin.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.Map;
public class myInterceptor implements Interceptor {
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//得到當前攔截的的目標Action代理對象(那個action請求來訪問就獲取這個action的代理對象。就好像給這個對象起個別名)
ActionProxy proxy = invocation.getProxy();
////得到當前執行的action的方法,action請求中的執行方法。
String proxyMethod = proxy.getMethod();
//簡單的判斷,這個是不攔截的方法,類似於登陸,註冊之類的
if ("login".equals(proxyMethod)||"registr".equals(proxyMethod)){
//這就話就是放行
invocation.invoke();
}
//獲取session域中的數據
Map<String, Object> sessionMap = ActionContext.getContext().getSession();
User user = (User) sessionMap.get("user");
if (user==null){
//判斷是否存在,不存在就跳轉回登陸頁面
return "login";
}else {
//方形
return invocation.invoke();
}
}
}
user:
別忘了創建實體類了
package com.bin.entity;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "user{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
struts.xml:
最重點的來的,xml的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--自己定義的默認包,繼承struts2中默認額攔截器包中的18個攔截器,同時還使用了自己定義的攔截器 -->
<package name="myDefault" extends="struts-default" abstract="true">
<!--定義攔截器 -->
<interceptors>
<!--自己定義的攔截器,在這裏配置到xml文件中-->
<interceptor name="myInterceptor" class="com.bin.interceptor.myInterceptor"></interceptor>
<!--攔截器棧,定義自己的攔截器棧,裏面注入的是struts2默認的攔截器和我們自己寫的攔截器-->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--這是在全局使用的我們的攔截器棧-->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
</package>
<package name="default" extends="myDefault" namespace="/">
<!--定義的全局變量,只要是返回值爲login的,都走這個result-->
<global-results>
<result name="login">/logini.jsp</result>
</global-results>
<!--登陸action的配置-->
<action name="login" class="com.bin.action.LoginAction" method="login">
<result name="success">/WEB-INF/jsp/bins.jsp</result>
<result name="error">/index.jsp</result>
</action>
<!--這個是登陸之後才能訪問的-->
<action name="bin" class="com.bin.action.LoginAction" method="sss">
<result name="success">/WEB-INF/jsp/shout.jsp</result>
</action>
<!-- 需要權限才能訪問的頁面-->
<action name="*">
<result>/WEB-INF/jsp/*.jsp</result>
</action>
</package>
</struts>