Jfinal 学习(一)

岳凯康
2023-12-01

Jfinal

标签:小本教程仅限小白,大神自行绕过。

1、准备工作

2、eclipse maven配置

  • eclipse4.73a(这里说下个人建议使用eclipse-mars2)下我们创建maven project,填写pom依赖并保存,稍后就能看到Maven Dependencies我们引入的所有jar。
  • 写入一下pom.xml,这个pom是Jfinal_1.5官网的我直接拿过来方便新手手忙脚乱。构建时间随本地网络会有延迟,希望大家耐心等待
<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>
	<groupId>com.jfinal</groupId>
	<artifactId>club</artifactId>
	<packaging>war</packaging>
	<version>1.5</version>
	<name>jfinal-club Maven Webapp</name>
	<url>http://jfinal.com/club</url>
	
	<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>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<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.4</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.29</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.31</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.44</version>
		</dependency>
		<dependency>
			<groupId>com.jfinal</groupId>
			<artifactId>cos</artifactId>
			<version>2017.5</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.6.11</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.9</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-email</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.9.2</version>
		</dependency>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>2.9.3</version>
		</dependency>
		<dependency>
			<groupId>it.sauronsoftware.cron4j</groupId>
			<artifactId>cron4j</artifactId>
			<version>2.2.5</version>
		</dependency>
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>javase</artifactId>
			<version>3.2.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.jfinal/jfinal-ext -->
		<dependency>
			<groupId>com.jfinal</groupId>
			<artifactId>jfinal-ext</artifactId>
			<version>3.1.4</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>jfinal-club</finalName>
		<plugins>
		
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
					<!-- java8 保留参数名编译参数 -->
					<!-- compilerArgument>-parameters</compilerArgument -->
					<compilerArguments>
						<verbose />
						<!-- 注意下面配置在 Mac OS、Linux 系统下,分号要改成冒号。分号支持 windows 系统 -->
						<!-- ImageKit 中已移除了对 com.sun.image.codec.jpeg.JPEGCodec 的依赖
							可以删掉下面的配置了,参考资料:
							https://blog.csdn.net/ppvqq/article/details/48269661 -->
						<!-- bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath -->
					</compilerArguments>
				</configuration>
			</plugin>
			
			<!-- 安装源码到本地仓库 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<version>2.1.2</version>
				<executions>
					<execution>
						<id>attach-sources</id>
						<phase>verify</phase>
						<goals>
							<goal>jar-no-fork</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			
			<!-- plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>8.1.8.v20121106</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>
  • 构建成功如下所示
  • 1. 项目树分布
  • 2、这里有一点注意,我们需要单独新建一个source folder src/main/java
  • 3、项目右键new-source folder
    若提示已存在,我们需要多一步这个操作。
  • 4、项目右键properties 左边build path 对应左边source下选择你重复的那两个路径 一般是src/main/java和src/main/test。
  • 5、我们在src/main/java下新建一个package,名称自定义,并在此包下选择new-class名称为MyConfig
  • 6、myconfig做作如下几步
  • 配置我们的web.xml,同理我们拿到jfinal1.5的xml文件,这些都是必须得。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>jfinal offical website</display-name>
    <filter>
        <filter-name>jfinal</filter-name>
        <filter-class>com.jfinal.core.JFinalFilter</filter-class>
        <init-param>
            <param-name>configClass</param-name>
            <param-value>com.jfinal.club.common.JFinalClubConfig</param-value>
        </init-param>
    </filter>

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

小贴士:这里必须是你刚才哪个myconfig完整的包名,有个小窍门去检查你配置的是不是正确,如果按住鼠标左键可以点进去就是正确的路径。

3、在myconfig编写启动main方法 和设置关系映射Controller

4、接下来我们建包写一个IndexController继承Controller,使用默认的模板引擎renderTemplate,然后新建一个html,run myconfig就可以成功启动了

如若有什么请关注作者私信我。

 类似资料: