利用struts完成增删改查
思路:
- 导入相关的pom依赖(struts、自定义标签库的依赖)以及创建要用的数据库
<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.0</modelVersion>
<groupId>com.zking</groupId>
<artifactId>web_struts</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>web_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.44</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</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.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>
</dependencies>
<build>
<finalName>web_struts</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 分页的tag类导入、z.tld、完成web.xml的配置
2.1 导入相关的工具类(之前mvc和分页都有写的util包和tag包)
2.2 配置tld文件
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/xiefuzhen</uri>
<tag>
<!-- 填写的是标签库中的标签名 -->
<name>page</name>
<!-- 标签对应后台助手类 -->
<tag-class>com.xfz.three.tag.PageTag</tag-class>
<!-- 标签类别 -->
<body-content>JSP</body-content>
<attribute>
<!-- 自定义标签中的属性 -->
<name>pageBean</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达式EL表达式/ognl表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
2.3 配置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.xfz.three.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
- dao层去访问数据
3.1 先写一个实体类
package com.xfz.three.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 + "]";
}
public Clazz(int cid, String cname, String cteacher, String pic) {
super();
this.cid = cid;
this.cname = cname;
this.cteacher = cteacher;
this.pic = pic;
}
public Clazz() {
super();
}
}
3.2 写dao层的四个方法
package com.xfz.three.dao;
import java.sql.SQLException;
import java.util.List;
import com.xfz.three.entity.Clazz;
import com.xfz.three.util.BaseDao;
import com.xfz.three.util.PageBean;
import com.xfz.three.util.StringUtils;
/**
* @author ld
* 操作数据库
*/
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(StringUtils.isNotBlank(cname)) {
sql+=" and cname like '%"+cname+"%'";
}
if(cid!=0) {
sql+=" and cid = "+cid;
}
return super.executeQuery(sql, Clazz.class, pageBean);
}
/**
* @param clazz
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* 班级信息的新增
*/
public int add(Clazz clazz) throws IllegalArgumentException, IllegalAccessException, SQLException {
String sql="insert into t_struts_class values(?,?,?,?)";
return super.executeUpdate(sql, new String[] {"cid","cname","cteacher","pic"}, clazz);
}
/**
* @param clazz
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* 班级信息的删除
*/
public int del(Clazz clazz) throws IllegalArgumentException, IllegalAccessException, SQLException {
String sql="delete from t_struts_class where cid=?";
return super.executeUpdate(sql, new String[] {"cid"}, clazz);
}
/**
* @param clazz
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* 班级信息的修改
*/
public int edit(Clazz clazz) throws IllegalArgumentException, IllegalAccessException, SQLException {
String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid=?";
return super.executeUpdate(sql, new String[] {"cname","cteacher","pic","cid"}, clazz);
}
}
- web层去调用dao层给前台返回数据
package com.xfz.three.web;
import java.sql.SQLException;
import com.opensymphony.xwork2.ModelDriven;
import com.xfz.three.dao.ClazzDao;
import com.xfz.three.entity.Clazz;
import com.xfz.three.util.BaseAction;
import com.xfz.three.util.PageBean;
public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
private ClazzDao clazzDao=new ClazzDao();
private Clazz clazz=new Clazz();
/**
* @return
* 查看班级信息
*/
public String list() {
PageBean pageBean=new PageBean();
pageBean.setRequest(request);
try {
this.result=clazzDao.list(clazz, pageBean);//result值栈的赋值
request.setAttribute("pageBean", pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "list";
}
/**
* @return
* 跳转编辑页面(新增编辑是同一页面)
*/
public String preSave() {
//跳转新增页面和编辑页面的区别在于跳转编辑页面的时候要传cid到后台进行查询,并且将查询到的内容回显
int cid=clazz.getCid();
if(cid!=0) {
try {
this.result=clazzDao.list(clazz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
}
return "preSave";
}
/**
* @return
* 新增班级信息
*/
public String add() {
try {
this.code=this.clazzDao.add(clazz);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**
* @return
* 删除班级信息
*/
public String del() {
try {
this.code=this.clazzDao.del(clazz);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**
* @return
* 修改班级信息
*/
public String edit() {
try {
this.code=this.clazzDao.edit(clazz);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Clazz getModel() {
return clazz;
}
}
- 在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>
<!--
相对于mvc的差异性
package:用来将一类子控制器进行分类
http://localhost:8080/web_struts/sy/user_add.action
这个地址中/sy对应的namespace="/sy"
extends:包的继承
*的含义:代表任意方法,只要前台浏览器匹配/user_*这一个,那么user_add中,*代表了add
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xfz.one.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<action name="demo_*" class="com.xfz.one.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="tomcat_*" class="com.xfz.one.web.TomcatAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="ognl_*" class="com.xfz.two.Demo7" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="clazz_*" class="com.xfz.three.web.ClazzAction" method="{1}">
<result name="list">/clazzList.jsp</result>
<result name="preSave">/clazzEdit.jsp</result>
<result name="toList" type="redirectAction">clazz_list</result>
</action>
</package>
</struts>
- 写jsp
6.1 显示全部的页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/xiefuzhen" prefix="fz"%>
<!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/clazz_list.action"
method="post">
<!-- <input type="hidden" name="rows" value="20"> -->
<!-- <input type="hidden" name="pagination" value="false"> -->
书名:<input type="text" name="bname">
<!-- 书籍类别: <input type="checkbox" name="bookType" value="1">玄幻
<input type="checkbox" name="bookType" value="2">都市
<input type="checkbox" name="bookType" value="3">言情 -->
<input type="submit" value="确定">
</form>
<a href="${pageContext.request.contextPath}/sy/clazz_preSave.action">新增</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>班级名称</td>
<td>教员</td>
<td>图片</td>
<td>操作</td>
</tr>
<c:forEach items="${result}" var="c">
<tr>
<td>${c.cid }</td>
<td>${c.cname }</td>
<td>${c.cteacher }</td>
<td>${c.pic }</td>
<td>
<a href="${pageContext.request.contextPath}/sy/clazz_preSave.action?cid=${c.cid}">修改</a>
<a href="${pageContext.request.contextPath}/sy/clazz_del.action?cid=${c.cid}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<fz:page pageBean="${pageBean}"></fz:page>
</body>
</html>
6.2 新增修改页面
<%@ 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></title>
</head>
<body>
<form action="${pageContext.request.contextPath}${result.cname==null ? '/sy/clazz_add.action' :'/sy/clazz_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="hidden" name="pic" value="${result.pic}">
<input type="submit" value="ok">
</form>
</body>
</html>
显示结果
增加、查询:
修改:
删除: