JFinal+MySql实现登录注册功能

JFinal+MySql实现登录注册功能

JFinal 是基于Java 语言的极速 web 开发框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。在拥有Java语言所有优势的同时再拥有ruby、python等动态语言的开发效率。

本实例用JFinal框架结合MySql实现用户注册登录功能。

项目结构:JFinal+MySql实现登录注册功能_第1张图片

需要的jar包: jfinal-2.2-bin-with-src.jar、c3p0-0.9.5.1.jar、mchange-commons-java-0.2.10.jar、mysql-connector-java-5.1.20-bin.jar包

配置类:

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.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.model.User;
import com.sun.jmx.snmp.UserAcl;
import com.web.IndexController;
import com.web.LoginContraler;
import com.web.RegisterController;

public class MainConfig extends JFinalConfig{

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false)); 
		me.setViewType(ViewType.JSP);
		
		
	}
 
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		me.add("/", IndexController.class);  
        me.add("/login", LoginContraler.class);  
        me.add("/register",RegisterController.class);

	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		
	}
	 public static C3p0Plugin createC3p0Plugin() {  
	        return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());  
	    }  
	      

	@Override
	public void configPlugin(Plugins me) {
		
		
		   //c3p0连接池插件  
        C3p0Plugin C3p0Plugin = createC3p0Plugin();  
        me.add(C3p0Plugin);  
        //数据库操作插件  
        ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);  
        me.add(arp);  
        //该方法建立了数据库表名到 Model 的映射关系,下面的示例为默认主键是id  
        arp.addMapping("user", User.class);  
    
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		
	}

}

在src目录下新建配置文件:config.properties,对连接的数据库进行配置:

jdbcUrl = jdbc:mysql://localhost:3306/mydata
user = root
password =1996112lin

实体类 User:

import java.util.List;

import com.jfinal.plugin.activerecord.Model;

public class User extends Model<User> {
	
	 public static final User dao = new User();  
	
	   
	      
	   /*查找用户,用于登录*/
	    public List findByNameAndPwd(String name, String password){  
	        return find("select * from user where name='"+name+"' and password='"+password+"'");  
	    }  

	    
	    
	    /*实现保存数据到数据库的功能*/
	    public void saveUser(String name,String password) {
			new User().set("name", name).set("password", password).save();
		}
	    
}

三个控制类

import com.jfinal.core.Controller;

public class IndexController extends Controller{
    public void index(){  
        
        render("login.jsp");  
    }  
}
import java.util.List;

import com.jfinal.core.Controller;
import com.model.User;

public class LoginContraler extends Controller{

	 
    public void index(){  
          
        List users = User.dao.findByNameAndPwd(getPara("name"),getPara("password"));  
          
        if (users.size() > 0) {  
            //找到用户  
            setSessionAttr("userInfo", users.get(0));  
            render("/OK.jsp");  
        } else {  
              
            render("/no.jsp");  
        }  
          
    }  
	
	
}
import com.jfinal.core.Controller;
import com.model.User;

public class RegisterController extends Controller{

	public void index() {
		String name = getPara("name");
		String password = getPara("password");
		User.dao.saveUser(name, password);
		render("/theOK.jsp");
	}
	
}

登录页面login.jsp:


    		
注册

注册页面register.jsp:

你可能感兴趣的:(Java学习)