攔截器 struts2 核心css
注:本文系做者在看了浪曦的風中葉老師的struts2視頻的我的總結,但願能幫助廣大struts2的初學者。
第一步:(這一步和其餘同樣,這裏從簡)依舊是新建一個web project,命名爲interceptor1,導入struts2必須的包。在src目錄下新建struts.xml,修改web.xml文件。
第二步:在前面幾講中已經對一個struts2的web project有了一個具體的說明 這裏只給代碼:
目錄結構爲:
web.xml
html
<?xml version="1.0" encoding="UTF-8"?> web
<web-app version="2.4" apache
xmlns="http://java.sun.com/xml/ns/j2ee" 瀏覽器
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tomcat
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee app
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 框架
<filter> jsp
<filter-name>struts2</filter-name>
<!-- 控制器 -->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- 任何請求均有過濾器 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<s:form action="test_interceptor">
<s:submit name="submit"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'success.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>
去看看控制檯吧
</body>
</html>
Test_InterceptorAction
package cn.edu.hdu.action;
import com.opensymphony.xwork2.ActionSupport;
public class Test_InterceptorAction extends ActionSupport
{
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception
{
return SUCCESS;
}
public String test() throws Exception
{
System.out.println("此時全部攔截器完畢,已經調用了action中的test方法");
return SUCCESS;
}
}
以上幾個文件基本沒有什麼花頭,接下去咱們寫本身定義的攔截器MyInterceptor.java
如今src下建包 cn.edu.hdu.interceptor 在裏面建攔截器(說白了就是一個類)
代碼以下:注意一些註釋:
package cn.edu.hdu.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor implements Interceptor {
public void destroy() {
System.out.println("destroy");
}
public void init() {
System.out.println("攔截器已經被加載");
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("調用intercept方法");
String result = invocation.invoke(); // invocation.invoke()方法檢查是否還有攔截器 有的話繼續調用餘下的攔截器 沒有了 執行action的業務邏輯
return result;
}
}
這個類使咱們本身定義的。
1.繼承interceptor接口中的三個方法
public void init();
public void destroy();
public String interceptor(ActionInvocation invocation);
2.在interceptor中調用invocation.invoke(); 當執行完當前攔截器。檢查是否還有攔截器須要存在。
好了。攔截器寫好了。要怎樣將它插入action中去呢?
struts.xml
注意一些註釋
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="interceptor1" extends="struts-default">
<!-- 定義攔截器 -->
<interceptors>
<interceptor name="myInterceptor" class="cn.edu.hdu.interceptor.MyInterceptor"></interceptor>
</interceptors>
<!-- 配置action -->
<action name="test_interceptor" calss="cn.edu.hdu.action.Test_InterceptorAction">
<result name="success">/success.jsp</result>
<result name="input">/test.jsp</result>
<!-- 將聲明好的攔截器插入action中 -->
<interceptor-ref name="myInterceptor"></interceptor-ref>
</action>
</package>
</struts>
好了。如今來檢測一下吧:
首先是啓動tomcat
注意控制檯的信息,以下:
看到中文那行了吧,說明什麼? 本身想咯……
瀏覽器中輸入:
http://localhost:8080/interceptor1/test.jsp
單擊submit
發現控制檯有什麼?
刷新試試看?
再刷?
再試試看?
哈哈
附件中有源碼
Struts2輸入校驗2(框架效驗)———stru ... | struts2 核心攔截器2 (微微進階)——stru ...
interceptor1.rar (3.2 MB)
下載次數: 274
interceptor1.rar (3.2 MB)
下載次數: 49