strust的CRUD

4 篇文章 0 订阅
2 篇文章 0 订阅

目标:使用strust对数据库表格进行增删改查

实现

需要的jar包依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>

    <!--添加jar包依赖-->
    <!--mysql-->
    <mysql.version>8.0.19</mysql.version>
    <!--5.其他-->
    <junit.version>4.12</junit.version>
    <servlet.version>4.0.0</servlet.version>
    <jackson.version>2.9.3</jackson.version>
    <jstl.version>1.2</jstl.version>
    <standard.version>1.1.2</standard.version>
    <tomcat-jsp-api.version>8.0.47</tomcat-jsp-api.version>
    <commons-beanutils.version>1.9.3</commons-beanutils.version>
    <dom4j.version>1.6.1</dom4j.version>
    <jaxen.version>1.1.6</jaxen.version>
	<struts2-core.version>2.5.13</struts2-core.version>
  </properties>
  
  <dependencies>
  	<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>${struts2-core.version}</version>
		</dependency>
    <dependency>
      <groupId>jaxen</groupId>
      <artifactId>jaxen</artifactId>
      <version>${jaxen.version}</version>
    </dependency>

    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>${dom4j.version}</version>
    </dependency>


    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>${commons-beanutils.version}</version>
    </dependency>

    <!--mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.version}</version>
    </dependency>

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>${standard.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-jsp-api</artifactId>
      <version>${tomcat-jsp-api.version}</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.10</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.belerweb</groupId>
      <artifactId>pinyin4j</artifactId>
      <version>2.5.0</version>
    </dependency>

    <dependency>
      <groupId>com.github.davidcarboni</groupId>
      <artifactId>encrypted-file-upload</artifactId>
      <version>2.1.0</version>
    </dependency>
  </dependencies>

由于重点在体现strust的使用,所以很多东西就用之前的,(例如之前的前端页面,帮助类,自定义标签)

a.实体类:

clz

package com.csf.entity;

public class Clz {
	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;
	}
	public Clz() {
		// TODO Auto-generated constructor stub
	}
	public Clz(int cid, String cname, String cteacher, String pic) {
		super();
		this.cid = cid;
		this.cname = cname;
		this.cteacher = cteacher;
		this.pic = pic;
	}
	@Override
	public String toString() {
		return "Clz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]";
	}
	
}

b.先写web层,根据需求写,这里继承BaseDao (在上一篇博客中有,所以省略了)

package com.csf.web;

import com.csf.dao.ClzDao;
import com.csf.entity.Clz;
import com.zking.util.BaseAction;
import com.zking.util.PageBean;

public class ClzAction extends BaseAction<Clz>{
	private Clz clz=new Clz();
	private ClzDao clzDao=new ClzDao();
	
	
/**
 * 查询班级列表
 * @return
 * @throws Exception
 */
	public String list() throws Exception {
	PageBean pageBean=new PageBean();
	pageBean.setRequest(req);
	this.result=this.clzDao.list(clz, pageBean);
	this.req.setAttribute("result", result);
	this.req.setAttribute("pageBean", pageBean);
		return LIST;
	}
	
/**
 * 跳转新增/修改界面
 * @return
 * @throws Exception
 */
	public String toEdit() throws Exception {
		int cid = clz.getCid();
		if(cid!=0) {
			this.result=this.clzDao.list(clz,null).get(0);
			this.req.setAttribute("result", result);
		}
		return TOEDIT;
	}
	
/**
 * 往数据库中新增数据
 * @return
 * @throws Exception
 */
	public String add() throws Exception {
	this.clzDao.add(clz);
		return TOLIST;
	}
	
	/**
	 * 删除数据
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		this.clzDao.del(clz);
		return TOLIST;
	}
	
	/**
	 * 修改数据
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		this.clzDao.edit(clz);
		return TOLIST;
	}
	
	
	@Override
	public Clz getModel() {
		// TODO Auto-generated method stub
		return clz;
	}

}

c.再配置到strust-sy.xml中:

<package name="sy" extends="base" namespace="/sy">
		<!-- 1.默认forward标签体对应的转发页面 
			 2.action:标签体对应的转发的action后台方法 
			 3.redirect标签体对应的重定向 
			 4.redirectAction标签体对应的重定向action后台方法 -->
		<action name="/clz_*" class="com.csf.web.ClzAction" method="{1}">
			<result name="list">/clzList.jsp</result>
			<result name="toEdit">/clzEdit.jsp</result>
			<result name="toList" type="redirectAction">/clz_list</result>
		</action>

	</package>

d.编写前端页面,clzList:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>	
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>	
<!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">
<link
	href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
	rel="stylesheet">
<script
	src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>列表</title>
<style type="text/css">
.page-item input {
	padding: 0;
	width: 40px;
	height: 100%;
	text-align: center;
	margin: 0 6px;
}

.page-item input, .page-item b {
	line-height: 38px;
	float: left;
	font-weight: 400;
}

.page-item.go-input {
	margin: 0 10px;
}
</style>
</head>
<body>
	<form class="form-inline"
		action="${pageContext.request.contextPath }/sy/clz_list.action" method="post">
		<div class="form-group mb-2">
			<input type="text" class="form-control-plaintext" name="title"
				placeholder="请输入名称">
<!-- 			<input name="rows" value="20" type="hidden"> -->
<!-- 不想分页 -->
	
		</div>
		<button type="submit" class="btn btn-primary mb-2">查询</button>
		<a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/sy/clz_toEdit.action">新增</a>
	</form>

	<table class="table table-striped bg-success">
		<thead>
			<tr>
				<th scope="col">ID</th>
				<th scope="col">名字</th>
				<th scope="col">教员</th>
				<th scope="col">图片</th>
				<th scope="col">操作</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach  var="b" items="${result }">
			<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_toEdit.action?cid=${b.cid}">修改</a>
					<a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${b.cid}">删除</a>
				</td>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<!-- 这一行代码就相当于前面分页需求前端的几十行了 -->
	<z:page pageBean="${pageBean }"></z:page>

</body>
</html>

 clzEdit:

<%@ 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 }/sy/clz_${empty result? 'add' : 'edit'}.action" method="post">
id:<input type="text" name="cid" value="${result.cid }"><br>
title:<input type="text" name="cname" value="${result.cname }"><br>
cteacher:<input type="text" name="cteacher" value="${result.cteacher}"><br>
<input type="submit">
</form>
</body>
</html>

效果展示

展示数据页面:

 增加页面:

 修改页面:

 删除比较简单,不做演示

strust与mvc框架用法相似不同的在与配置文件和web层的方法使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无感_K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值