MVC框架-mentawai(6)
来源:互联网 发布:手机歌曲剪辑软件 编辑:程序博客网 时间:2024/06/08 16:47
过滤器
过滤器是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() { } }
阅读全文
0 0
- MVC框架-mentawai(6)
- MVC框架-Mentawai(1)
- MVC框架-Mentawai(2)
- MVC框架-Mentawai(3)
- MVC框架-mentawai(4)
- MVC框架-mentawai(5)
- MVC框架-mentawai(7)
- MVC框架-mentawai(8)
- MVC框架-mentawai(9)
- MVC框架-mentawai(10)
- MVC框架-mentawai(11)
- MVC框架-mentawai(12)
- MVC框架-mentawai(13)
- MVC框架(1)
- MVC模式(框架)
- MVC框架(二)
- MVC框架(二)
- MVC封装框架 第一步 (分析mvc框架运行流程)
- Python __str__(self)和__unicode__(self)
- jQuery实现的增删改查
- 精通比特币
- oracle数据类型和对应的java类型
- C#实现cmd命令的相同效果
- MVC框架-mentawai(6)
- scrapy 元素的相对xpath
- AMD 和 CMD 的区别有哪些?
- TCP_NODELAY 和 TCP_NOPUSH的解释
- pytesseract 报windows err no2的错误
- python使用tkinter写带界面的工具
- python使用pyqt写带界面工具
- 基于pyteseract google ocr的图形验证码识别
- 51NOD 1276 岛屿的数量(脑洞+思维)