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

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.zking</groupId>
<artifactId>struts</artifactId>
<packaging>war</packaging>
<version>0.0.-SNAPSHOT</version>
<name>struts Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.</version>
</dependency>
<!-- 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.</version>
</dependency> <!-- 5.4、tomcat-jsp-api -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>8.0.</version>
</dependency> </dependencies>
<build>
<finalName>struts</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

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

pageTag

package com.jt.tag;

import java.io.IOException;
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.jt.util.PageBean; public class PageTag extends BodyTagSupport { private static final long serialVersionUID = -553969530960334073L; private PageBean pageBean; public PageBean getPageBean() {
return pageBean;
} public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
@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() {
// TODO Auto-generated method stub
StringBuilder sb=new StringBuilder();
//拼接下一次发送请求所要提交得隐藏得form表单
sb.append(" <form id='pageBeanForm' action='"+pageBean.getUrl()+"' method='post'> ");
sb.append(" <input type='hidden' name='page'> ");
Map<String, String[]> paMap = pageBean.getPaMap();
if(paMap!=null&&paMap.size()>) {
Set<Entry<String, String[]>> entrySet = paMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
//上一次请求可能携带页码namepage得参数,但是改参数在前面以及单独赋值
//为什么要单独赋值?因为上一次请求时第一页得数据,下一次可能时第二页,以后这前后请求page对应得值不一样,需要单独赋值
if(!"page".equals(entry.getKey())) {
// <input type='hidden' name='hobby' value='1'>
// <input type='hidden' name='hobby' value='2'>
//[1,2]
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(" 每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页&nbsp;&nbsp;<a ");
sb.append(" href='javascript:gotoPage(1)'>首页</a>&nbsp;&nbsp;<a");
sb.append(" href='javascript:gotoPage("+pageBean.getpreviousPage()+")'>上一页</a>&nbsp;&nbsp;<a");
sb.append(" href='javascript:gotoPage("+pageBean.getNextPage()+")'>下一页</a>&nbsp;&nbsp;<a");
sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页</a>&nbsp;&nbsp;<input type='text'");
sb.append(" id='skipPage'");
sb.append(" style='text-align: center; font-size: 12px; width: 50px;'>&nbsp;&nbsp;<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)>"+pageBean.getMaxPage()+"){");
sb.append(" alert('请输入1~N的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append(" }");
sb.append(" </script>"); return sb.toString();
}
}

z.tld

  <tag>
<!-- 标签库中的标签(类似c:set c:out的定义) -->
<name>page</name>
<!-- 是标签运行具体代码,也是助手类,下面填写的是助手类的全路径名 -->
<tag-class>com.jt.tag.PageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<!-- 该标签的属性 -->
<name>pageBean</name>
<!-- 该属性是否必填 -->
<required>true</required>
<!-- 是否支持表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>encodingFiter</filter-name>
<filter-class>com.jt.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>

3、dao层去访问数据

实体类

Clazz

package com.jt.entity;

public class Clazz {

    private int cid;
private String cname;
private String cteacher;
private String pic="图片暂定";
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCteacher() {
return cteacher;
}
public void setCteacher(String cteacher) {
this.cteacher = cteacher;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
@Override
public String toString() {
return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]";
} }

Clazzdao

package com.jt.dao;

import java.sql.SQLException;
import java.util.List; import org.apache.commons.lang3.StringUtils; import com.jt.entity.Clazz;
import com.jt.util.BaseDao;
import com.jt.util.PageBean; public class ClazzDao extends BaseDao<Clazz> { public List<Clazz> 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!=) {
sql+="and cid ="+cid;
}
if(StringUtils.isNoneBlank(cname)) {
sql+=" and cname like '%"+cname+"%'";
}
return super.executeQuery(sql,Clazz.class, pageBean);
}
public int add(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
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 {
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 {
String sql="update t_struts_class set cname=?,cteacher=?,pic=?";
return super.executeupdate(sql, new String[] {"cname","cteacher","pic","cid"}, clazz);
}
}

4、web层去调用dao层给前台返回数据

ClazzAction.java

package com.jt.web;

import java.sql.SQLException;
import java.util.List; import com.jt.dao.ClazzDao;
import com.jt.entity.Clazz;
import com.jt.util.BaseAction;
import com.jt.util.PageBean;
import com.opensymphony.xwork2.ModelDriven; public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{ private Clazz clz=new Clazz();
private ClazzDao clzDao=new ClazzDao();
public String list(){
PageBean pageBean =new PageBean();
pageBean.setRequest(request);
try {
List<Clazz> list = this.clzDao.list(clz, pageBean);
request.setAttribute("clzList",list);
request.setAttribute("pageBean",pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list"; }
/**
* 跳转编辑页面 (新增修改页面)
* @return
*/
public String preSave() {
if(clz.getCid()!=) {
try {
this.result= this.clzDao.list(clz, null).get(); } catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "preSave"; }
public String add() {
try {
this.code= this.clzDao.add(clz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList"; }
public String edit() {
try {
this.clzDao.edit(clz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList"; }
public String del() {
try {
this.clzDao.del(clz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList"; } @Override
public Clazz getModel() {
// TODO Auto-generated method stub
return clz;
}
}

5、在struts_sy.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="sy" extends="base" > <action name="/hello_*" class="com.jt.HelloAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
<action name="/clz_*" class="com.jt.web.ClazzAction" method="{1}">
<result name="list">/clzList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
</action>
</package>
</struts>

6、写jsp

clzList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="z" uri="/jt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>小说目录</h2>
<br> <form action="${pageContext.request.contextPath}/sy/clz_list.action"
method="post">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
</form>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action">增加</a>
<table border="" width="100%">
<tr>
<td>编号</td>
<td>班级名称</td>
<td>老师</td>
<td>班级图片</td>
</tr>
<c:forEach items="${clzList }" var="b">
<tr>
<td>${b.cid }</td>
<td>${b.cname }</td>
<td>${b.cteacher }</td>
<td>${b.pic }</td>
<td><a
href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a>&nbsp;&nbsp;
<a
href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除</a>&nbsp;
<a
href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">图片上传</a>&nbsp;
</td>
</tr>
</c:forEach>
</table>
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>

clzEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}${result.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post">
cid:<input type="text" value="${result.cid }" name="cid"><br>
cname:<input type="text" value="${result.cname }" name="cname"><br>
cteacher:<input type="text" value="${result.cteacher }" name="cteacher"><br>
<input type="submit" value="ok">
</form>
</body>
</html>

Struts的增删改查的更多相关文章

  1. SSH(Struts 2.3.31 + Spring 4.1.6 + Hibernate 5.0.12 + Ajax)框架整合实现简单的增删改查(包含分页,Ajax 无刷新验证该用户是否存在)

    软件152 余建强 该文将以员工.部门两表带领大家进入SSH的整合教程: 源码下载:http://download.csdn.net/detail/qq_35318576/9877235 SSH 整合 ...

  2. SSHE框架整合(增删改查)

    1.前期准备:jar包(c3p0.jdbc ,各个框架) web.xml文件:spring的   转码的,和Struts2的过滤器 <?xml version="1.0" e ...

  3. SSH登录与增删改查demo详解+源代码

    点击下载,测试绝对可用SSH整合框架登录加增删改查demo 下载地址:http://download.csdn.net/detail/qq_33599520/9784679   一.框架概述 spri ...

  4. struts2增删改查---layer---iframe层

    在这里写一下struts2中的简单的增删改查 struts.xml中的配置 <?xml version="1.0" encoding="UTF-8" ?& ...

  5. struts2增删改查---layer---iframe层---通配符---国际化

    在前一篇文章的基础上,修改一部分即可(在此只是简单介绍) struts.xml页面 在原来的基础之上 action的name="*_*"  class="包名.{1}&q ...

  6. Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合

    前言        转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...

  7. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...

  8. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  9. IDEA搭建SSM实现登录、注册,数据增删改查功能

     本博文的源代码:百度云盘/java/java实例/SSM实例/SSM实现登录注册,增删改查/IDEA搭建SSM实现登录,注册,增删改查功能.zip 搭建空的Maven项目 使用Intellij id ...

随机推荐

  1. Shell—输入输出重定向

    大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准输出,默 ...

  2. Linux方案级ROM/RAM优化记录

    关键词:readelf.bloat-o-meter.graph-size.totalram_pages.reserved.meminfo.PSS.procrank.maps等等. 根据项目的需求,进行 ...

  3. The 2017 ACM-ICPC Asia Shenyang Regional Contest

    传送门 F - Heron and His Triangle 直接打表找到规律\(f_i=4f_{i-1}+f_{i-2}\),然后大数预处理一下,对于询问直接输出就行. Code #include ...

  4. LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters

    地址 https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/ 题目描述你将会得到一份单词表 words,一个字母 ...

  5. #undef常用法

    Copy from C语言中#undef的语法是: #undef 标识符 用来将前面定义的宏标识符取消定义. 整理了如下几种常见用法,如果以后发现其他的再添加进来. 1.在一个程序块中用完宏定义后,为 ...

  6. Python解释器和Python集成环境小结

    目录 一.执行Python程序的两种方式 1.1 交互式 1.2 命令行式 二.执行Python程序的两种IDE 2.1 Pycharm 2.2 Jupyter 一.执行Python程序的两种方式 1 ...

  7. .NET Core 序列化对象输出字节数大小比较

    写代码验证了一下 .NET Core 中序列化对象输出字节数大小,.NET Core 版本是 3.0.100-preview8-013656 ,对象属性使用了 Guid 与 DateTime 类型,胜 ...

  8. dva+umi+antd项目从搭建到使用(没有剖验证,不知道在说i什么)

    先创建一个新项目,具体步骤请参考https://www.cnblogs.com/darkbluelove/p/11338309.html 一.添加document.ejs文件(参考文档:https:/ ...

  9. angularjs中使用锚点,angular路由导致锚点失效的两种解决方案

     壹 ❀ 引 公司新项目开发中,首页要做个楼层导航效果(如下图),要求能点击图标对应跳到楼层即可,因为不需要跳转过度动画,也要求最好别用JQ,想着原生js操作dom计算top的兼容性,想着用锚点实现算 ...

  10. RMAN详细教程(四):备份脚本实战操作

    RMAN详细教程(一):基本命令代码 RMAN详细教程(二):备份.检查.维护.恢复 RMAN详细教程(三):备份脚本的组件和注释 RMAN详细教程(四):备份脚本实战操作 1.为了安全起见,先将数据 ...