博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts的增删改查
阅读量:5264 次
发布时间:2019-06-14

本文共 10381 字,大约阅读时间需要 34 分钟。

1、导入相关的pom依赖(struts、自定义标签库的依赖)

<!-- 5.3、jstl、standard -->

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

<!-- 5.4、tomcat-jsp-api -->

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>8.0.47</version>
</dependency>

2、分页的tag类导入、z.tld、完成web.xml的配置

pageTag.java

package com.huang.crud.tag;import java.io.IOException;import java.util.Map;import java.util.Map.Entry;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyTagSupport;import com.huang.crud.util.PageBean;public class PageTag extends BodyTagSupport {        private static final long serialVersionUID = -8476740758233598045L;    private PageBean pageBean;        @Override    public int doStartTag() throws JspException {        JspWriter out = pageContext.getOut();        try {            out.write(toHTML());        } catch (IOException e) {            e.printStackTrace();        }        return super.doStartTag();    }    public String toHTML() {//    pageBean=new PageBean();    StringBuffer sb=new StringBuffer();    //下一次请求提交到后台的表单html代码拼接    sb.append("
"); sb.append(""); sb.append(""); sb.append("
"); Map
paramMap = pageBean.getParameterMap(); if(paramMap!=null&&paramMap.size()>0) { for(Entry
entry:paramMap.entrySet()) { if(!"page".equals(entry.getKey())) { for(String val:entry.getValue()) { sb.append("
value='"+val+"'"); } } } } sb.append("
"); sb.append(""); //分页条html代码拼接 sb.append("
"); sb.append(" 每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页  "); sb.append("
首页  
上一页  
下一页  
尾页  
  
Go"); sb.append("
"); //分页所需要调用的js代码 sb.append("
"); return sb.toString(); } public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; }}

z.tid

dj 1.1 core library
dj core
1.1
d
/huang
page
com.huang.crud.tag.PageTag
JSP
pageBean
true
true

web.xml

Archetype Created Web Application
encodingFiter
com.huang.crud.util.EncodingFiter
encodingFiter
/*
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
*.action

 

3、dao层去访问数据

ClazzDao.java

package com.huang.crud.dao;import java.sql.SQLException;import java.util.List;import com.huang.crud.entity.Clazz;import com.huang.crud.util.BaseDao;import com.huang.crud.util.PageBean;import com.huang.crud.util.StringUtils;public class ClazzDao extends BaseDao
{ public List
list(Clazz clazz,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ String sql= "select * from t_struts_class where true "; String cname= clazz.getCname(); int cid=clazz.getCid(); if(cid!=0) { sql +=" and cid ="+cid; } if(StringUtils.isNotBlank(cname)) { sql +=" and cname like '%"+cname+"%'"; } return super.executeQuery(sql,Clazz.class , pageBean); } public int add(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException { String sql= "insert into t_struts_class values(?,?,?,?)"; return super.executeUpdate(sql,new String[] {"cid","cname","cteacher","pic"}, clazz); } public int del(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException { String sql= "delete from t_struts_class where cid=?"; return super.executeUpdate(sql,new String[] {"cid"}, clazz); } public int edit(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException { String sql= "update t_struts_class set cname=?,cteacher=?,pic=? where cid=?"; return super.executeUpdate(sql,new String[] {"cname","cteacher","pic","cid"}, clazz); }}

BaseDao.java

package com.huang.crud.util;import java.lang.reflect.Field;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.huang.crud.entity.Clazz;public class BaseDao
{ /** * T=Student.class * * @param sql 决定查询哪张表的数据 * @param clz 查询出来的数据封装到哪个实体类中 * @param pageBean 决定是否分页 * @return * @throws * @throws InstantiationException * @throws SQLException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InstantiationException */ public List
executeQuery(String sql,Class clz,PageBean pageBean) throws SQLException, IllegalArgumentException, IllegalAccessException, InstantiationException { List
ls=new ArrayList<>(); Connection con=DBAccess.getConnection(); PreparedStatement ps = null; ResultSet rs = null; if(pageBean!=null && pageBean.isPagination()) { //该分页了 String countSql=getCountSql(sql);//获取符合记录的行数的Sql语句 try { ps=con.prepareStatement(countSql); } catch (SQLException e) { e.printStackTrace(); } try { rs=ps.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } if(rs.next()) { pageBean.setTotal(rs.getLong(1)+"");//给pageBean的总页数赋值 } String pageSql=getPageSql(sql,pageBean);//拼接上符合条件的分页查询sql语句 ps=con.prepareStatement(pageSql); rs=ps.executeQuery(); } else { ps = con.prepareStatement(sql); rs = ps.executeQuery(); } try { while (rs.next()) { // ls.add(new Book(rs.getInt("bid"), // rs.getString("bname"), // rs.getFloat("price"))); /** * 1、创建了一个book对象 * 2、从ResultSet结果集中获取值放入Book对象属性中 * 2.1获取到book的属性对象 * 2.2给属性对象赋值 * 3、将已经有值的Book对象放进list集合中 * */ T t = (T) clz.newInstance(); Field[] fields = clz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); field.set(t, rs.getObject(field.getName())); } ls.add(t); } } finally { DBAccess.close(con,ps,rs); } return ls; } /** * 将原生sql拼接出符合条件的某一页的数据查询 * @param sql * @return */ private String getPageSql(String sql,PageBean pageBean) { return sql + " limit "+pageBean.getStartIndex()+","+pageBean.getRows(); } /** * 用原生sql拼接出查询符合条件的记录数 * @param sql * @return */ private String getCountSql(String sql) { return "select count(1) from ("+sql+") t"; } /** * 通用的增删改方法 * @param sql 增删改的sql语句 * @param attrs ?所代表的实体类的属性 * @param t 实体类的实例 * @return * @throws SQLException * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ public int executeUpdate(String sql, String[] attrs, T t) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException { Connection con = DBAccess.getConnection(); PreparedStatement pst = con.prepareStatement(sql); for (int i = 0; i < attrs.length; i++) { Field field = t.getClass().getDeclaredField(attrs[i]); field.setAccessible(true); pst.setObject(i+1, field.get(t)); } return pst.executeUpdate(); }}

 

4、在struts_sy.xml进行配置

struts_sy.xml

/clzList.jsp
/clzEdit.jsp
/clz_list

5、写jsp

clzList.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    <%@ taglib uri="/huang" prefix="d" %>
主界面

小说目录

书名:
增加
编号 班级名称 班级图片 操作
${b.cid } ${b.cname } ${b.cteacher } ${b.pic } 修改  删除 文件上传

 

 

 

clzEdit.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here
cid:
cname:
cteacher:

 

运行结果:

 

转载于:https://www.cnblogs.com/bf6rc9qu/p/11267323.html

你可能感兴趣的文章
python常用模块之sys, os, random
查看>>
HDU 2548 A strange lift
查看>>
Linux服务器在外地,如何用eclipse连接hdfs
查看>>
react双组件传值和传参
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
android:scaleType属性
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>