数据库的配置Jfinal的demo已经在DemoConfig中详细写明
Controller相当于struts2的namespace,类的成员方法相当于Action
在JFinalConfig中配置Action与访问路径的映射关系,相当于struts.xml,注意路径以“/”开头
public void configRoute(Routes me) { me.add("/", CommonController.class); me.add("/user", UserController.class); }
注意:Controller默认执行index方法
以登录为例:
@Before(LoginValidator.class) public void login(){ String username = getPara("username"); String password = getPara("password"); String rememberme = getPara("rememberme"); User user = User.dao.getByUsernameAndPassword(username, password); if (user != null){ if(null != rememberme && rememberme.equals("on")){ String sysID = username + Const.SYS_ID_SEPARATOR + password; setCookie("sysID", sysID, 3600*24*30); setSessionAttr("user", user); } redirect("/pages/index.html"); }else{ setAttr("msg", "用户名或密码错误"); renderFreeMarker("/pages/login.html"); } }
注意getPara取前台表单内容,然后调用DAO层(我觉得是dao,pojo,model的混合体)查询方法
public class User extends Model<User> { public static final User dao = new User(); public User getByUsernameAndPassword(String username, String password){ password = getMD5(password.getBytes()); return dao.findFirst("select id, username, password from user where username=? and password=?", username, password); } }
别忘了将Model,也就是这里的User映射数据库名称,类似hibernate的hbm文件
arp.addMapping("user", User.class); //DemoConfig类中 映射user 表到 User模型,arp为ActiveRecordPlugin
1.上传文件
public void upFile() { PathKit pk = new PathKit(); String contextPath = pk.getWebRootPath(); String path = "\\download\\"; String pathUrl = contextPath + path; int maxSize = 200 * 1024 * 1024; //10M UploadFile upFile = getFile("upload_file", pathUrl, maxSize, "utf-8"); }
2.验证码
private static final String RANDOM_CODE_KEY = "1"; public void login() { String inputRandomCode = getPara("inputRandomCode"); boolean loginSuccess = CaptchaRender.validate(this, inputRandomCode, RANDOM_CODE_KEY); if (loginSuccess) { render("/pages/index.html"); }else{ index(); } } public void img() { CaptchaRender img = new CaptchaRender(RANDOM_CODE_KEY); render(img); }
上传到BAE云服务器,需要引入如下jar:
因为BAE不支持c3p0,所以在Config中数据库连接使用
MysqlDataSource ds = new MysqlDataSource();
其中,log4J-adapter.jar是bae的日志实现(就一个class文件,还自己搞一套,害人又难用)
log4J.properties log4j.rootLogger=INFO, A2 log4j.appender.A2=com.baidu.bae.api.baelog.BaeAppender log4j.appender.A2.layout=org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
a_little_config:
jdbcUrl = jdbc:mysql://sqld.duapp.com:4050/云数据库名?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull user = apiKEY password = secretKEY devMode = true
注意:最好是使用svn checkout到本地工程,然后加入jfinal工程内容。有问题看bae的日志!
吐槽:最要命的是验证码图片显示不了,原因是BAE找不到字体,Time New Roman都没有,想实现用云存储图片试试吧