struts2自定义拦截器
Posted 风清云流
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2自定义拦截器相关的知识,希望对你有一定的参考价值。
1、index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP \'index.jsp\' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="login.jsp">login.jsp</a> </body> </html>
2、login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ taglib prefix="struts" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP \'login.jsp\' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <struts:form action="authentication" method="post"> <struts:label value="登录系统"></struts:label> <struts:textfield name="account" label="帐号"></struts:textfield> <struts:password name="password" label="密码"></struts:password> <struts:submit value="登录"></struts:submit> </struts:form> </body> </html>
3、
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="/struts-tags" prefix="struts"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP \'index.jsp\' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <struts:head theme="ajax" /> <style type="text/css"> body,td { font-size: 12px; } </style> </head> <body> <struts:property value="message" /> </body> </html>
4、struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="main" extends="struts-default"> <!-- 自定义一个拦截器 --> <interceptors> <interceptor name="authentication" class="com.ouyang.struts2.interceptor.AuthenticationInterceptor"> </interceptor> </interceptors> <global-results> <result name="login">/login.jsp</result> </global-results> <action name="authentication" class="com.ouyang.struts2.action.AuthenticationAction"> <interceptor-ref name="authentication"></interceptor-ref> <result>/authenticationSuccess.jsp</result> </action> </package> </struts>
5、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>customizedInterceptor</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
6、AuthenticationAction.java
package com.ouyang.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class AuthenticationAction extends ActionSupport { private static final long serialVersionUID = -6161973490186833069L; private String message; public String execute() { message = "您正在访问 AuthenticationAction. "; return SUCCESS; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
7、AuthenticationInterceptor.java
package com.ouyang.struts2.interceptor; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthenticationInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -4433771430728214868L; @Override @SuppressWarnings("all") public String intercept(ActionInvocation invocation) throws Exception { Map<String, Object> sessionValues = invocation.getInvocationContext() .getSession(); String account = (String) sessionValues.get("account"); if(account!=null){ System.out.println(account); } if (account == null) { System.out.println("您输入的用户名为空!"); return Action.LOGIN; } else { return invocation.invoke(); } } }
8、文件列表
以上是关于struts2自定义拦截器的主要内容,如果未能解决你的问题,请参考以下文章