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 ...
随机推荐
- 数组中重复的数字(Python)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-08-13 22:35 # @Author : daryl # @File : ...
- 排Bug技巧
排Bug技巧 什么是Bug? Bug一词的英文原意是"臭虫"或"虫子".但是现在,在电脑系统或游戏程序中,如果隐藏着的一些未被发现的缺陷或问题,可被人利用,人们 ...
- 树莓派Raspbian系统格式化挂载硬盘
1.查看树莓派系统挂载的储存设备 使用工具查看系统识别到的硬盘设备,命令: fdisk -l /dev/sda 和 /dev/sdb 分别是两块硬盘. 2.修改硬盘分区 Linux和windows一 ...
- Autofac在.NetCore 下的使用
在工作开发中很多地方都会使用到接口模式开发,这里就会用到依赖注入,.NetCore目前是自带的 ,在 Startup.cs中的 ConfigureServices方法中增加 public void C ...
- JSON C# Class Generator
http://www.xamasoft.com/json-class-generator/ JsonHelper.cs using System; using System.Collections.G ...
- Widget Size and Position !!!!!!!!!!!!!!!!!!
https://medium.com/@diegoveloper/flutter-widget-size-and-position-b0a9ffed9407 I have read many ques ...
- Ubuntu linux安装完成后隐藏linux磁盘挂载点
方案1 打开注册表 , 找到这个位置: 计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explore ...
- Mongodb 学习笔记(三) .net core SDK
首先添加 Nuget包 MongoDB.Driver 创建一个Model. public class Student { public ObjectId _id { get; set; } publ ...
- Tomcat - 控制台乱码
1.找到${CATALINA_HOME}/conf/logging.properties 2.找到java.util.logging.ConsoleHandler.encoding = UTF-8 修 ...
- Jmeter学习笔记(十五)——常用的4种参数化方式
一.Jmeter参数化概念 当使用JMeter进行测试时,测试数据的准备是一项重要的工作.若要求每次迭代的数据不一样时,则需进行参数化,然后从参数化的文件中来读取测试数据. 参数化是自动化测试脚本的一 ...