JFinal framework learning: integrate the front-end framework of bootstrap to realize simple addition, deletion, modification and query functions

Posted by wempy on Sat, 04 Jan 2020 17:56:55 +0100

1, JFinal integrates bootstrap

1. Download bootstrap package: download address: https://v3.bootcss.com/

2. Put the jQuery package under the js package of bootstrap, and then import the bootstrap package into the webapp of the project.

2, Realize the function of adding, deleting, modifying and checking

1. In the resources folder, create a new file a small config.txt to configure the database.

2. Configure the database connection in commonConfig.java

package com.config;

import com.controller.BlogController;
import com.controller.IndexController;
import com.controller.UserController;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import java.awt.image.IndexColorModel;

public class commonConfig extends JFinalConfig{
    public commonConfig() {

     * Const constant
     * @param constants
    public void configConstant(Constants constants) {
        //Configure view layer, use freemarker type

    public void configRoute(Routes routes) {
        routes.add("/", IndexController.class);

    public void configEngine(Engine engine) {


    //DruidPlugin is the data source plug-in, and ActiveRecordPlugin is the ActiveRecord support plug-in
    public void configPlugin(Plugins plugins) {
        DruidPlugin druidPlugin=creatDruidPlugins();
        ActiveRecordPlugin arp=new ActiveRecordPlugin(druidPlugin);

        //The mapping from database table to Model is established

    public void configInterceptor(Interceptors interceptors) {


    public void configHandler(Handlers handlers) {


     * Get data connection pool
     * @return
    public static DruidPlugin creatDruidPlugins(){
        return new DruidPlugin(PropKit.get("jdbcUrl"),PropKit.get("user"),PropKit.get("password").trim());

    public static void main(String[] args){


3. Create a new MappingKit.java class to map the database tables

Note: when the primary key of the database is named as id, the second parameter in arp.Mapping() can be left blank, and JFinal default id is the primary key.

If the primary key name of the database table is not id, the second parameter needs to be the primary key name.


4. Create a new Blog.java class in the model

package com.model;

import com.jfinal.plugin.activerecord.Model;

public class Blog extends Model<Blog>{



5. Create a new BlogService.java class in service, in which SQL and business logic are written.

package com.service;

import com.model.Blog;
import java.util.List;

public class BlogService {

    private Blog blog=new Blog().dao();

    public Blog queryById(int id){
        return blog.findById(id);

    public List<Blog> queryList(){
        List<Blog> blogList=blog.find("select * from Blog");
        return blogList;

    public void delectById(int id){



6. Create a new BlogController.java Controller in the Controller layer,

package com.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.model.Blog;
import com.service.BlogService;
import com.sun.prism.impl.Disposer;

import java.util.List;

public class BlogController extends Controller{

    private static final BlogService blogService=new BlogService();

    public void index(){

     * Add blog
    public void saveBlog(){
        Blog blog=getModel(Blog.class);

     * Find all blog s
    public void queryAllBlog(){
        List<Blog> blogs=blogService.queryList();

     * delete
    public void deleteBlog(){

     * Modify blog
    public void updateBlog(){

     * Modify blog
    public void edit(){


7. Write the front page below

Bootstrap is a front-end framework for rapid development of Web applications and websites. So after importing the bootstrap package, we can directly reference it.

Create a new BlogList.html under the blog package:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <mate naem="Viewport" content="width=drive-width, user-scalable=no,initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0"></mate>
    <link rel="stylesheet" href="/bootstrap/css/bootstrap.css">
<script type="text/javascript" src="/bootstrap/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script>

<ul class="nav nav-pills">
    <li role="presentation" class="active"><a href="/blog/queryAllBlog">Home</a></li>
    <li role="presentation"><a href="/blog/addBlog.html">AddBlog</a></li>
    <li role="presentation"><a href="#">Messages</a></li>

<div class="container">
    <table class="table table-hover">
        <#list blogs as blog>
                    <a href="/blog/deleteBlog/#{blog. ID} "> delete</a>
                    <a href="/blog/edit/#{blog. ID} "> Modify</a>


Then, let's run our project on tomcat:

Query function completed!

Topics: Java Database JQuery Javascript