关闭

实现一个简单的拦截器

标签: 数据class函数
124人阅读 评论(0) 收藏 举报
分类:

 代码:

reg.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix = "s"  uri ="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts 2拦截器应用</title>
</head>
<body>
<h1>用户注册</h1>
<s:form id="id" action="helloaction">
 <s:textfield name="username" label="用户名"></s:textfield>
 <s:password name="pass1" label="密码"></s:password>
 <s:password name="pass2" label="重复密码"></s:password>
 <s:submit value="注册"></s:submit>
</s:form>
</body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>struts2 拦截器应用</title>
</head>
<body>
<h2>用户名:<s:property value="username"/></h2>
<h2>密码:<s:property value="pass1"/></h2>
</body>
</html>

HelloAction.java

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private String username;
	private String pass1;
	private String pass2;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPass1() {
		return pass1;
	}
	public void setPass1(String pass1) {
		this.pass1 = pass1;
	}
	public String getPass2() {
		return pass2;
	}
	public void setPass2(String pass2) {
		this.pass2 = pass2;
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if(username!=null && getPass1().equals(getPass2()) && !getUsername().trim().equals("")){
			System.out.print("正在执行action!……");
			return SUCCESS;
		}
		else{
			System.out.print("正在执行action!……");
			return INPUT;
		}
	}
	
	

}

HelloInterceptor.java(拦截器)

package com.inter;

import com.action.HelloAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class HelloInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1L;
	public String intercept(ActionInvocation arg0) throws Exception {
		@SuppressWarnings("unused")
		HelloAction haAction=(HelloAction)arg0.getAction();
		System.out.print("拦截器开始运行……");
		String resuletString = arg0.invoke();
		System.out.println("拦截器已结束……");

		return resuletString;
	}

}

FilterHelloInterceptor.java

package com.inter;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class FilterHelloInterceptor extends MethodFilterInterceptor {
	private static final long serialVersionUID = 1L;
	protected String doIntercept(ActionInvocation arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.print("逻辑的实现过程是一样的,只是方法名不同而已!");
		String reString = arg0.invoke();
		return reString;
	}
	@Override
	public void setExcludeMethods(String excludeMethods) {
		// TODO Auto-generated method stub
		super.setExcludeMethods(excludeMethods);
	}
	@Override
	public void setIncludeMethods(String includeMethods) {
		// TODO Auto-generated method stub
		super.setIncludeMethods(includeMethods);
	}
	
}

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="HelloIntercertor" extends="struts-default">
      <interceptors>
          <interceptor name="helloInterceptor" class="com.inter.HelloInterceptor"></interceptor>
      </interceptors>
      
      <action name="helloaction" class="com.action.HelloAction">
          <result name="success">/success.jsp</result>
		  <result name="input">/reg.jsp</result>
		  
		  <interceptor-ref name="defaultStack"></interceptor-ref>
		  <interceptor-ref name="helloInterceptor"></interceptor-ref>
      </action>
  </package>
</struts>

截图:




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:228412次
    • 积分:9011
    • 等级:
    • 排名:第2056名
    • 原创:699篇
    • 转载:6篇
    • 译文:0篇
    • 评论:66条
    最新评论