strutsCRUD
Bookdao
public class BookDao extends JsonBaseDao{ //分页查询书本信息
//根据书本id查询当个书本信息
public List<Map<String, Object>> queryBook(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
//String sql="SELECT * FROM t_book";
String sql="select * from t_mvc_book where true";
String bname=JsonUtils.getParamVal(paMap, "bname");
if(StringUtils.isNotBlank(bname)) {
sql+=" and bname like '%"+bname+"%'";
}
return super.executeQuery(sql, pageBean); } //新增书本信息
public int addBook (Map<String, String[]> paMap,PageBean pageBean) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException{
String sql="insert into t_mvc_book(bname,price) values(?,?)";
return super.executeUpdate(sql, new String[] {"bname","price"}, paMap);
} //修改书本信息
public int editBook (Map<String, String[]> paMap,PageBean pageBean) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException{
String sql="update t_mvc_book set bname=?,price=? where bid=?";
return super.executeUpdate(sql, new String[] {"bname","price","bid"}, paMap);
} //新增书本信息
public int delBook (Map<String, String[]>paMap,PageBean pageBean) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException{
String sql="delete from t_mvc_book where bid=?";
return super.executeUpdate(sql, new String[] {"bid"}, paMap);
} public List<Map<String, Object>> book(Map<String, String[]>paMap,PageBean pageBean) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException{ String bid=JsonUtils.getParamVal(paMap, "bid");
if(StringUtils.isNotBlank(bid)) {
String sql="select * from t_mvc_book where bid="+bid;
return executeQuery(sql, pageBean);
}
else {
return null;
}
} }
BookAction
public class BookAction extends BaseAction{
private BookDao bookDao=new BookDao();
private PageBean pageBean;
private String type; public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} //新增书本
public String addBook() throws Exception, Exception {
bookDao.addBook(request.getParameterMap(), null);
return "success"; } //修改书本
public String editBook() throws Exception, Exception {
bookDao.editBook(request.getParameterMap(), null);
return "success"; } //删除书本
public String delBook() throws Exception, Exception {
bookDao.delBook(request.getParameterMap(), null);
return "success"; } //查询当个
public String book() throws Exception, Exception {
List<Map<String, Object>> book =bookDao.queryBook(request.getParameterMap(), null);
Map<String, Object> map = book.get(0);
request.setAttribute("book", map);
if("edit".equals(type)) {
return "edit";
}
else {
return "detail";
} } public String queryBook() throws Exception, Exception {
pageBean=new PageBean();
pageBean.setRequest(request);
List<Map<String, Object>> queryBook = bookDao.queryBook(request.getParameterMap(), pageBean); Map<String, Object> result=new HashMap<>();
result.put("queryBook",queryBook);
result.put("pageBean", pageBean);
request.setAttribute("result", result);
return "list"; }
}
BaseAction
public abstract class BaseAction implements ServletResponseAware,ServletRequestAware{ protected HttpServletResponse response;
protected HttpServletRequest request;
protected HttpSession session;
protected ServletContext application; @Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
this.session=request.getSession();
this.application= request.getServletContext();
} @Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}
}
book实体
public class Book implements Serializable{
private Integer bid;
private String bname;
private Float price; public Integer getBid() {
return bid;
} public void setBid(Integer bid) {
this.bid = bid;
} public String getBname() {
return bname;
} public void setBname(String bname) {
this.bname = bname;
} public Float getPrice() {
return price;
} public void setPrice(Float price) {
this.price = price;
} @Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
} }
分页助手类
public class PageTag extends BodyTagSupport { private PageBean bean; public PageBean getBean() {
return bean;
} public void setBean(PageBean bean) {
this.bean = bean;
} @Override
public int doStartTag() throws JspException {
JspWriter out= pageContext.getOut();
try {
out.print(tohtml());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
} private String tohtml() {
StringBuilder sb=new StringBuilder();
//拼接下一次发送请求所要提交的隐藏的form表单
sb.append("<form id='pageBeanForm' action='"+bean.getUrl()+"' method='post'>");
sb.append(" <input type='hidden' name='page'>"); Map<String, String[]> paMap = bean.getParameterMap();
if(paMap!=null&&paMap.size()>0) {
Set<Entry<String, String[]>> entrySet = paMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
//上一次请求可能携带页面name=page的参数,但是改参数在前面已经单独赋值
//为什么要单独赋值呢?因为上一次请求是第一页的数据,下一次可能是第二页,因为这前后请求page对应的值是不一样的,要单独赋值
if(!"page".equals(entry.getKey())) {
for(String val:entry.getValue()) {
sb.append("<input type='hidden' name='"+entry.getKey()+"' value='"+val+"'>");
}
}
}
}
sb.append(" </form>"); //拼接分页条
sb.append("<div style='text-align: right; font-size: 12px;'>");
sb.append(" 每页"+bean.getRows()+"条,共"+bean.getTotal()+"条,第"+bean.getPage()+"页,共"+bean.getMaxPage()+"页 <a href='javascript:gotoPage(1)'>首页");
sb.append(" </a> <a");
sb.append(" href='javascript:gotoPage("+bean.previousPage()+")'>上一页</a> <a");
sb.append(" href='javascript:gotoPage("+bean.nextPage() +")'>下一页</a> <a");
sb.append(" href='javascript:gotoPage("+bean.getMaxPage()+")'>尾页</a> <input type='text'"); sb.append(" id='skipPage'");
sb.append(" style='text-align: center; font-size: 12px; width: 50px;'> <a");
sb.append(" href='javascript:skipPage()'>Go</a>");
sb.append(" </div>");
//拼接所需要的js代码
sb.append(" <script type='text/javascript'>");
sb.append(" function gotoPage(page) {");
sb.append(" document.getElementById('pageBeanForm').page.value = page;");
sb.append(" document.getElementById('pageBeanForm').submit();");
sb.append(" }");
sb.append(" function skipPage() {");
sb.append(" var page = document.getElementById('skipPage').value;");
sb.append(" if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+bean.getMaxPage()+"){");
sb.append(" alert('请输入1~N的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append("}");
sb.append("</script>");
return sb.toString(); }
}
struts-book.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="struts-book" extends="struts-base">
<action name="bookAction_*"
class="com.hmc.struts2_03.action.BookAction" method="{1}"> <result name="success" type="redirect">
/bookAction_queryBook.action
</result> <result name="list" type="dispatcher">
/bookList.jsp
</result> <result name="edit" >
/bookEdit.jsp
</result> <result name="detail" >
/bookDetail.jsp
</result> </action>
</package> </struts>
创建jsp页面进行测试
新增
结果
修改
修改圣墟为斗神
结果
删除斗神
模糊查询(不死不灭)
strutsCRUD的更多相关文章
- struts--CRUD优化(图片上传)
1.上传方式 上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系 文件服务器 2.web代码优化 package com.yuan.crud.web; impo ...
随机推荐
- xorm -Exist方法实例
判断某个记录是否存在可以使用Exist, 相比Get,Exist性能更好. package main import ( "fmt" _ "github.com/go-sq ...
- 【IDEA使用技巧】(3) —— IntelliJ IDEA Maven配置
1.IntelliJ IDEA Maven配置 1.1. Maven介绍与下载 Maven是一个项目管理工具,使用它能对Java项目中的jar包进行管理与项目构建,很好地解决了传统项目使用导包的方式管 ...
- Ubuntu下载搜狗输入法
实在...因为百度上写的就很好了,所以这里就直接“链”了.. https://jingyan.baidu.com/article/2d5afd6933a67b85a2e28e9f.html
- 使用 Issue 管理软件项目详解
文章来源:http://www.ruanyifeng.com/blog/2017/08/issue.html 软件开发(尤其是商业软件)离不开项目管理,Issue 是最通用的管理工具之一. 本文介绍 ...
- Feign 失败降级未生效和超时配置优先级问题
一.问题: 生产环境服务A 通过feign调用 服务B,服务A报警信息如下: 详细分析发现问题 (1)服务A调用服务B失败,未触发声明的失败降级操作 (2)同时配置ribbon和feign超时时间,优 ...
- SDOI2019 Round2
这鬼家伙已经咕了好久了-- SDOIR2的题目挺好玩的- 快速查询(???) 不难发现所有的操作都可以通过区间打Tag实现 那么可以维护两个标记\(a,b\)表示序列中的数为\(x\)时实际表示的值是 ...
- Python3 MySQL
首先安装pymysql pip install pymysql 准备数据库:创建一个数据库testdb mysql实例: import pymysql #打开数据库连接,使用数据库所在的IP127. ...
- JS基础_强制类型转换-Number
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- kubernetes第三章--创建harbor私有镜像库
- 当ajax都完成后执行方法
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...