struts2中拦截器的简单实现流程

2014-11-24 02:03:49 · 作者: · 浏览: 0

\

struts.xml文件的设置

  




  
	
   
	
   
	
   
	
   
	
    
     
      
      
       
      计时拦截器 
      
     
     
     
      
      
      
       
      hello计时 
      
      
     
      /index.
      jsp
      
     
   



  

自定义拦截器的配置

package interceptor;

import java.util.Date;

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

//继承抽象的拦截器类
public class ActionInterceptorAbstract extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 定义身份区分标识,接收struts.xml中XXX标签体的内容
	private String name;

	// 接收配置文件传入的数据
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		// 记录执行action中excute方法前的时间
		long start = System.currentTimeMillis();
		System.out.println(name + "调用前:" + new Date());

		// 调用excute方法
		// 并接受excute返回的结果
		String actionResult = invocation.invoke();

		// 记录执行action中excute方法后的时间
		long end = System.currentTimeMillis();

		System.out.println(name + "调用后:" + new Date());

		System.out.println(name + "执行用时:" + (end - start));

		// 将excute执行结果返回给配置文件的result
		return actionResult;
	}

}

action的配置

package action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWordAction extends ActionSupport {

	//定义一个字符串用例验证
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String execute() throws Exception {
		//赋值操作
		message = "Helloworld";
		System.out.println(message);
		return SUCCESS;
	}

}

index.jsp页面显示的配置

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>

<html>



My JSP 'index.jsp' starting page

  

  

  

  

  

  



	
  
	${message}