利用struts进行增删查改

struts的增删查改


今天给大家带来一个利用struts进行增删查改的demo
第一步:导入相关的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.wt</groupId>
  <artifactId>struts</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-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.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>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的配置
先进行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.wt.crud.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>

进行z.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>zking 1.1 core library</description>
  <display-name>zking core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>c</short-name>
  <uri>/zking</uri>

  
 
  
  
   <tag>
  <!--标签库中的标签名  -->
    <name>page</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>com.wt.crud.tag.PageTag</tag-class>
   	<!-- JSP -->
    <body-content>JSP</body-content>
    <attribute>
    <!-- 属性 -->
        <name>pageBean</name>
        <!--属性值是否必填  -->
        <required>true</required>
        <!-- 是否支持表达式 -->
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
 
</taglib>

第三步;dao层去访问数据
首先我们先看实体类

package com.wt.crud.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 + "]";
	}
	
}

其次我以前写的通用分页,也可以用在这里;

package com.wt.crud.dao;

import java.sql.SQLException;
import java.util.List;

import com.wt.crud.entity.Clazz;
import com.wt.crud.util.BaseDao;
import com.wt.crud.util.PageBean;
import com.wt.crud.util.StringUtils;

public class ClazzDao extends BaseDao<Clazz> {
	
	/**
	 * 查询所有  查单个数据
	 * @param clz
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Clazz> list(Clazz clz,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql ="select *from t_struts_class where true";
		String cname = clz.getCname();
		int cid = clz.getCid();
		if(cid!=0) {
			sql +=" and cid="+cid;
		}
		if(StringUtils.isNotBlank(cname)) {
			sql +=" and cname like '%"+cname+"%'";
		}
		return super.executeQuery(sql, Clazz.class, pageBean);
	}
	
	/**增加
	 * @param clz
	 * @return
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public int add(Clazz clz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql = "insert into t_struts_class values(?,?,?,?)";
		return super.executeUpdate(clz, sql, new String [] {"cid","cname","cteacher","pic"});
	}
	
	
	/**
	 * 编辑
	 * @param clz
	 * @return
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public int edit(Clazz clz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql = "update  t_struts_class set cname=?,cteacher=?,pic=? where cid=?";
		return super.executeUpdate(clz, sql, new String [] {"cname","cteacher","pic","cid"});
	}
	
	/**
	 * 删除
	 * @param clz
	 * @return
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public int del(Clazz clz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql = "delete from t_struts_class where cid=?";
		return super.executeUpdate(clz, sql, new String [] {"cid"});
	}
	
}

第四步:web层去调用dao层给前台返回数据
也就是action这个类

package com.wt.crud.web;

import java.sql.SQLException;
import java.util.List;

import com.opensymphony.xwork2.ModelDriven;
import com.wt.crud.dao.ClazzDao;
import com.wt.crud.entity.Clazz;
import com.wt.crud.util.PageBean;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
	
	private ClazzDao clzdao = new ClazzDao();
	private Clazz clz = new Clazz();
	
	
	
	
	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) {
			e.printStackTrace();
		}
		return "list";
	}

	
	/**
	 * 跳转编辑界面的公用方法
	 * @return
	 */
	public String preSave() {
		if(clz.getCid()!=0) {
			try {
				Clazz c = this.clzdao.list(clz, null).get(0);
				request.setAttribute("clz", c);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				e.printStackTrace();
			}
		}
		return "preSave";
	}
	
	/**
	 * 新增
	 * @return
	 */
	public String add() {
		try {
			result = this.clzdao.add(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		try {
			this.clzdao.del(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}


	/**
	 * 修改
	 * @return
	 */
	public String edit() {
		try {
			this.clzdao.edit(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}

	@Override
	public Clazz getModel() {
		return clz;
	}
}

第五步;在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" namespace="/sy">
		<action name="/demo_*" class="com.wt.web.HelloAction" method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		<action name="/stack_*" class="com.wt.test.DemoAction" method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="/clz_*" class="com.wt.crud.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>

最后一步就是编写我们的jsp界面了:
首先是首页

<%@ 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="/zking" prefix="z"%>
<!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>
	<%=System.currentTimeMillis() %>
	<br>

	<form action="${pageContext.request.contextPath}/sy/clz_list.action"
		method="post">
		班级名:<input type="text" name="cname"> <input type="submit"
			value="确定">
	</form>
	<a href="${pageContext.request.contextPath}/sy/clz_preSave.action">新增</a>
	<table border="1" width="100%">
		<tr>
			<td>编号</td>
			<td>班级</td> 
			<td>教员</td>
			<td>图片</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${clzList }" 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/clz_preSave.action?cid=${c.cid}">修改</a>&nbsp;&nbsp;
					<a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${c.cid}">删除</a>&nbsp;&nbsp;
				</td>
			</tr>
		</c:forEach>
		
	</table>

	<z:page pageBean="${pageBean }"></z:page>

	
</body>
</html>

然后就是编辑界面;

<%@ 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 id="bookForm" action="${pageContext.request.contextPath}${clz.cname==null ? '/sy/clz_list.action' : '/sy/clz_edit.action'}" method="post">
		cid:<input type="text" name="cid" value="${c.cid }"><br>
		cname:<input type="text" name="cname" value="${c.cname }"><br>
		cteacher:<input type="text" name="price" value="${c.cteacher }"><br>
		<input type="submit" value="提交" ><br>
	</form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值