目录
一、准备工作
1.将框架打成jar包,然后导入新工程,并且把框架的依赖jar包导入进去
找到Export---->搜索jar包---->java(jar file)----jar file 的保存地址路径----下一步完成
2.将分页标签相关文件、及相关助手类pageTag类导入
3.框架的配置文件添加、以及web.xml的配置-以后开源框架的使用从这一步开始
导入所需jar包
二、代码操作
package com.zking.mvcplus.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.zking.mvcplus.entity.Book;
import com.zking.mvcplus.util.CommonUtils;
import com.zking.mvcplus.util.PageBean;
import com.zking.mvcplus.util.PinYinUtil;
import com.zking.mvcplus.util.StringUtils;
/**
* 图书方法类
* @author lucy
*
*/
public class BookDao extends BaseDao<Book> {
/**
* 图书新增
* @param book
*/
public void addBook(Book book) {
String sql = "insert into t_book(book_name,book_name_pinyin" + ",book_price,book_type) values(?,?,?,?)";
super.executeUpdate(sql,
new Object[] { book.getBook_name(), PinYinUtil.toPinyin(book.getBook_name()).toUpperCase(),
book.getBook_price(), book.getBook_type() });
}
/**
* 图书编辑
* @param book
*/
public void editBook(Book book) {
String sql = "update t_book set book_name=?,book_name_pinyin=?" + ",book_price=?,book_type=? where book_id=?";
super.executeUpdate(sql,
new Object[] { book.getBook_name(), PinYinUtil.toPinyin(book.getBook_name()).toUpperCase(),
book.getBook_price(), book.getBook_type(), book.getBook_id() });
}
/**
* 图书删除
* @param book
*/
public void delBook(Book book) {
String sql = "delete from t_book where book_id=?";
super.executeUpdate(sql, new Object[] { book.getBook_id() });
}
/**
* 查询单个书籍
* @param book
* @return
*/
public Book selectBookById(Book book) {
String sql = "select book_id,book_name,book_name_pinyin," + "book_price,book_type from t_book where book_id="
+ book.getBook_id();
List<Book> list = super.executeQuery(sql, null, new CallBack<Book>() {
@Override
public List<Book> foreachRs(ResultSet rs) throws SQLException {
return CommonUtils.toList(rs, Book.class);
}
});
//判断是否有数据
if (null != list && list.size() != 0)
return list.get(0);
else
return null;
}
/**
* 图书分页查询
* @param book
* @return
*/
public List<Book> queryBookPager(Book book, PageBean pageBean) {
// 定义SQL语句
String sql = "select book_id,book_name,book_name_pinyin," + "book_price,book_type from t_book where 1=1";
// 按照图书名称模糊查询
if (StringUtils.isNotBlank(book.getBook_name()))
sql += " and book_name like '%" + book.getBook_name() + "%'";
// 按照图书编号降序排序
sql += " order by book_id desc";
return super.executeQuery(sql, pageBean, new CallBack<Book>() {
@Override
public List<Book> foreachRs(ResultSet rs) throws SQLException {
return CommonUtils.toList(rs, Book.class);
}
});
}
}
方法实现类
package com.zking.mvcplus.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.mvc.framework.DispatcherAction;
import com.zking.mvc.framework.DriverModel;
import com.zking.mvcplus.dao.BookDao;
import com.zking.mvcplus.entity.Book;
import com.zking.mvcplus.util.PageBean;
/**
* 做一件事情:XxxAction extends Action -> execute
* 做多件事情:XxxAction extends DispatcherAction
* 注:
* 1)该子控制器类Action中提供一组与execute方法相同的参数和返回类型,值使方法名不相同的方法
* 2)必须要传递methodName的参数,该参数表示方法名,用于反射调用
* 3)每一次创建子控制器类Action,请先到mvc.xml配置文件中定义对应关系(基本要求)
*
* @author lucy
*
*/
public class BookAction extends DispatcherAction implements DriverModel<Book> {
//图书实体类
private Book book = new Book();
//图书方法类
private BookDao bd = new BookDao();
@Override
public Book getModel() {
return book;
}
/**
* 增加图书
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String addBook(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
bd.addBook(book);
return "list";
}
/**
* 图书修改
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String editBook(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
bd.editBook(book);
return "list";
}
/**
* 图书删除
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String delBook(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
bd.delBook(book);
return "list";
}
/**
* 分页查询图书信息
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String queryBookPager(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List<Book> books = bd.queryBookPager(book, pageBean);
//将查询的图书分页结果集和PageBean对象保存到request中
req.setAttribute("books", books);
req.setAttribute("pageBean", pageBean);
return "success";
}
/**
* 查询单个图书对象,跳转到编辑或者详情界面
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String selectOne(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//根据图书id查询单个图书信息
Book b = bd.selectBookById(book);
//将查询图书对象保存到request中
req.setAttribute("book", b);
//根据前端传入的参数type判断跳转到编辑还是详情界面
String type = req.getParameter("type");
//判断挑战的参数类型
if("edit".equals(type))
return "edit";//图书修改界面
else
return "detail";//图书详情界面
}
}
配置config.xml文件-------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ELEMENT forward EMPTY>
<!ATTLIST action
path CDATA #REQUIRED
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<!--
config标签:可以包含0~N个action标签
-->
<config>
<action type="com.zking.mvcplus.action.BookAction" path="/bookAction">
<forward name="list" path="/bookAction.action?methodName=queryBookPager" redirect="true"/>
<forward name="success" path="/bookList.jsp"/>
<forward name="edit" path="/editBook.jsp"/>
<forward name="detail" path="/selectBook.jsp"/>
</action>
</config>
引入pageTag类
package com.zking.mvcplus.tag;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.zking.mvcplus.util.PageBean;
/**
* 分页参数信息类
* @author Administrator
*
*/
public class PaginationTag extends BodyTagSupport {
private PageBean pageBean;
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.write(toHtml());
} catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
private String toHtml() {
//判断是否分页
if(null==pageBean||!pageBean.isPagination())
return "";
else {
StringBuilder sb=new StringBuilder();
//TODO
sb.append("<div style=\"float:right\">");
//拼接Form表单
sb.append("<form id=\"pageBeanForm\" action=\""+pageBean.getUrl()+"\" method=\"post\">");
//设置page隐藏域
sb.append("<input type=\"hidden\" name=\"page\"/>");
//拼接请求参数集合
Map<String, String[]> map = pageBean.getParams();
//获取请求参数集合键值对
Set<Entry<String,String[]>> entrySet = map.entrySet();
//遍历请求参数键值对
for (Entry<String, String[]> entry : entrySet) {
//获取请求参数名,也就是来自于表单中的name属性名称
String name=entry.getKey();
//如果参数为page,则continue跳过
if(name.equals("page"))
continue;
//获取请求参数对应的值,String[]
String[] values=entry.getValue();
//遍历value值
for (String value : values) {
//拼接请求参数
sb.append("<input type='hidden' name='"+name+"' value='"+value+"'/>");
}
}
sb.append("</form>");
//拼接共几页/第几页
sb.append("第"+pageBean.getPage()+"页/共"+pageBean.getMaxPager()+"页,");
//拼接首页、上一页、下一页、末页
if(pageBean.getPage()==1)
sb.append("首页 上一页 ");
else {
sb.append("<a href=\"javascript:gotoPage(1)\">首页</a> ");
sb.append("<a href=\"javascript:gotoPage("+pageBean.getProviousPager()+")\">上一页</a> ");
}
if(pageBean.getPage()==pageBean.getMaxPager())
sb.append("下一页 末页 ");
else {
sb.append("<a href=\"javascript:gotoPage("+pageBean.getNextPager()+")\">下一页</a> ");
sb.append("<a href=\"javascript:gotoPage("+pageBean.getMaxPager()+")\">末页</a> ");
}
//拼接跳转页码
sb.append("<input type=\"text\" id=\"p\" style=\"width:20px;\"/>");
sb.append("<input type=\"button\" value=\"GO\" οnclick=\"javascript:skipPage();\"/>");
//拼接javascript跳转方法
sb.append("<script type=\"text/javascript\">\r\n" +
"function gotoPage(page){\r\n" +
" document.getElementById(\"pageBeanForm\").page.value=page;\r\n" +
" document.getElementById(\"pageBeanForm\").submit();\r\n" +
"}");
sb.append("function skipPage(){\r\n" +
" var page=document.getElementById(\"p\").value;\r\n" +
" if(isNaN(page)||page<1||page>="+pageBean.getMaxPager()+"){\r\n" +
" alert('请输入1~"+pageBean.getMaxPager()+"之间数字!');\r\n" +
" return false;\r\n" +
" }\r\n" +
" gotoPage(page);\r\n" +
"}\r\n" +
"</script>");
sb.append("</div>");
return sb.toString();
}
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
}
导入自定义分页标签tld描述文件下
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 标签库描述符 -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<!-- 代表标签库的版本号 -->
<tlib-version>1.0</tlib-version>
<!-- 代表jsp的版本 -->
<jsp-version>1.2</jsp-version>
<!-- 你的标签库的简称 -->
<short-name>z</short-name>
<!-- 你标签库的引用uri -->
<uri>/zking</uri>
<tag>
<!-- 标签名 -->
<name>pagination</name>
<!-- 标签工具类 -->
<!-- 利用反射机制,通过Class.forName(类的全路径名/包名+类名)的方式反射机制实例化标签助手类 -->
<tag-class>com.zking.mvcplus.tag.PaginationTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>empty</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<attribute>
<!-- 自定义标签的属性名称 -->
<name>pageBean</name>
<!-- true表示必填 -->
<required>true</required>
<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>