zfskkk的博客

平常的学习和总结

JFinal入门教程

JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。

1、配置pom文件,JFinal需要添加对应的依赖

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>

    <name>jfinal-demo</name>
    <groupId>com.zfskkk</groupId>
    <artifactId>jfinal-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    </properties>

    <!-- 使用阿里 maven 库 -->
    <repositories>
        <repository>
            <id>ali-maven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jetty-server</artifactId>
            <version>8.1.8</version>
            <!--
                此处的 scope 值为 compile 仅为支持 IDEA 下启动项目
                打 war 包时需要改成 provided,以免将一些无用的 jar 打进去
            -->
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal</artifactId>
            <version>3.3</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>jfinal_demo_for_maven</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.11.v20130520</version>
                <configuration>
                    <stopKey>stop</stopKey>
                    <stopPort>5599</stopPort>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>80</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2、JFinal需要有一个配置文件,所有的配置信息都可以在该文件中进行配置,该配置文件需要继承JFinalConfig

package com.zfskkk;

import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

public class MyConfig extends JFinalConfig {

    public void configConstant(Constants constants) {
        constants.setDevMode(true);
        constants.setEncoding("UTF-8");
        constants.setViewType(ViewType.JSP);
    }

    public void configRoute(Routes routes) {
    }

    public void configEngine(Engine engine) {

    }

    public void configPlugin(Plugins plugins) {

    }

    public void configInterceptor(Interceptors interceptors) {
    }

    public void configHandler(Handlers handlers) {
    }

    // 系统启动完成后回调
    public void afterJFinalStart() {
    }

    // 系统关闭之前回调
    public void beforeJFinalStop() {
    }

}

3、在web.xml文件中配置过滤器,web项目启动后需要根据这个过滤器来找到配置文件读取相关配置信息

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
        <filter-name>jfinal</filter-name>
        <filter-class>com.jfinal.core.JFinalFilter</filter-class>
        <init-param>
            <param-name>configClass</param-name>
            <param-value>com.zfskkk.MyConfig</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>jfinal</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

4、启动,启动的main方法不一定需要在Controller里面,比如在MyConfig 配置文件中也是可以的

package com.zfskkk;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;

public class HelloController extends Controller {
    public static void main(String[] args) {
        // 第二个参数为端口
        JFinal.start("src/main/webapp", 80, "/");
    }
}

如下图所示说明启动成功,
这里写图片描述

5、创建Controller

package com.zfskkk;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;

public class MyController extends Controller {
    public void index() {
        renderText("hello world ,this is my first controller");
    }

    public void hello() {
        renderText("this is hello method");
    }

    @ActionKey("/my/test1/key")
    public void testKey() {
        renderText("/my/test1/key");
    }
}

自己创建的Controller必须要继承JFinal的Controller,然后在MyConfig 配置文件中添加路由配置,最后重启访问http://localhost/hello

    public void configRoute(Routes routes) {
        routes.add("/hello", MyController.class);
    }

这里写图片描述

阅读更多
文章标签: JFinal
个人分类: JFinal
上一篇jfinal解决ios的post提交接受不了参数问题
下一篇重放攻击的概念
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