JFinal教程


自學JFinal總結

前言:每次搭建ssm框架時,就像搬家一樣,非常繁雜,並且還容易出錯。正好了解到JFinal極簡,無需配置即可使用,在這里記錄下學習的過程。

感謝:非常感謝此網站發布的教程,非常詳細,有興趣的可以多看看,手把手教程了。。。。。https://www.jfinal.com/doc/1-3

開始正文吧!

一、搭建JFinal項目:

  1.創建一個maven工程,選擇An archetype which contains a sample Maven Webapp project。

2.pom文件加入依賴的jar包:

        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal-undertow</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal</artifactId>
            <version>4.3</version>
        </dependency>

3.加入指定jdk1.8的plugin,以下plugin需要放在<build><plugins>下(主要提醒別放錯了,看不明白就百度一下)

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

 4.創建配置類(無需改動)

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

public class DemoConfig extends JFinalConfig {

    public static void main(String[] args) {
        UndertowServer.start(DemoConfig.class, 80, true);
    }

    public void configConstant(Constants me) {
        me.setDevMode(true);
    }

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

    public void configEngine(Engine me) {
    }

    public void configPlugin(Plugins me) {
    }

    public void configInterceptor(Interceptors me) {
    }

    public void configHandler(Handlers me) {
    }
}

5.生成一個controller(無需改動)

import com.jfinal.core.Controller;

public class HelloController extends Controller {
    public void index() {
       renderText("Hello JFinal World.");
    }
}

6.運行main方法,直接訪問127.0.0.1/hello 即可看到效果。至此簡易JFinal已經搭建完畢,非常簡單快捷!!!!!!

二、打包為jar包或生成快捷啟動方式

1.JFinal默認使用jetty,無需使用tomcat!!!上一步運行main方法時即是運行在容器內了!!

2.加入pom文件有關maven打包jar的配置(無需改動)

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <excludes>
                        <exclude>*.txt</exclude>
                        <exclude>*.xml</exclude>
                        <exclude>*.properties</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <!-- 打包生成的文件名 -->
                            <finalName>${project.artifactId}</finalName>
                            <!-- jar 等壓縮文件在被打包進入 zip、tar.gz 時是否壓縮,設置為 false 可加快打包速度 -->
                            <recompressZippedFiles>false</recompressZippedFiles>
                            <!-- 打包生成的文件是否要追加 release.xml 中定義的 id 值 -->
                            <appendAssemblyId>true</appendAssemblyId>
                            <!-- 指向打包描述文件 package.xml -->
                            <descriptors>
                                <descriptor>package.xml</descriptor>
                            </descriptors>
                            <!-- 打包結果輸出的基礎目錄 -->
                            <outputDirectory>${project.build.directory}/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

3.在項目根目錄下創建package.xml(與2的紅色字體一致),根目錄即為項目一級目錄,項目名下的目錄(如JFinalTest項目,根目錄即為JFinalTest/)。

tip:此處需要注意,我的配置文件是windows下的,所以打包格式是zip(紅色字體處),如果是linux下,將zip修改為tar.gz

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <!-- assembly 打包配置更多配置可參考官司方文檔: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html -->
    <id>release</id>
    <!-- 設置打包格式,可同時設置多種格式,常用格式有:dir、zip、tar、tar.gz dir 格式便於在本地測試打包結果 zip 格式便於 
        windows 系統下解壓運行 tar、tar.gz 格式便於 linux 系統下解壓運行 -->
    <formats>
        <format>zip</format>
        <!-- <format>tar.gz</format> -->
    </formats>
    <!-- 打 zip 設置為 true 時,會在 zip 包中生成一個根目錄,打 dir 時設置為 false 少層目錄 -->
    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>
        <!-- src/main/resources 全部 copy 到 config 目錄下 -->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
        </fileSet>
        <!-- src/main/webapp 全部 copy 到 webapp 目錄下 -->
        <fileSet>
            <directory>${basedir}/src/main/webapp</directory>
            <outputDirectory>webapp</outputDirectory>
        </fileSet>

        <!-- 項目根下面的腳本文件 copy 到根目錄下 -->
        <fileSet>
            <directory>${basedir}</directory>
            <outputDirectory></outputDirectory>
            <!-- 腳本文件在 linux 下的權限設為 755,無需 chmod 可直接運行 -->
            <fileMode>755</fileMode>
            <includes>
                <include>*.sh</include>
                <include>*.bat</include>
            </includes>
        </fileSet>
    </fileSets>
    <!-- 依賴的 jar 包 copy 到 lib 目錄下 -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

4.我是在eclipse下開發,首先maven update,然后執行clean,最后執行package(也可以直接去項目目錄下cmd,mvn clean package),最終會在target目錄下生成zip包和一個jar文件。

5.生成start.bat(或者start.sh)文件!只需要將下面的批處理程序的紅色字體改為自己的main類即可。注意,這是windows下的bat文件。

使用方式將生成的zip包解壓后,將.bat程序放到一級目錄下(如JFinalTest項目,.bat將放到JFinalTest/下),然后在cmd運行start.bat start

@echo off

