|
1 | 1 | package cn.netbuffer.jfinal_bootstrap_table.config;
|
2 | 2 |
|
3 | | -import java.io.File; |
4 | | - |
5 | | -import javax.servlet.http.HttpServletRequest; |
6 | | - |
7 | 3 | import cn.netbuffer.jfinal_bootstrap_table.interceptor.AuthInterceptor;
|
8 | 4 | import cn.netbuffer.jfinal_bootstrap_table.model._MappingKit;
|
9 | | - |
10 | 5 | import com.alibaba.druid.filter.logging.LogFilter;
|
11 | 6 | import com.alibaba.druid.filter.logging.Slf4jLogFilter;
|
12 | 7 | import com.alibaba.druid.filter.stat.StatFilter;
|
13 | 8 | import com.alibaba.druid.util.JdbcConstants;
|
14 | 9 | import com.alibaba.druid.wall.WallFilter;
|
15 | | -import com.jfinal.config.Constants; |
16 | | -import com.jfinal.config.Handlers; |
17 | | -import com.jfinal.config.Interceptors; |
18 | | -import com.jfinal.config.JFinalConfig; |
19 | | -import com.jfinal.config.Plugins; |
20 | | -import com.jfinal.config.Routes; |
| 10 | +import com.jfinal.config.*; |
21 | 11 | import com.jfinal.kit.PathKit;
|
22 | 12 | import com.jfinal.kit.Prop;
|
23 | 13 | import com.jfinal.kit.PropKit;
|
|
30 | 20 | import com.jfinal.plugin.ehcache.EhCachePlugin;
|
31 | 21 | import com.jfinal.render.ViewType;
|
32 | 22 | import com.jfinal.template.Engine;
|
33 | | -import org.slf4j.Logger; |
34 | | -import org.slf4j.LoggerFactory; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | + |
| 25 | +import javax.servlet.http.HttpServletRequest; |
| 26 | +import java.io.File; |
35 | 27 |
|
36 | 28 | /**
|
37 | 29 | * jfinal配置文件
|
38 | | - * |
39 | | - * @author netbuffer |
40 | 30 | *
|
| 31 | + * @author netbuffer |
41 | 32 | */
|
| 33 | +@Slf4j |
42 | 34 | public class AppConfig extends JFinalConfig {
|
43 | 35 |
|
44 | | - private static final Logger LOGGER= LoggerFactory.getLogger(AppConfig.class); |
| 36 | + /** |
| 37 | + * 启动完回调 |
| 38 | + */ |
| 39 | + public void afterJFinalStart() { |
| 40 | + /** |
| 41 | + * 添加setResolveBeanAsModel(boolean) |
| 42 | + * ,设置为true时,用于指定在JSP/jstl中,对待合体后的Bean仍然采用老版本对待Model的方式输出数据,也即使用 |
| 43 | + * Model.get(String attr)而非Bean的getter方法输出数据,有利于在关联查询时输出无 getter |
| 44 | + * 方法的字段值。建议mysql数据表中的字段采用驼峰命名,表名采用下划线方式命名便于win与linux间移植 |
| 45 | + */ |
| 46 | + ModelRecordElResolver.setResolveBeanAsModel(true); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 停止回调 |
| 51 | + */ |
| 52 | + public void beforeJFinalStop() { |
| 53 | + System.out.println("jfinal停止!"); |
| 54 | + } |
45 | 55 |
|
46 | | - /** |
47 | | - * 启动完回调 |
48 | | - */ |
49 | | - public void afterJFinalStart() { |
50 | | - /** |
51 | | - * 添加setResolveBeanAsModel(boolean) |
52 | | - * ,设置为true时,用于指定在JSP/jstl中,对待合体后的Bean仍然采用老版本对待Model的方式输出数据,也即使用 |
53 | | - * Model.get(String attr)而非Bean的getter方法输出数据,有利于在关联查询时输出无 getter |
54 | | - * 方法的字段值。建议mysql数据表中的字段采用驼峰命名,表名采用下划线方式命名便于win与linux间移植 |
55 | | - */ |
56 | | - ModelRecordElResolver.setResolveBeanAsModel(true); |
57 | | - } |
| 56 | + ; |
58 | 57 |
|
59 | | - /** |
60 | | - * 停止回调 |
61 | | - */ |
62 | | - public void beforeJFinalStop() { |
63 | | - System.out.println("jfinal停止!"); |
64 | | - }; |
| 58 | + /** |
| 59 | + * 常量配置 |
| 60 | + */ |
| 61 | + @Override |
| 62 | + public void configConstant(Constants me) { |
| 63 | + // 开启对 jfinal web 项目组件 Controller、Interceptor、Validator 的注入 |
| 64 | + me.setInjectDependency(true); |
| 65 | + // 开启对超类的注入。不开启时可以在超类中通过 Aop.get(...) 进行注入 |
| 66 | + me.setInjectSuperClass(true); |
| 67 | + Prop config = PropKit.use("config.properties"); |
| 68 | + log.info("config.properties:{}", config); |
| 69 | + me.setDevMode(config.getBoolean("app.devMode"));// 开发模式 |
| 70 | + me.setEncoding("utf8"); |
| 71 | + /** |
| 72 | + * 该路径参数接受以"/"打头或者以 windows 磁盘盘符打头的绝对路径, |
| 73 | + * 即可将基础路径指向项目根径之外,方便单机多实例部署。当该路径参数设置为相对路径时, 则是以项目根为基础的相对路径 |
| 74 | + */ |
| 75 | + me.setBaseUploadPath("upload"); |
| 76 | + me.setBaseDownloadPath("export"); |
| 77 | + me.setViewType(ViewType.FREE_MARKER); |
| 78 | + me.setError403View("/403.html"); |
| 79 | + // 404错误是web应用报出的,只能依靠web.xml里面来配置 |
| 80 | + me.setError404View("/404.html"); |
| 81 | + me.setError500View("/500.html"); |
| 82 | + } |
65 | 83 |
|
66 | | - /** |
67 | | - * 常量配置 |
68 | | - */ |
69 | | - @Override |
70 | | - public void configConstant(Constants me) { |
71 | | - Prop config = PropKit.use("config.properties"); |
72 | | - LOGGER.info("config.properties:{}",config); |
73 | | - me.setDevMode(config.getBoolean("app.devMode"));// 开发模式 |
74 | | - me.setEncoding("utf8"); |
75 | | - /** |
76 | | - * 该路径参数接受以"/"打头或者以 windows 磁盘盘符打头的绝对路径, |
77 | | - * 即可将基础路径指向项目根径之外,方便单机多实例部署。当该路径参数设置为相对路径时, 则是以项目根为基础的相对路径 |
78 | | - */ |
79 | | - me.setBaseUploadPath("upload"); |
80 | | - me.setBaseDownloadPath("export"); |
81 | | - me.setViewType(ViewType.FREE_MARKER); |
82 | | - me.setError403View("/403.html"); |
83 | | - // 404错误是web应用报出的,只能依靠web.xml里面来配置 |
84 | | - me.setError404View("/404.html"); |
85 | | - me.setError500View("/500.html"); |
86 | | - } |
| 84 | + @Override |
| 85 | + public void configRoute(Routes me) { |
| 86 | + // me.add("/", IndexController.class, "/"); // 第三个参数为该Controller的视图存放路径 |
| 87 | + // me.add("/blog", BlogController.class); // 第三个参数省略时默认与第一个参数值相同,在此即为 |
| 88 | + // "/blog" |
| 89 | + me.add(new FrontRoutes()); // 前端路由 |
| 90 | + me.add(new AdminRoutes()); // 后端路由 |
| 91 | + } |
87 | 92 |
|
88 | | - @Override |
89 | | - public void configRoute(Routes me) { |
90 | | - // me.add("/", IndexController.class, "/"); // 第三个参数为该Controller的视图存放路径 |
91 | | - // me.add("/blog", BlogController.class); // 第三个参数省略时默认与第一个参数值相同,在此即为 |
92 | | - // "/blog" |
93 | | - me.add(new FrontRoutes()); // 前端路由 |
94 | | - me.add(new AdminRoutes()); // 后端路由 |
95 | | - } |
| 93 | + @Override |
| 94 | + public void configEngine(Engine engine) { |
| 95 | + log.info("jfinal engine config:{}", engine); |
| 96 | + } |
96 | 97 |
|
97 | | - @Override |
98 | | - public void configEngine(Engine engine) { |
99 | | - LOGGER.info("jfinal engine config:{}",engine); |
100 | | - } |
| 98 | + /** |
| 99 | + * 插件配置 |
| 100 | + */ |
| 101 | + @Override |
| 102 | + public void configPlugin(Plugins me) { |
| 103 | + loadPropertyFile("config.properties"); |
| 104 | + DruidPlugin druid = new DruidPlugin( |
| 105 | + getProperty("jdbc.jdbcUrl"), |
| 106 | + getProperty("jdbc.user"), |
| 107 | + getProperty("jdbc.password")); |
| 108 | + druid.setDriverClass(getProperty("jdbc.driver")); |
| 109 | + druid.addFilter(new StatFilter()); |
| 110 | + WallFilter wall = new WallFilter(); |
| 111 | + wall.setDbType(JdbcConstants.MYSQL); |
| 112 | + wall.setLogViolation(true); |
| 113 | + LogFilter logFilter = new Slf4jLogFilter(); |
| 114 | + logFilter.setConnectionLogEnabled(true); |
| 115 | + logFilter.setStatementLogEnabled(true); |
| 116 | + logFilter.setResultSetLogEnabled(true); |
| 117 | + logFilter.setStatementExecutableSqlLogEnable(true); |
| 118 | + druid.addFilter(wall); |
| 119 | + druid.addFilter(logFilter); |
| 120 | + me.add(druid); |
| 121 | + ActiveRecordPlugin arp = new ActiveRecordPlugin(druid); |
| 122 | + arp.setDevMode(true); |
| 123 | + arp.setShowSql(false); |
| 124 | + // 设置数据库大小写不敏感 |
| 125 | + arp.setContainerFactory(new CaseInsensitiveContainerFactory(true)); |
| 126 | + _MappingKit.mapping(arp); |
| 127 | + me.add(arp); |
| 128 | + // arp.addMapping("user", User.class); |
| 129 | + //EhCachePlugin |
| 130 | + log.info("ehcache.xml路径:{}", PathKit.getRootClassPath() + File.separator + "ehcache.xml"); |
| 131 | + me.add(new EhCachePlugin()); |
| 132 | + } |
101 | 133 |
|
102 | | - /** |
103 | | - * 插件配置 |
104 | | - */ |
105 | | - @Override |
106 | | - public void configPlugin(Plugins me) { |
107 | | - loadPropertyFile("config.properties"); |
108 | | - DruidPlugin druid = new DruidPlugin(getProperty("jdbc.jdbcUrl"), |
109 | | - getProperty("jdbc.user"), getProperty("jdbc.password")); |
110 | | - druid.setDriverClass(getProperty("jdbc.driver")); |
111 | | - druid.addFilter(new StatFilter()); |
112 | | - WallFilter wall = new WallFilter(); |
113 | | - wall.setDbType(JdbcConstants.MYSQL); |
114 | | - wall.setLogViolation(true); |
115 | | - LogFilter logFilter=new Slf4jLogFilter(); |
116 | | - logFilter.setConnectionLogEnabled(true); |
117 | | - logFilter.setStatementLogEnabled(true); |
118 | | - logFilter.setResultSetLogEnabled(true); |
119 | | - logFilter.setStatementExecutableSqlLogEnable(true); |
120 | | - druid.addFilter(wall); |
121 | | - druid.addFilter(logFilter); |
122 | | - me.add(druid); |
123 | | - ActiveRecordPlugin arp = new ActiveRecordPlugin(druid); |
124 | | - arp.setDevMode(true); |
125 | | - arp.setShowSql(false); |
126 | | - // 设置数据库大小写不敏感 |
127 | | - arp.setContainerFactory(new CaseInsensitiveContainerFactory(true)); |
128 | | - _MappingKit.mapping(arp); |
129 | | - me.add(arp); |
130 | | - // arp.addMapping("user", User.class); |
131 | | - //EhCachePlugin |
132 | | - LOGGER.info("ehcache.xml路径:{}",PathKit.getRootClassPath()+File.separator+"ehcache.xml"); |
133 | | - me.add(new EhCachePlugin()); |
134 | | - } |
135 | | - |
136 | | - /** |
137 | | - * 拦截器 ,当某个 Method 被多个级别的拦截器所拦截,拦截器各级别执行的次序依次为:Global、 |
138 | | - * Inject、Class、Method,如果同级中有多个拦截器,那么同级中的执行次序是:配置在前面的 先执行。 拦截器从上到下依次分为 |
139 | | - * Global、Inject、Class、Method 四个层次,Clear 用于清除自身 所处层次以上层的拦截器。 |
140 | | - */ |
141 | | - @Override |
142 | | - public void configInterceptor(Interceptors me) { |
143 | | - // 添加控制层全局拦截器 |
144 | | - me.addGlobalActionInterceptor(new AuthInterceptor()); |
145 | | - // 添加业务层全局拦截器 |
146 | | - // me.addGlobalServiceInterceptor(new TimeInterceptor()); |
147 | | - // 为兼容老版本保留的方法,功能与addGlobalActionInterceptor完全一样 |
148 | | - // me.add(new AuthInterceptor()); |
149 | | - /** |
150 | | - * 上例中的 TxByRegex 拦截器可通过传入正则表达式对 action 进行拦截,当 actionKey 被正 |
151 | | - * 则匹配上将开启事务。TxByActionKeys 可以对指定的 actionKey 进行拦截并开启事务, TxByMethods |
152 | | - * 可以对指定的 method 进行拦截并开启事务。 注意:MySql 数据库表必须设置为 InnoDB 引擎时才支持事务,MyISAM |
153 | | - * 并不支持事务 |
154 | | - */ |
155 | | - // me.add(new TxByMethodRegex("(.*save.*|.*update.*)")); me.add(new |
156 | | - // TxByMethods("save", "update")); |
157 | | - // me.add(new TxByActionKeyRegex("/trans.*")); me.add(new |
158 | | - // TxByActionKeys("/tx/save", "/tx/update")); |
| 134 | + /** |
| 135 | + * 拦截器 ,当某个 Method 被多个级别的拦截器所拦截,拦截器各级别执行的次序依次为:Global、 |
| 136 | + * Inject、Class、Method,如果同级中有多个拦截器,那么同级中的执行次序是:配置在前面的 先执行。 拦截器从上到下依次分为 |
| 137 | + * Global、Inject、Class、Method 四个层次,Clear 用于清除自身 所处层次以上层的拦截器。 |
| 138 | + */ |
| 139 | + @Override |
| 140 | + public void configInterceptor(Interceptors me) { |
| 141 | + // 添加控制层全局拦截器 |
| 142 | + me.addGlobalActionInterceptor(new AuthInterceptor()); |
| 143 | + // 添加业务层全局拦截器 |
| 144 | + // me.addGlobalServiceInterceptor(new TimeInterceptor()); |
| 145 | + // 为兼容老版本保留的方法,功能与addGlobalActionInterceptor完全一样 |
| 146 | + // me.add(new AuthInterceptor()); |
| 147 | + /** |
| 148 | + * 上例中的 TxByRegex 拦截器可通过传入正则表达式对 action 进行拦截,当 actionKey 被正 |
| 149 | + * 则匹配上将开启事务。TxByActionKeys 可以对指定的 actionKey 进行拦截并开启事务, TxByMethods |
| 150 | + * 可以对指定的 method 进行拦截并开启事务。 注意:MySql 数据库表必须设置为 InnoDB 引擎时才支持事务,MyISAM |
| 151 | + * 并不支持事务 |
| 152 | + */ |
| 153 | + // me.add(new TxByMethodRegex("(.*save.*|.*update.*)")); me.add(new |
| 154 | + // TxByMethods("save", "update")); |
| 155 | + // me.add(new TxByActionKeyRegex("/trans.*")); me.add(new |
| 156 | + // TxByActionKeys("/tx/save", "/tx/update")); |
159 | 157 |
|
160 | | -} |
| 158 | +} |
161 | 159 |
|
162 | | -/** |
163 | | - * ,Handler 可以接管所有 web 请求,并对应用拥有完全的控制权,可以很方便地实现更高层的功能性扩 展 |
164 | | - */ |
165 | | -@Override |
166 | | -public void configHandler(Handlers me) { |
167 | | -// me.add(new ResourceHandler()); |
168 | | -// 添加druid监控 |
169 | | -DruidStatViewHandler dvh = new DruidStatViewHandler("/druid", |
170 | | -new IDruidStatViewAuth() { |
171 | | -public boolean isPermitted(HttpServletRequest request) { |
172 | | -// HttpSession hs = request.getSession(false); |
173 | | -// return (hs != null && hs.getAttribute("admin") != |
174 | | -// null); |
175 | | -return true; |
176 | | -} |
177 | | -}); |
178 | | -me.add(dvh); |
179 | | -} |
| 160 | +/** |
| 161 | + * ,Handler 可以接管所有 web 请求,并对应用拥有完全的控制权,可以很方便地实现更高层的功能性扩 展 |
| 162 | + */ |
| 163 | +@Override |
| 164 | +public void configHandler(Handlers me) { |
| 165 | +// me.add(new ResourceHandler()); |
| 166 | +// 添加druid监控 |
| 167 | +DruidStatViewHandler dvh = new DruidStatViewHandler("/druid", |
| 168 | +new IDruidStatViewAuth() { |
| 169 | +public boolean isPermitted(HttpServletRequest request) { |
| 170 | +// HttpSession hs = request.getSession(false); |
| 171 | +// return (hs != null && hs.getAttribute("admin") != |
| 172 | +// null); |
| 173 | +return true; |
| 174 | +} |
| 175 | +}); |
| 176 | +me.add(dvh); |
| 177 | +} |
180 | 178 | }
|
0 commit comments