注:本文系作者在看了浪曦的风中叶老师的struts2视频的个人总结,希望能帮助广大struts2的初学者。
第一步:(这一步和其他一样,这里从简)依旧是新建一个web project,命名为interceptor1,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。
第二步:在前面几讲中已经对一个struts2的web project有了一个具体的说明 这里只给代码:
目录结构为:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<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