JFinal 是基于 Java 语言的极速 WEB + ORM 开发框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。在拥有Java 语言所有优势的同时再拥有 ruby、python、php 等动态语言的开发效率!
1:在项目pom.xml 文件添加相关依赖
<!-- jfinal 依赖的jar -->
<dependencies>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.15</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<!-- 数据库连接依赖包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
2:编写实体类,继承 model 抽象类
import com.jfinal.plugin.activerecord.Model;
public class User extends Model<User>{
}
3:编写config 配置文件
package conf;
import model.User;
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.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import controller.LoadController;
import controller.UserController;
public class DemoConf extends JFinalConfig{
@Override
public void configConstant(Constants me) {
// TODO Auto-generated method stub
//设置页面的显示类型为jsp
me.setViewType(ViewType.JSP);
}
@Override
public void configHandler(Handlers arg0) {
// TODO Auto-generated method stub
}
@Override
public void configInterceptor(Interceptors arg0) {
// TODO Auto-generated method stub
}
@Override
public void configPlugin(Plugins me) {
// TODO Auto-generated method stub
try {
DruidPlugin dp = new DruidPlugin(
"jdbc:mysql://hadoop1:3306/test", "root",
"root");
me.add(dp);
ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
me.add(arp);
//实体类与数据库表映射
arp.addMapping("user", "id", User.class);
System.out.println("success");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("失败");
}
}
//设置路由
@Override
public void configRoute(Routes me) {
// TODO Auto-generated method stub
//me.add("/", UserController.class);
me.add("/user", UserController.class);
}
}
4:编写控制器类
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.Restful;
public class UserController extends Controller {
public void index(){
render("login.jsp");
}
User userdao = new User();
public void login() {
System.out.println("数据库操作");
User us = getModel(User.class);
List<User> userlist = userdao.find(
"select *from user where username=? and password=?",
us.getStr("username"), us.getStr("password"));
if(userlist.size()==0){
redirect("/index.jsp");
}else{
redirect("/index1.jsp");
}
//return user;
}
5: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>conf.DemoConf</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5:页面action 配置
<!-- action 走的是user 这个路由对应的控制器的login() 方法 -->
<div class="loginBox" >
<div class="login">
<h1 class="mg-t30">用户登录</h1>
<form action="/user/login" method="post">
<input name="user.username" type="text">
<input name="user.password" type="text">
<input type="submit" value="登录">
</form>
</div>
</div>