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 ...
随机推荐
- SpringCloud之Ribbon负载均衡及Feign消费者调用服务
目的: 微服务调用Ribbon Ribbon负载均衡 Feign简介及应用 微服务调用Ribbon Ribbon简介 1. 负载均衡框架,支持可插拔式的负载均衡规则 2. 支持多种协议,如HTTP.U ...
- Spring Cloud Alibaba学习笔记(18) - Spring Cloud Gateway 内置的过滤器工厂
参考:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-clou ...
- MVC-10HTML助手
HTML帮助器用于修改HTML输出. HTML帮助器 通过MVC,HTML帮助器类似于传统的ASP.NET Web Form控件. 类似ASP.NET中的web form控件,HTML帮助器用于修改H ...
- java之servlet之文件下载
1.在页面中,可以直接通过超链接来下载: a) 如果浏览器能够打开该文件,那么直接在浏览器中显示---不是想要的效果 b) 任何人都能下载,不能进行权限控制 2.通过servlet来进行下载,在ser ...
- django-nginx与uwsgi项目部署
uwsgi是提供动态服务的 nginx反向代理 在项目中创建一个settings.py的副本.我这里重命名为copy_settings.py,将配置文件中的DEBUG=False 修改项目下wsgi. ...
- QLineEdit 按键Tab键时 显示历史记录
#LineEdit添加历史记录功能,按下回车添加至历史中 class LineEditWithHistory(QtWidgets.QLineEdit): def __init__(self, pare ...
- 常用的HTTP响应头
响应头 说明 示例 状态 Access-Control-Allow-Origin 指定哪些网站可以跨域源资源共享 Access-Control-Allow-Origin: * 临时 Accept-Pa ...
- GitHub上传文件夹
1.输入自己的用户名和邮箱 为注册GitHub账号时所用的用户名和邮箱;我的用户名为“1997ST2016”,邮箱为“1324971964@qq.com ”. $ git config --globa ...
- MySql时区修改
1.查看当前时间 > select curtime(); #或select now()也可以+-----------+| curtime() |+-----------+| 15:18:10 | ...
- c实现二叉树
C实现二叉树 简单说明 实现了先序遍历.中序遍历.后序遍历.搜索 本来想着和平衡二叉树一起放上来的,但是花了一个下午也只是把平衡二叉树原理弄懂和左右旋代码实现,最难的平衡左/右旋还没弄,就不显摆了,就 ...