转载自http://z18022893621.iteye.com/blog/1961412
一、建Web Project,导入SSH2框架
二、在WebRoot下建给文件夹script,里面放入如下文件:
1.themes(可以给ui换主题)
2.jquery-1.8.0.min.js(可能会报错,不用管)
3.jquery.easyui.min.js
4.easyui-lang-zh_CN.js(国际化文件)
5.book.js(脚本)
三、entity和entity的映射文件
Book:
- package org.ajax.entity;
- import java.sql.Timestamp;
- /**
- * Book entity. @author MyEclipse Persistence Tools
- */
- public class Book implements java.io.Serializable {
- // Fields
- private Integer id;
- private String isbn;
- private String title;
- private Double price;
- private Timestamp pubdate;
- private String intro;
- // Constructors
- /** default constructor */
- public Book() {
- }
- /** minimal constructor */
- public Book(Double price, Timestamp pubdate) {
- this.price = price;
- this.pubdate = pubdate;
- }
- /** full constructor */
- public Book(String isbn, String title, Double price, Timestamp pubdate, String intro) {
- this.isbn = isbn;
- this.title = title;
- this.price = price;
- this.pubdate = pubdate;
- this.intro = intro;
- }
- // Property accessors
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getIsbn() {
- return this.isbn;
- }
- public void setIsbn(String isbn) {
- this.isbn = isbn;
- }
- public String getTitle() {
- return this.title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public Double getPrice() {
- return this.price;
- }
- public void setPrice(Double price) {
- this.price = price;
- }
- public Timestamp getPubdate() {
- return this.pubdate;
- }
- public void setPubdate(Timestamp pubdate) {
- this.pubdate = pubdate;
- }
- public String getIntro() {
- return this.intro;
- }
- public void setIntro(String intro) {
- this.intro = intro;
- }
- }
Book.hbm.xml:
- "1.0" encoding="utf-8"?>
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
-
- <class name="org.ajax.entity.Book" table="BOOK" schema="y2">
-
"id" type="java.lang.Integer"> -
"ID" length="200" /> -
class ="sequence"> - "sequence">book_seq
-
"isbn" type="java.lang.String"> -
"ISBN" length="100" /> -
"title" type="java.lang.String"> -
"TITLE" length="2048" /> -
"price" type="java.lang.Double"> -
"PRICE" precision="126" scale="0" not-null="true" /> -
"pubdate" type="java.sql.Timestamp"> -
"PUBDATE" length="11" not-null="true" /> -
"intro" type="java.lang.String"> -
"INTRO" length="4000" /> - class>
四、dao和daoImpl
- package org.ajax.dao;
- import java.util.List;
- import org.ajax.entity.Book;
- /**
- * 接口
- *
- * @author miao
- *
- */
- public interface BookDao {
- /**
- * 查询所有书籍
- *
- * @return
- */
- public List
find(); - /**
- * 添加书籍
- *
- * @param book
- * @return
- */
- public int add(Book book);
- /**
- * 删除书籍
- *
- * @param id
- * @return
- */
- public int delete(int id);
- /**
- * 获得一书籍记录
- *
- * @param id
- * @return
- */
- public Book findById(int id);
- /**
- * 更新书籍
- *
- * @param book
- * @return
- */
- public int update(Book book);
- /**
- * 统计书籍共多少本
- *
- * @return
- */
- public long findTotal();
- /**
- * 查询一页的数据
- *
- * @param begin 从哪条开始0
- * @param end 得到多少条
- * @param sort 排序字段
- * @param order 升序或降序 desc/asc
- */
- public List
findPageBooks(int begin, int end, String sort, String order); - }
- package org.ajax.dao.impl;
- import java.sql.SQLException;
- import java.util.List;
- import org.ajax.dao.BookDao;
- import org.ajax.entity.Book;
- import org.hibernate.Criteria;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.criterion.DetachedCriteria;
- import org.hibernate.criterion.Order;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- /**
- * 接口实现类
- *
- * @author miao
- *
- */
- public class BookDaoImpl extends HibernateDaoSupport implements BookDao {
- @SuppressWarnings("unchecked")
- public List
find() { - DetachedCriteria criteria = DetachedCriteria.forClass(Book.class);
- criteria.addOrder(Order.desc("pubdate"));
- return super.getHibernateTemplate().findByCriteria(criteria);
- }
- public int add(Book book) {
- return (Integer) super.getHibernateTemplate().save(book);
- }
- public int delete(int id) {
- return super.getHibernateTemplate().bulkUpdate("delete from Book b where b.id=?", id);
- }
- public Book findById(int id) {
- return (Book) super.getHibernateTemplate().get(Book.class, id);
- }
- public int update(Book book) {
- return super.getHibernateTemplate().bulkUpdate(
- "update Book b set b.isbn = ?, b.title = ?, b.price = ?,"
- + "b.pubdate = ?, b.intro = ? where b.id = ?",
- new Object[] { book.getIsbn(), book.getTitle(), book.getPrice(), book.getPubdate(),
- book.getIntro(), book.getId() });
- }
- public long findTotal() {
- return (Long) super.getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException, SQLException {
- return (Long) session.createQuery("select count(*) from Book").uniqueResult();
- }
- });
- }
- /**
- * 查询一页的数据
- *
- * @param begin 从哪条开始0
- * @param end 得到多少条
- * @param sort 排序字段
- * @param order 升序或降序 desc/asc
- */
- @SuppressWarnings("unchecked")
- public List
findPageBooks(final int begin, final int end, final String sort, - final String order) {
- // 当要用到原生的Hibernate的Session的时候,这种最灵活,可以使用Query和Criteria,不用着急管理会话和事
- return super.getHibernateTemplate().executeFind(new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException, SQLException {
- Criteria criteria = session.createCriteria(Book.class);
- if ("desc".equals(order)) {
- criteria.addOrder(Order.desc(sort));
- } else {
- criteria.addOrder(Order.asc(sort));
- }
- criteria.setFirstResult(begin).setMaxResults(end);
- return criteria.list();
- }
- });
- }
- }
五、biz和bizImpl
- package org.ajax.biz;
- import java.util.List;
- import org.ajax.entity.Book;
- /**
- * 业务类
- *
- * @author miao
- *
- */
- public interface BookBiz {
- /**
- * 查询所有书籍
- *
- * @return
- */
- public List
findAllBooks(); - /**
- * 添加书籍
- *
- * @param book
- * @return
- */
- public int addBook(Book book);
- /**
- * 删除书籍
- *
- * @param id
- * @return
- */
- public int deleteBook(int id);
- /**
- * 获得一书籍记录
- *
- * @param id
- * @return
- */
- public Book findBook(int id);
- /**
- * 更新书籍
- *
- * @param book
- * @return
- */
- public int updateBook(Book book);
- /**
- * 统计书籍共多少本
- *
- * @return
- */
- public long findTotal();
- /**
- * 查询一页的数据
- *
- * @param page 当前页号
- * @param size 页面大小
- * @param sort 排序字段
- * @param order 升序或降序 desc/asc
- */
- public List
findPageBooks(int page, int size, String sort, String order); - }
- package org.ajax.biz.impl;
- import java.util.List;
- import org.ajax.biz.BookBiz;
- import org.ajax.dao.BookDao;
- import org.ajax.entity.Book;
- /**
- * 业务实现类
- *
- * @author miao
- *
- */
- public class BookBizImpl implements BookBiz {
- private BookDao bookDao;
- public void setBookDao(BookDao bookDao) {
- this.bookDao = bookDao;
- }
- public List
findAllBooks() { - return bookDao.find();
- }
- public int addBook(Book book) {
- return bookDao.add(book);
- }
- public int deleteBook(int id) {
- return bookDao.delete(id);
- }
- public Book findBook(int id) {
- return bookDao.findById(id);
- }
- public int updateBook(Book book) {
- return bookDao.update(book);
- }
- public long findTotal() {
- return bookDao.findTotal();
- }
- /**
- * 查询一页的数据
- *
- * @param page 当前页号
- * @param size 页面大小
- * @param sort 排序字段
- * @param order 升序或降序 desc/asc
- */
- public List
findPageBooks(int page, int size, String sort, String order) { - int begin = (page - 1) * size;
- return bookDao.findPageBooks(begin, size, sort, order);
- }
- }
六、action
- package org.ajax.action;
- import java.util.HashMap;
- import java.util.Map;
- import org.ajax.biz.BookBiz;
- import org.ajax.entity.Book;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * Action
- * @author miao
- *
- */
- public class BookAction extends ActionSupport {
- // 调用业务类
- private BookBiz bookBiz;
- private Book book; // 一本书
- private int page;// 当前第几页
- private Map
data = new HashMap ();// 封装数据 - private int size;// 页面大小,页面是rows
- private String order;// 排序方向,desc和asc
- private String sort;// 排序属性名,如price
- // 标识操作是否成功
- private boolean operateSuccess;
- // set注入
- public void setBookBiz(BookBiz bookBiz) {
- this.bookBiz = bookBiz;
- }
- /*
- * 给easyui排序用的,表示排序方法
- */
- public void setOrder(String order) {
- this.order = order;
- }
- /*
- * 给easyui排序用的,表示排序字段
- */
- public void setSort(String sort) {
- this.sort = sort;
- }
- /*
- * 给easyui指定页面大小用的,如果要指定页面大小可变
- * 页面是rows
- */
- public void setRows(int size) {
- this.size = size;
- }
- /*
- * 给easyui分页用的
- */
- public void setPage(int page) {
- this.page = page;
- }
- // getter/setter方法
- public Book getBook() {
- return book;
- }
- public int getPage() {
- return page;
- }
- public Map
getData() { - return data;
- }
- public void setData(Map
data) { - this.data = data;
- }
- public int getRows() {
- return size;
- }
- public String getOrder() {
- return order;
- }
- public String getSort() {
- return sort;
- }
- public void setBook(Book book) {
- this.book = book;
- }
- public boolean isOperateSuccess() {
- return operateSuccess;
- }
- public void setOperateSuccess(boolean operateSuccess) {
- this.operateSuccess = operateSuccess;
- }
- /**
- * 查询某一页的书籍
- */
- public String list() {
- data.clear();// 清除
- if (sort == null) {
- sort = "title";// 默认按书名排序
- }
- if (order == null) {
- order = "asc";// 默认按升序排序
- }
- data.put("rows", bookBiz.findPageBooks(page, size, sort, order));// 得到某一页的数据
- data.put("total", bookBiz.findTotal());// 得到所有的记录数
- return SUCCESS;
- }
- /**
- * 添加书籍
- */
- public String addBook() {
- operateSuccess = (bookBiz.addBook(book) > 0);
- return SUCCESS;
- }
- /**
- * 更新书籍
- */
- public String updateBook() {
- operateSuccess = (bookBiz.updateBook(book) > 0);
- return SUCCESS;
- }
- /**
- * 删除书籍
- */
- public String deleteBook() {
- operateSuccess = (bookBiz.deleteBook(book.getId()) > 0);
- return SUCCESS;
- }
- /**
- * 查询一本书
- */
- public String findBook() {
- book = bookBiz.findBook(book.getId());
- return SUCCESS;
- }
- }
七、spring配置文件
- xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
- property>
- <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl">
- property>
- <property name="username" value="y2">property>
- <property name="password" value="bdqn">property>
- bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.Oracle10gDialect
- prop>
- <prop key="show_sql">trueprop>
- <prop key="format_sql">trueprop>
- props>
- property>
- <property name="mappingResources">
- <list>
- <value>org/ajax/entity/Book.hbm.xmlvalue>
- list>
- property>
- bean>
- <bean id="bookDao" class="org.ajax.dao.impl.BookDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- bean>
- <bean id="bookBiz" class="org.ajax.biz.impl.BookBizImpl">
- <property name="bookDao" ref="bookDao" />
- bean>
- <bean id="bookAction" class="org.ajax.action.BookAction">
- <property name="bookBiz" ref="bookBiz" />
- bean>
- <bean id="txManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- bean>
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
- tx:attributes>
- tx:advice>
- <aop:config>
- <aop:pointcut id="bizMethod" expression="execution(* org.ajax.biz.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethod" />
- aop:config>
- beans>
八、struts配置文件
- xml version="1.0" encoding="UTF-8" ?>
- >
- <struts>
- <constant name="stuts.objectFactory" value="spring" />
- <constant name="struts.devMode" value="true" />
- <package name="book" extends="json-default" namespace="/">
- <action name="list" class="bookAction" method="list">
- <result type="json">
- <param name="root">dataparam>
- result>
- action>
- <action name="addBook" class="bookAction" method="addBook">
- <result type="json" />
- action>
- <action name="deleteBook" class="bookAction" method="deleteBook">
- <result type="json" />
- action>
- <action name="findBook" class="bookAction" method="findBook">
- <result type="json" />
- action>
- <action name="updateBook" class="bookAction" method="updateBook">
- <result type="json" />
- action>
- package>
- struts>
九、页面,book.jsp
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":"
- + request.getServerPort() + path + "/";
- %>
- >
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <base href="<%=basePath%>" />
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link rel="stylesheet" type="text/css" href="script/themes/default/easyui.css" />
- <link rel="stylesheet" type="text/css" href="script/themes/icon.css" />
- <script type="text/javascript" src="script/jquery-1.8.0.min.js">script>
- <script type="text/javascript" src="script/jquery.easyui.min.js">script>
- <script type="text/javascript" src="script/easyui-lang-zh_CN.js">script>
- <link rel="stylesheet" href="style/book.css" type="text/css" />
- <script type="text/javascript" src="script/book.js">script>
- <title>书籍操作title>
- head>
- <body>
- <div id="main">
- <table id="bookbody">
- table>
- div>
- <div id="divEdit">
- <div id="tabEdit">
- <form id="frmEdit">
- <input type="hidden" id="id" name="book.id" />
- <dl>
- <dd>
- ISBN:
- dd>
- <dd>
- <input type="text" size="15" id="isbn" name="book.isbn" />
- dd>
- dl>
- <dl>
- <dd>
- 书名:
- dd>
- <dd>
- <input type="text" size="40" id="title" name="book.title" />
- dd>
- dl>
- <dl>
- <dd>
- 价格¥:
- dd>
- <dd>
- <input type="text" size="10" id="price" name="book.price" />
- dd>
- dl>
- <dl>
- <dd>
- 出版日期:
- dd>
- <dd>
- <input type="text" style="width: 150px" id="pubdate" name="book.pubdate" />
- dd>
- dl>
- <dl>
- <dd>
- 简介:
- dd>
- <dd>
- <textarea cols="45" rows="3" id="intro" name="book.intro">textarea>
- dd>
- dl>
- form>
- div>
- div>
- body>
- html>
十、脚本文件,book.js
- //JQuery的入口
- $(function() {
- listBook();
- // 日期加上日期控件
- $("#pubdate").datebox({
- required : true
- });
- // 给文本框加上验证器
- $("#isbn").validatebox({
- required : true
- });
- // 书名的验证
- $("#title").validatebox({
- required : true,
- missingMessage : '书名不能为空'
- });
- // 价格用货币验证框
- $("#price").numberbox({
- required : true,
- min : 5.5,
- max : 9999,
- precision : 2,
- missingMessage : '请输入价格'
- });
- // 简介加验证
- $("#intro").validatebox({
- required : true
- });
- });
- // 加载书籍列表
- function listBook() {
- $("#bookbody").datagrid({
- width : 600,
- height : "auto",
- iconCls : 'icon-help', // 表格左上角的图标样式
- url : 'list.action', // 访问服务器的地址,要求返回JSON对象
- rownumbers : true, // 在最前面显示行号
- fitColumns : true, // 自动适应列宽
- pagination : true, // 在底部显示分页工具栏
- striped : true, // 隔行变色
- singleSelect : true, // 每次只选中一行
- loadMsg : '加载书籍列表ing……',
- pageSize : 5, // 指定每页的大小,服务器要加上page属性和total属性
- remoteSort : true, // 从服务器端排序,默认true
- pageList : [ 3, 5, 10 ], // 可以设置每页记录条数的列表,服务器要加上rows属性
- idField : 'id', // 主键属性
- toolbar : [ {// 工具栏
- text : '添加',
- iconCls : 'icon-add', // 图标
- handler : function() { // 处理函数
- addBook();
- }
- }, {
- text : '删除',
- iconCls : 'icon-cancel', // 图标
- handler : function() { // 处理函数
- deleteBook();
- }
- }, {
- text : '编辑',
- iconCls : 'icon-edit',// 图标
- handler : function() {// 处理函数
- editBook();
- }
- } ],
- columns : [ [ {
- field : 'isbn',
- title : 'ISBN',
- width : 70
- }, {
- field : 'title',
- title : '书籍名称',
- // 可以排序,但服务器也完成相应的代码,要加入sort和order属性
- sortable : true
- }, {
- field : 'price',
- title : '价格',
- align : 'right',
- width : 60,
- sortable : true,
- formatter : function(value) {
- return "$" + value;
- }
- }, {
- field : 'pubdate',
- title : '出版日期',
- sortable : true,
- formatter : function(value) {
- return value.substring(0, 10);
- }
- } ] ]
- });
- }
- // 显示编辑窗口
- function showEditForm() {
- $("#tabEdit").dialog({
- modal : true,// 模式窗口
- title : '书籍操作',
- iconCls : 'icon-save',
- buttons : [ {
- text : '确认',
- handler : function() {
- // 进行表单字段验证,当全部字段都有效时返回true和validatebox一起使用
- if ($('#frmEdit').form('validate')) {
- // 提交到服务器并写入数据库
- dealSave();
- // 关闭窗口
- closeForm();
- } else {
- $.messager.alert('验证', '书籍信息有误或不完整', 'error');
- }
- }
- }, {
- text : '取消',
- handler : function() {
- closeForm();
- }
- } ]
- });
- }
- // 关闭窗口
- function closeForm() {
- $("#frmEdit").form('clear');
- $('#tabEdit').dialog('close');
- }
- // 添加的函数
- function addBook() {
- // 清空原有的数据
- $('#frmEdit').form('clear');
- // 显示添加对话框
- showEditForm();
- }
- // 编辑按钮的操作
- function editBook() {
- var book = $('#bookbody').datagrid('getSelected');// 得到选中的一行数据
- // 如果没有选中记录
- if (book == null) {
- $.messager.alert('书籍', '请先选中要编辑的书籍', 'info');
- return;
- }
- $('#frmEdit').form('clear');
- // 填充数据
- $("#id").val(book.id);
- $("#isbn").val(book.isbn);
- $("#title").val(book.title);
- $("#price").numberbox("setValue", book.price);
- // 给默认值
- $("#pubdate").datebox("setValue", book.pubdate.substring(0, 10));
- $("#intro").val(book.intro);
- // 显示编辑页面
- showEditForm();
- }
- // 在增加和更新时点确定按钮的处理函数
- function dealSave() {
- // 表单数据序列化成一个字符串用&拼接
- var params = $("#frmEdit").serialize();
- // 得到id的值,为空串表示添加
- if ($("#id").val() == "") {
- $.post("addBook.action", params, function(result) {
- if (result.operateSuccess) {
- $('#bookbody').datagrid('reload');// 重新加载
- $.messager.alert('添加', '添加成功', 'info');
- } else {
- $.messager.alert('添加', '添加失败', 'warning');
- }
- });
- } else {
- // 表示更新
- $.post("updateBook.action", params, function(result) {
- if (result.operateSuccess) {
- $('#bookbody').datagrid('reload');// 重新加载
- $.messager.alert('更新', '更新成功', 'info');
- } else {
- $.messager.alert('更新', '更新失败', 'warning');
- }
- });
- }
- }
- // 删除书籍
- function deleteBook() {
- var book = $('#bookbody').datagrid('getSelected');// 得到选中的一行数据
- // 如果没有选中记录
- if (book == null) {
- $.messager.alert('删除', '请先选中要删除的书籍', 'info');
- return;
- }
- $.messager.confirm('确认', '真的要删除选中的记录吗?', function(r) {
- if (r) {
- var url = "deleteBook.action?book.id=" + book.id;
- // 试一下get方法(地址,回调函数)
- $.get(url, function(result) {
- if (result.operateSuccess) {
- $.messager.alert('删除', '选中的书籍成功删除!', 'info');
- // 重新加载
- $("#bookbody").datagrid('reload');
- } else {
- $.messager.alert('删除', '删除失败!', 'warning');
- }
- });
- }
- });
- }
十一、css,book.css
- #divEdit {
- display: none;
- }
- * {
- font: 12px Arial;
- }
- div#main {
- margin: 0px auto;
- width: 600px;
- }
- #tabEdit input[type="text"],#tabEdit textarea {
- border: solid #66ccff 1px;
- }
- #tabEdit dl {
- padding-right: 35px;
- }
十二、demo
JQuery-EasyUI-Book.zip