码迷,mamicode.com
首页 > 其他好文 > 详细

struts2拦截器の简单实现(日语系统,请忽略乱码,重在实现)

时间:2016-08-23 11:34:01      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

1.创建类实现interceptor接口或者继承abstractinter~~~类

package com.mi.intercepter;

import java.util.Date;

import com.mi.action.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class Intercep implements Interceptor{

    private static final long serialVersionUID = 1L;

    private String name;
    @Override
    public void destroy() {
        System.out.println("destroy is run************");
    }

    @Override
    public void init() {
        System.out.println("init is run************");
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        LoginAction loginAction = (LoginAction) invocation.getAction();
        
        System.out.println(getName()+"the***starttime is***  "+new Date());
        long start = System.currentTimeMillis();
        String result = invocation.invoke();
        
        System.out.println("the endtime***  "+new Date());
        long end = System.currentTimeMillis();
        
        System.out.println(getName()+"the costtime is ***  "+(end-start)+"毫秒");
        System.out.println("result s result***  "+result);
        return result;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2.在struts.xml文件中配置已经创建的拦截器,并且配置要拦截诶的action

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="utf-8" />

    <package name="lee" extends="struts-default">
    
        <interceptors>
            <interceptor name="simple" class="com.mi.intercepter.Intercep">
                <param name="name">简单拦截器</param>
            </interceptor>
        </interceptors>
        
        <!-- 用户注册的Action -->
        <action name="regist" class="com.mi.action.RegistAction">
            <!-- 类型转换失败、输入校验失败,转入input对应的视图 -->
            <result name="input">/content/registForm.jsp</result>
            <result>/content/show.jsp</result>
        </action>

        <action name="login" class="com.mi.action.LoginAction">
            <result name="error">/content/error.jsp</result>
            <result name="success">/content/welcome.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="simple">
                <param name="name">改名后的拦截器</param>
            </interceptor-ref>
        </action>
        <action name="*">
            <result>/content/{1}.jsp</result>
        </action>

    </package>
</struts>

3.我的loginAction

package com.mi.action;

import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;

    // username??setter??getter????
    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    // password??setter??getter????
    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public String execute() throws Exception {
        System.out.println("??execute??????????..........");
        Thread.sleep(1500);
        if (getUsername().equals("tangxiujuan") && getPassword().equals("tangxiujuan")) {
            return SUCCESS;
        }
        return ERROR;
    }
}

4.此时控制台会打印出如下内容,请忽略乱码!!!重点在于拦截器!

技术分享

 

struts2拦截器の简单实现(日语系统,请忽略乱码,重在实现)

标签:

原文地址:http://www.cnblogs.com/tingbogiu/p/5798490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!