UserController.java
package com.wbs.controller;
import java.util.List;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.wbs.model.User;
public class UserController extends Controller {
public void index(){
String sql="select * from t_user ";
/* setAttr("objectlist", User.dao.find(sql));*/
List<Record> re = Db.find(sql);
setAttr("obj", re);
System.out.println(re);
renderJsp("index.jsp");
}
public void adduser(){
//System.out.println("adduser");
renderJsp("adduser.jsp");
}
public void updateUser(){
User user=getModel(User.class);
//User user=getModel(User.class,"u");
//user.set("sex", "男");
boolean flag= user.update();//save function
if (flag) redirect("/user/");
else{
renderText("sorry update fauiler!");
}
}
public void queryById(){
int id=getParaToInt(0);//获取参数的值
String sql="select * from t_user where id =? ";
//User user=User.dao.findFirst(sql,id);
Record re=Db.findFirst(sql,id);
setAttr("query", re);
renderJsp("editer.jsp");
}
public void deleteById(){
int id=getParaToInt(0);
//String sql="delete from t_user where id=?";
//boolean flag= Db.deleteById(sql, id);
//boolean flag=User.dao.deleteById(id);
boolean flag=Db.deleteById("t_user", id);
if (flag) {
redirect("/user");
}else{
renderText("删除失败");
}
}
public void doadduser(){
User user=getModel(User.class);
//User user=getModel(User.class,"u");
//user.set("sex", "男");
boolean flag= user.save();//save function
if (flag) redirect("/user/");
else{
renderText("sorry error!");
}
}
}
Config.java
package com.wbs.config;
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.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.wbs.controller.UserController;
import com.wbs.model.User;
public class Config extends JFinalConfig {
@Override
public void configConstant(Constants me) {
PropKit.use("a_little_config.txt");
me.setDevMode(PropKit.getBoolean("devMode", true));
me.setViewType(ViewType.JSP);
}
@Override
public void configRoute(Routes me) {
me.add("/user",UserController.class);
}
@Override
public void configEngine(Engine me) {
}
@Override
public void configPlugin(Plugins me) {
/*
DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
me.add(druidPlugin);
ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
arp.setDialect(new SqlServerDialect());
me.add(arp);
arp.addMapping("t_user", User.class);*/
// 配置C3p0数据库连接池插件
C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),PropKit.get("driver"));
me.add(c3p0Plugin);
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
arp.setDialect(new AnsiSqlDialect());//数据库方言的配置
me.add(arp);
arp.addMapping("t_user", User.class); // 映射user 表到 User模型
}
@Override
public void configInterceptor(Interceptors me) {
}
@Override
public void configHandler(Handlers me) {
me.add(new ContextPathHandler("ctx"));
}
}
editer.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>editeruser</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table>
<thead>
<tr>
<td>姓名</td>
<td>性别</td>
<td>住址</td>
</tr>
</thead>
<tbody>
<tr>
<td>${query.username}</td>
<td>${query.sex}</td>
<td>${query.dress}</td>
</tr>
</tbody>
</table>
<form action="/user/updateUser" >
<input type="hidden" name="user.id" value="${query.id}" >
<p>用户名<input name="user.username" value="${query.username}" size="30" maxlength="30" ></p>
<p>性别<input name="user.sex" value="${query.sex}" size="30" maxlength="30" ></p>
<p>地址<input name="user.dress" value="${query.dress}" size="30" maxlength="30" ></p>
<input type="submit" name="submit" value="保存" >
</form>
</body>
</html>
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>">
<title>My JSP 'adduser.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!-- 数据卡打 -->
<body>
<br>
<p><a href="/user/adduser">添加用户</a>
<p><a href="/user">查询用户</a>
<br>
<h1>User 列表</h1>
<!-- 做表格 首先需要一个 table table里面包括 head和body -->
<table border="2px solid bule">
<!-- table 就是一个表格框 -->
<thead>
<!-- 这是头 -->
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>地址</td>
</tr>
</thead>
<tbody>
<!-- 头部是固定的一行 内容循环 所以需要循环 body里面的信息 运行一下试试看-->
<c:forEach var="objj" items="${requestScope.obj }" varStatus="stat">
<tr>
<td>${stat.index+1}</td>
<td>${objj.username}</td>
<td>${objj.sex}</td>
<td>${objj.dress}</td>
<td><a href="/user/queryById/${objj.id}">修改</a></td>
<td><a href="/user/deleteById/${objj.id}">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<%-- <form>
<p>user_name<input type="text" name="username" value="${objj.username}" maxlength="20" size="50" /> </p>
<p>user_sex<input type="text" name="sex" value="${objj.sex}" maxlength="20" size="50" /></p>
<p>user_address<input type="text" name="dress" value="${objj.dress}" maxlength="20" size="50" /></p>
</form> --%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'adduser.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="/user/doadduser" method="post">
<p>用户名<input type="text" name="user.username" value="" maxlength="20" size="50"> </p>
<p>性别<input type="text" name="user.sex" value="" maxlength="20" size="50"></p>
<p>地址<input type="text" name="user.dress" value="" maxlength="20" size="50"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>