rem -------------------------------------------------------------------------
rem
rem 使用說明:
rem
rem 1: 該腳本用於別的項目時只需要修改 MAIN_CLASS 即可運行
rem
rem 2: JAVA_OPTS 可通過 -D 傳入 undertow.port 與 undertow.host 這類參數覆蓋
rem    配置文件中的相同值此外還有 undertow.resourcePath, undertow.ioThreads
rem    undertow.workerThreads 共五個參數可通過 -D 進行傳入
rem
rem 3: JAVA_OPTS 可傳入標准的 java 命令行參數,例如 -Xms256m -Xmx1024m 這類常用參數
rem
rem
rem -------------------------------------------------------------------------

setlocal & pushd


rem 啟動入口類,該腳本文件用於別的項目時要改這里
set MAIN_CLASS=com.chx.test.DemoConfig

rem Java 命令行參數,根據需要開啟下面的配置,改成自己需要的,注意等號前后不能有空格
rem set "JAVA_OPTS=-Xms256m -Xmx1024m -Dundertow.port=80 -Dundertow.host=0.0.0.0"
rem set "JAVA_OPTS=-Dundertow.port=80 -Dundertow.host=0.0.0.0"


if "%1"=="start" goto normal
if "%1"=="stop" goto normal
if "%1"=="restart" goto normal

goto error


:error
echo Usage: jfinal.bat start | stop | restart
goto :eof


:normal
if "%1"=="start" goto start
if "%1"=="stop" goto stop
if "%1"=="restart" goto restart
goto :eof


:start
set APP_BASE_PATH=%~dp0
set CP=%APP_BASE_PATH%config;%APP_BASE_PATH%lib\*
echo starting jfinal undertow
java -Xverify:none %JAVA_OPTS% -cp %CP% %MAIN_CLASS%
goto :eof


:stop
set "PATH=%JAVA_HOME%\bin;%PATH%"
echo stopping jfinal undertow
for /f "tokens=1" %%i in ('jps -l ^| find "%MAIN_CLASS%"') do ( taskkill /F /PID %%i )
goto :eof


:restart
call :stop
call :start
goto :eof

endlocal & popd
pause

linux下的sh文件:將下面的紅色字體改為自己的main類。文件放置的位置和上面相同,執行./start.sh start

#!/bin/bash
# ----------------------------------------------------------------------
# name:         jfinal.sh
# version:      1.0
# author:       yangfuhai
# email:        fuhai999@gmail.com
#
# 使用說明:
# 1: 該腳本使用前需要首先修改 MAIN_CLASS 值,使其指向實際的啟動類
#
# 2:使用命令行 ./jfinal.sh start | stop | restart 可啟動/關閉/重啟項目  
#
# 3: JAVA_OPTS 可通過 -D 傳入 undertow.port 與 undertow.host 這類參數覆蓋
#    配置文件中的相同值此外還有 undertow.resourcePath、undertow.ioThreads、
#    undertow.workerThreads 共五個參數可通過 -D 進行傳入,該功能盡可能減少了
#    修改 undertow 配置文件的必要性
#
# 4: JAVA_OPTS 可傳入標准的 java 命令行參數,例如 -Xms256m -Xmx1024m 這類常用參數
#
# 5: 函數 start() 給出了 4 種啟動項目的命令行,根據注釋中的提示自行選擇合適的方式
#
# ----------------------------------------------------------------------

# 啟動入口類,該腳本文件用於別的項目時要改這里
MAIN_CLASS=com.yourpackage.YourMainClass

if [[ "$MAIN_CLASS" == "com.yourpackage.YourMainClass" ]]; then
    echo "請先修改 MAIN_CLASS 的值為你自己項目啟動Class,然后再執行此腳本。"
    exit 0
fi

COMMAND="$1"

if [[ "$COMMAND" != "start" ]] && [[ "$COMMAND" != "stop" ]] && [[ "$COMMAND" != "restart" ]]; then
    echo "Usage: $0 start | stop | restart"
    exit 0
fi


# Java 命令行參數,根據需要開啟下面的配置,改成自己需要的,注意等號前后不能有空格
# JAVA_OPTS="-Xms256m -Xmx1024m -Dundertow.port=80 -Dundertow.host=0.0.0.0"
# JAVA_OPTS="-Dundertow.port=80 -Dundertow.host=0.0.0.0"

# 生成 class path 值
APP_BASE_PATH=$(cd `dirname $0`; pwd)
CP=${APP_BASE_PATH}/config:${APP_BASE_PATH}/lib/*

function start()
{
    # 運行為后台進程,並在控制台輸出信息
    java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} &

    # 運行為后台進程,並且不在控制台輸出信息
    # nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} >/dev/null 2>&1 &

    # 運行為后台進程,並且將信息輸出到 output.log 文件
    # nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} > output.log &

    # 運行為非后台進程,多用於開發階段,快捷鍵 ctrl + c 可停止服務
    # java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS}
}

function stop()
{
    # 支持集群部署
    kill `pgrep -f ${APP_BASE_PATH}` 2>/dev/null
    
    # kill 命令不使用 -9 參數時,會回調 onStop() 方法,確定不需要此回調建議使用 -9 參數
    # kill `pgrep -f ${MAIN_CLASS}` 2>/dev/null

    # 以下代碼與上述代碼等價
    # kill $(pgrep -f ${MAIN_CLASS}) 2>/dev/null
}

if [[ "$COMMAND" == "start" ]]; then
    start
elif [[ "$COMMAND" == "stop" ]]; then
    stop
else
    stop
    start
fi

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM