标签:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><filter><filter-name>jfinal</filter-name><filter-class>com.jfinal.core.JFinalFilter</filter-class><init-param><param-name>configClass</param-name><param-value>com.fw.config.MppConfig</param-value></init-param></filter><filter-mapping><filter-name>jfinal</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
public final class JFinalFilter implements Filter {private Handler handler;private String encoding;private JFinalConfig jfinalConfig;private Constants constants;private static final JFinal jfinal = JFinal.me();private static Log log;private int contextPathLength;public void init(FilterConfig filterConfig) throws ServletException {createJFinalConfig(filterConfig.getInitParameter("configClass"));if (jfinal.init(jfinalConfig, filterConfig.getServletContext()) == false)throw new RuntimeException("JFinal init error!");handler = jfinal.getHandler();constants = Config.getConstants();encoding = constants.getEncoding();jfinalConfig.afterJFinalStart();String contextPath = filterConfig.getServletContext().getContextPath();contextPathLength = (contextPath == null || "/".equals(contextPath) ? 0 : contextPath.length());}public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest)req;HttpServletResponse response = (HttpServletResponse)res;request.setCharacterEncoding(encoding);String target = request.getRequestURI();if (contextPathLength != 0)target = target.substring(contextPathLength);boolean[] isHandled = {false};try {handler.handle(target, request, response, isHandled);}catch (Exception e) {if (log.isErrorEnabled()) {String qs = request.getQueryString();log.error(qs == null ? target : target + "?" + qs, e);}}if (isHandled[0] == false)chain.doFilter(request, response);}public void destroy() {jfinalConfig.beforeJFinalStop();jfinal.stopPlugins();}private void createJFinalConfig(String configClass) {if (configClass == null)throw new RuntimeException("Please set configClass parameter of JFinalFilter in web.xml");Object temp = null;try {temp = Class.forName(configClass).newInstance();} catch (Exception e) {throw new RuntimeException("Can not create instance of class: " + configClass, e);}if (temp instanceof JFinalConfig)jfinalConfig = (JFinalConfig)temp;elsethrow new RuntimeException("Can not create instance of class: " + configClass + ". Please check the config in web.xml");}static void initLog() {log = Log.getLog(JFinalFilter.class);}}
boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) {this.servletContext = servletContext;this.contextPath = servletContext.getContextPath();initPathUtil();Config.configJFinal(jfinalConfig);constants = Config.getConstants();initActionMapping();initHandler();initRender();initOreillyCos();initTokenManager();return true;}
static void configJFinal(JFinalConfig jfinalConfig) {jfinalConfig.configConstant(constants); initLogFactory();jfinalConfig.configRoute(routes);jfinalConfig.configPlugin(plugins); startPlugins(); // very important!!!jfinalConfig.configInterceptor(interceptors);jfinalConfig.configHandler(handlers);}
public class MppConfig extends JFinalConfig {//配置常量public void configConstant(Constants me) {PropKit.use("jdbc.properties");me.setDevMode(PropKit.getBoolean("devMode", false));}//配置路由public void configRoute(Routes me) {me.add(new RoutesMapping());}public static C3p0Plugin createC3p0Plugin() {return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());}//配置插件public void configPlugin(Plugins me) {// 配置C3p0数据库连接池插件C3p0Plugin C3p0Plugin = createC3p0Plugin();me.add(C3p0Plugin);// 配置ActiveRecord插件ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);me.add(arp);// 配置属性名(字段名)大小写不敏感容器工厂 oraclearp.setContainerFactory(new CaseInsensitiveContainerFactory());//缓存插件me.add(new EhCachePlugin());// 所有配置在 MappingKit 中搞定ModelMapping.mapping(arp);}//配置全局拦截器public void configInterceptor(Interceptors me) {me.add(new SessionInViewInterceptor());//session拦截器,用于在View模板中取出session值}//配置处理器,接管所有 web 请求public void configHandler(Handlers me) {me.add(new ContextPathHandler("contextPath"));//设置上下文路径}//系统启动完成后回调,如创建调度线程public void afterJFinalStart(){}//系统关闭前回调,如写回缓存public void beforeJFinalStop(){}}
标签:
原文地址:http://www.cnblogs.com/xmsx/p/5535634.html