刚开始接触JFinal,感觉很强大,看了下文档,上面写的是
public class Blog extends Model<Blog> {
public static final Blog me = new Blog();
}
//
在页面表单中采用
modelName.attrName
形式为作为表单域的
name
<form action ="/blog/save" method="post">
<input name ="blog.title" type="text">
<input name="blog.content" type="text">
<input value="提交" type="submit">
</form>
public class BlogController extends Controller {
public void save() {
//
页面的
modelName正好是Blog类名的首字母小写
Blogblog = getModel(Blog.class);
//
如果表单域的名称为
"otherName.title"
可加上一个参数来获取
blog = getModel(Blog.class,"otherName");
}
}
小弟照着写了一个类:
import com.jfinal.plugin.activerecord.Model;
public class User extends Model<User>{
public static final User user = new User();
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static User getUser() {
return user;
}
}
在HelloController类里面调用的时候用getModel()方法去取表单数据总是空不知道为什么,求波总指教:
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
@Before(DemoInterceptor.class)
public class HelloController extends Controller{
public void index() {
renderText(getPara());
}
public void test(){
renderText(getPara());
}
public void login(){
System.out.println("------------------------------------");
User user = getModel(User.class);
String username = getPara("user.username");
System.out.println(user.getUsername());
setSessionAttr("username",username);
renderJsp("/NewFile.jsp");
}
}
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hello\login" method="post">
<span>username:</span><input type="text" name="user.username"><br>
<span>password:</span><input type="password" name="user.password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>