MVC框架-mentawai(6)

过滤器

过滤器是mentawai框架的组件。你可以在应用的某个action中进行配置,也可以对所有action进行配置。

在应用管理器中创建

// 应用于指定action上的过滤器

    @Override 
    public void loadActions() {  

        action("/Hello", HelloAction.class, "sayHi")
            .filter(new SomeFilter())  
            .on(SUCCESS,   redir("/index.jsp"));  
    }  

// 一个作用于所有action的全局过滤器

    @Override 
    public void loadFilters() {  

        filter(new SomeGlobalFilter());  
    }

基本使用示例:

import org.mentawai.core.*;  

import java.util.*;  

public class CacheFilter implements Filter {  

    private static final String KEY = "cache";  

    @Override
    public String filter(InvocationChain chain) throws Exception {  

        Action action = chain.getAction();  

        Context application = action.getApplication();  

        Map<String, Object> cache = (Map<String, Object>) application.getAttribute(KEY);  

        if (cache != null) {  

            Input input = action.getInput();  

            input.setValue(KEY, cache);  

        }  

        return chain.invoke(); // next filter or the action  
    }  

    @Override
    public void destroy() { }  
}  

一个简单的授权过滤器
tips:你不用编写授权过滤器,因为这是框架的特性之一

import org.mentawai.core.*;  

public class AuthenticationFilter implements Filter {  

    public static final String LOGIN = "login";  

    @Override
    public String filter(InvocationChain chain) throws Exception {  

        Action action = chain.getAction();         

        Context session = action.getSession();  

        if (session.hasAttribute("user")) {  

            return chain.invoke();  

        }  

        return LOGIN;  
    }  

    @Override      
    public void destroy() { }  
} 

在action执行之前或之后做修改

import org.mentawai.core.*;  

import java.util.Date;  

public class TimerFilter implements Filter {  

    @Override
    public String filter(InvocationChain chain) throws Exception {  

        Action action = chain.getAction();  

        Output output = action.getOutput();  

        output.setValue("timeBefore", new Date()); // 在action执行之前 

        long now = System.currentTimeMillis();  

        String result = chain.invoke();  

        long totalTime = System.currentTimeMillis() - now;  

        output.setValue("timeAfter", new Date()); // action执行之后

        output.setValue("totalTime", totalTime);  

        return result;  
    }  

    @Override
    public void destroy() { }  

}  

在action结果执行之后修改

package examples.helloworld.filter;  

import org.mentawai.core.*;  

import java.util.Date;  

public class TimerFilter implements AfterConsequenceFilter {  

    @Override 
    public String filter(InvocationChain chain) throws Exception {  

        Action action = chain.getAction();  

        Output output = action.getOutput();  

        output.setValue("timeBefore", new Date().toString()); // before the action execution...  

        long now = System.currentTimeMillis();  

        String result = chain.invoke();  

        long totalTime = System.currentTimeMillis() - now;  

        output.setValue("timeAfter", new Date().toString()); // after the action execution...  

        output.setValue("totalTime", totalTime);  

        // save the initial time for the consequence here...  

        Input input = action.getInput();  

        input.setValue("initialTime", System.currentTimeMillis());  

        return result;  
    }  

    @Override
    public void afterConsequence(Action action, Consequence c, boolean conseqExecuted, boolean actionExecuted, String result) {  

        // Note: You should understand that after the consequence is executed it is too
        // late to include anything in the response to the client.

        if (!conseqExecuted) {  

            System.out.println("There was an error executing the consequence!");  

        } else {  

            Input input = action.getInput();  

            Long initialTime = (Long) input.getValue("initialTime");  

            long totalTime = System.currentTimeMillis() - initialTime.longValue();  

            System.out.println("The total time for the consequence was: " + totalTime);  
        }  
    }  

    @Override 
    public void destroy() { }  
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值