JFinal使用

来源:互联网 发布:淘宝拍卖车可信吗 编辑:程序博客网 时间:2024/05/15 08:34

下面主要介绍JFinal的最简单的配置,包括路由映射,数据库配置和操作,返回结果。

1.在web.xml中做如下配置:

<filter>    <filter-name>jfinal</filter-name>    <filter-class>com.jfinal.core.JFinalFilter</filter-class>    <init-param>        <param-name>configClass</param-name>        <param-value>com.xxh.demo.DemoConfig</param-value>    </init-param></filter><filter-mapping>    <filter-name>jfinal</filter-name>    <url-pattern>/*</url-pattern></filter-mapping>

2.新建配置文件DemoConfig,其实这里主要注意的是数据库的配置即可,虽然路由也可以在这里配置,但是一般都使用注解了。

public class DemoConfig extends JFinalConfig {    @Override    public void configConstant(Constants constants) {        constants.setDevMode(true);    }    @Override    public void configRoute(Routes routes) {        routes.add("/hello", HelloController.class);        routes.add("/blog", BlogController.class);    }    @Override    public void configEngine(Engine engine) {//        engine.addSharedFunction("/_view/common/__layout.html");//        engine.addSharedFunction("/_view/common/_paginate.html");//        engine.addSharedFunction("/_view/common/__admin_layout.html");    }    @Override    public void configPlugin(Plugins plugins) {        loadPropertyFile("config.properties");        System.out.println(getProperty("jdbcUrl") + "..." + getProperty("username") + "..." + getProperty("password"));        C3p0Plugin cp = new C3p0Plugin(getProperty("jdbcUrl")                , getProperty("username"), getProperty("password"));        plugins.add(cp);        ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);        plugins.add(arp);        /**         * 必须加上这个配置,否则getModel的时候会抛出如下异常         * com.jfinal.plugin.activerecord.ActiveRecordException:         * The Table mapping of model:         * com.xxh.bean.Blog not exists or the ActiveRecordPlugin not start.         */        arp.addMapping("blog", Blog.class);    }    @Override    public void configInterceptor(Interceptors interceptors) {    }    @Override    public void configHandler(Handlers handlers) {    }}

3.注解路由配置和最简单的返回文本数据和页面

public class HelloController extends Controller {    public void index() {        renderText("Hello Jfinal world");    }    @ActionKey("/login")    public void login() {        renderText("login");    }    @ActionKey("/testRender")    public void testRender() {        render("testRender.html");    }}

4.对于数据库的操作过程如下:

定义Java Bean

public class Blog extends Model {

public static final Blog me = new Blog();

}

执行数据库的增删查改

下面方法中,返回json数据使用的是renderJson()方法,添加的时候需要new对象,其他使用静态公共对象

public class BlogController extends Controller {    @ActionKey("/insertTest")    public void insertTest() {        Blog blog = new Blog();        blog.set("title", "xxh").set("content", "hjee").save();        renderJson(blog.toJson());    }    @ActionKey("/deleteTest")    public void deleteTest() {        boolean flag = Blog.me.deleteById(1);        renderText(flag+"");    }    @ActionKey("/updateTest")    public void updateTest() {        boolean flag = Blog.me.findByIdLoadColumns(2, "id,title,content").set("title", "gggg").update();        renderText(flag+"");    }    @ActionKey("/selectTest")    public void selectTest() {        List<Blog> blog = Blog.me.find("select * from blog");        for (Blog b : blog) {            System.out.println(b.getInt("userId"));        }        renderJson(blog);    }    @ActionKey("/save")    public void save() {        String title = getPara("title");        String content = getPara("content");        System.out.println(title + "...." + content);        Blog blog = getModel(Blog.class, "");        System.out.println(blog.toJson());//        renderText(blog.toString() + "----" + title + "...." + content);        renderJson(blog.toJson());    }    @Before(LoginValidator.class)    @ActionKey("/testLogin")    public void testLogin() {        renderText("testLogin");    }}

上面代码还设计了一个验证器的东西,其实就是拦截器,用来验证字段的规则,不符合做相应的跳转之类的,定义如下:

public class LoginValidator extends Validator {    @Override    protected void validate(Controller controller) {        validateRequiredString("name", "nameMsg", "请输入用户名");        validateRequiredString("pass", "passMsg", "请输入密码");    }    @Override    protected void handleError(Controller controller) {        controller.keepPara("name");        controller.render("login.html");    }}
4 0
原创粉丝点击