https://github.com/Dreampie/jfinal-web jfinal-web是一款jfinal下的web插件:
<dependency>
<groupId>cn.dreampie</groupId>
<artifactId>jfinal-web</artifactId>
<version>${jfinal-web.version}</version>
</dependency>
目前刚刚发布第一个版本0.1:
<jfinal-web.version>0.1</jfinal-web.version>
1.使用Cache+CacheRemove 过滤器管理你的缓存
public void configInterceptor(Interceptors interceptors) {
//开发时不用开启 避免不能试试看到数据效果
interceptors.add(new CacheRemoveInterceptor());
interceptors.add(new CacheInterceptor());
}
在controller里使用
使用缓存,在方法前加上注解,并设置缓存名称
@CacheName(AppConstants.DEFAULT_CACHENAME)//AppConstants.DEFAULT_CACHENAME 是个字符串 表示缓存的name 和配置文件对应 如 "defaultCache"
public void query() {
//数据查询
}
@CacheName(AppConstants.DEFAULT_CACHENAME)
public void search() {
//数据查询
}
如果在使用增删改时需要移除search方法的缓存
@CacheRemove(name = AppConstants.DEFAULT_CACHENAME,keys = {"search"})//keys表示方法名称,可以多个,如果为空会移除该controller下所以cachename为default名字的缓存
public void save() {
//数据保存
}
2.使用基础Model简化sql操作
@TableBind(tableName = "sec_token", pkName = "uuid")
public class Token extends cn.dreampie.web.model.Model<Token> {
public static Token dao = new Token();
}
继承cn.dreampie.web.model.Model
public List<M> findAll() {
return find(getSelectSql() + getExceptSelectSql());
}
public List<M> findBy(String where, Object... paras) {
return find(getSelectSql() + getExceptSelectSql() + getWhere(where), paras);
}
public List<M> findTopBy(int topNumber, String where, Object... paras) {
return paginate(1, topNumber, getSelectSql(), getExceptSelectSql() + getWhere(where), paras).getList();
}
public M findFirstBy(String where, Object... paras) {
return findFirst(getSelectSql() + getExceptSelectSql() + getWhere(where), paras);
}
public Page<M> paginateAll(int pageNumber, int pageSize) {
return paginate(pageNumber, pageSize, getSelectSql(), getExceptSelectSql());
}
public Page<M> paginateBy(int pageNumber, int pageSize, String where, Object... paras) {
return paginate(pageNumber, pageSize, getSelectSql(), getExceptSelectSql() + getWhere(where), paras);
}
public boolean updateAll(String set, Object... paras) {
return Db.update(getUpdateSql() + getSet(set), paras) > 0;
}
public boolean updateBy(String set, String where, Object... paras) {
return Db.update(getUpdateSql() + getSet(set) + getWhere(where), paras) > 0;
}
public boolean deleteAll() {
return Db.update(getDeleteSql()) > 0;
}
public boolean deleteBy(String where, Object... paras) {
return Db.update(getDeleteSql() + getWhere(where), paras) > 0;
}
public boolean dropAll() {
return Db.update(getDropSql()) > 0;
}
public boolean dropBy(String where, Object... paras) {
return Db.update(getDropSql() + getWhere(where), paras) > 0;
}
public long countAll() {
long result = Db.queryFirst(getCountSql());
return result;
}
public long countBy(String where, Object... paras) {
long result = Db.queryFirst(getCountSql() + getWhere(where), paras);
return result;
}
可以解决大多数情况下的基础性sql操作,不用编写任何查询方法
也可以和sqlinxml结合使用
public List<Role> findUserBy(String where, Object... paras) {
List<Role> result = find(getSelectSql() + SqlKit.sql("role.findUserByExceptSelect") + blank + getWhere(where), paras);
return result;
}
3.使用GzipFilter和CacheFilter来压缩和缓存web数据
<!--gzip compress filter-->
<filter>
<filter-name>gzipFilter</filter-name>
<filter-class>cn.dreampie.web.filter.gzip.GZIPFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>gzipFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<!--gzip compress filter-->
<!--cache filter-->
<filter>
<filter-name>cacheFilter</filter-name>
<filter-class>cn.dreampie.web.filter.cache.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cacheFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<!--cache filter-->
4.使用其他过滤器
handlers.add(new AccessDeniedHandler("/**/*.ftl")); //拒绝访问某些文件
handlers.add(new ResourceHandler("/javascript/**", "/images/**", "/css/**", "/lib/**", "/**/*.html"));//对静态文件目录放行
handlers.add(new SkipHandler("/im/**"));//跳过某些全球路径
//防xss攻击
handlers.add(new AttackHandler());
5.ajax请求,返回json的错误信息 jfinal configConstant
constants.setErrorRenderFactory(new JsonErrorRenderFactory());
6.websocket,jfinal使用websocket需要避免jfinalFilter过滤websocket路径
handlers.add(new SkipHandler("/im/**"));//跳过该目录
//连接websocket
WebSocketSrv.connect("ws://localhost:9090/im/" + user.id)
//服务器向客户端发送消息
MessageServer.send(new Message(authorid,receiverId,"message"));
7.AutoBindRoutes,自动根据注解和约定的规则生成访问url路由
/**
* 配置路由
*/
public void configRoute(Routes routes) {
this.routes = routes;
AutoBindRoutes autoBindRoutes = new AutoBindRoutes();
routes.add(autoBindRoutes);
}
//默认规则是AdminController的ControllerKey是/admin,如果你不想使用默认规则 ,controller使用注解@ControllerKey(第一个参数 value 是指访问某个 Controller 所需要的一个controllerKey字符串,该字符串唯一对应一个 Controller,value 仅能定位到 Controller。第二个参数 controllerClass 是该controllerKey 所对应到的 Controller。第三个参数 path 是指该 Controller 返回的视图的相对路径(该参数具体细节将在 Controller相关章节中给出)。当 view 未指定时默认值为controllerKey的value。)
@ControllerKey(value = "/x", path = "/view/")
public class AdminController extends Controller {
}
demo:https://github.com/Dreampie/icedog
https://github.com/Dreampie?tab=repositories 目录下有多款插件:
cn.dreampie.jfinal-shiro https://github.com/Dreampie/jfinal-shiro shiro插件
cn.dreampie.jfinal-shiro-freemarker https://github.com/Dreampie/jfinal-shiro-freemarker shiro插件实现的freemarker标签库
cn.dreampie.jfinal-web https://github.com/Dreampie/jfinal-web 相关web插件,简洁model实现
cn.dreampie.jfinal-utils https://github.com/Dreampie/jfinal-utils 部分jfinal工具
cn.dreampie.jfinal-tablebind https://github.com/Dreampie/jfinal-tablebind jfinal的table自动绑定插件,支持多数据源
cn.dreampie.jfinal-flyway https://github.com/Dreampie/jfinal-flyway 数据库脚本升级插件,开发中升级应用时,使用脚本同步升级数据库或者回滚
cn.dreampie.jfinal-captcha https://github.com/Dreampie/jfinal-captcha 基于jfinal render的超简单验证吗插件
cn.dreampie.jfinal-quartz https://github.com/Dreampie/jfinal-quartz 基于jfinal 的quartz管理器
cn.dreampie.jfinal-sqlinxml https://github.com/Dreampie/jfinal-sqlinxml 基于jfinal 的类似ibatis的sql语句管理方案
cn.dreampie.jfinal-lesscss https://github.com/Dreampie/jfinal-lesscss java实现的lesscsss实时编译插件,可以由于jfinal
cn.dreampie.jfinal-coffeescript https://github.com/Dreampie/jfinal-coffeescript java实现的coffeescript实时编译插件,可以由于jfinal
cn.dreampie.jfinal-akka https://github.com/Dreampie/jfinal-akka java使用akka执行异步任务
cn.dreampie.jfinal-mailer https://github.com/Dreampie/jfinal-mailer 使用akka发布邮件的jfinal插件
cn.dreampie.jfinal-slf4j https://github.com/Dreampie/jfinal-slf4j 让jfinal使用slf4j的日志api
部分内容借鉴了网络资料