J2EE--自定义mvc增删改查

一、配置依赖

1、将框架打成jar包,然后导入新工程,并且把框架的依赖jar包导入进去

将我们前面的写的代码打成一个jar包然后导入我们的项目

在这里插入图片描述
点击后我们在输入jar,选中java下的第一个选项
在这里插入图片描述
输入要放的地址,点击finish即可
在这里插入图片描述
这里我们的桌面就有了一个mvc.jar
在这里插入图片描述
然后再导入我们其他的依赖
在这里插入图片描述
导入我们的项目后基本的框架算是完成一半了

2、导入分页依赖

分页的tag
在这里插入图片描述
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>x</short-name>
  <uri>http://jsp.xlb.like.cn</uri>

  <validator>
    <description>
        Provides core validation features for JSTL tags.
    </description>
    <validator-class>
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    </validator-class>
  </validator>



	 <tag>
  	<!-- 代表标签库的名字 -->
    <name>page</name>
    <!-- 改标签对应的助手类的全路径名 -->
    <tag-class>com.xlb.tag.PageTag</tag-class>
  	<!-- 代表一个JSP标签(不能省) -->
    <body-content>JSP</body-content>
    <!-- 该自定义JSP标签的属性名称 -->
    <attribute>
        <name>pageBean</name>
        <required>true</required> 
      	<rtexprvalue>true</rtexprvalue> 
    </attribute>
  </tag>
	
  
</taglib>
**

3、写入底层代码

实体类book

package com.xlb.entity;

/**
 * 书籍
 * @author 波哥
 *
 * 2022年6月29日 上午8:46:33
 */
public class Book {
	private int bid;
	private String bname;
	private float price;
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public Book() {
		// TODO Auto-generated constructor stub
	}
	public Book(int bid, String bname, float price) {
		super();
		this.bid = bid;
		this.bname = bname;
		this.price = price;
	}
	public Book(String bname, float price) {
		this.bname = bname;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
	}
	
	
	
}

二、通用增删改查

因为我们通用的查询前面博客已经有写过了,所有现在我们封装一个通用的增删改
BeseDao.java里面封装一个通用增删改

/**
	 * 通用增删改
	 * @param sql sql语句
	 * @param t 对象
	 * @param str 占位符
	 * @return 影响行数
	 * @throws Exception
	 */
	public int executeUpdate(String sql,T t,String[] str) throws Exception{
		Connection con = DBAccess.getConnection();
		PreparedStatement ps = con.prepareStatement(sql);
		for (int i = 0; i < str.length; i++) {
			//拿到数组里面所有对象
			Field f = t.getClass().getDeclaredField(str[i]);
			//打开访问权限
			f.setAccessible(true);
			//拿到属性值
			ps.setObject(i+1, f.get(t));
		}
		return ps.executeUpdate();
	}
	

然后我们写一个BookAction来写入增删改查

package com.xlb.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xlb.dao.BookDao;
import com.xlb.entity.Book;
import com.xlb.framework.ActionSupport;
import com.xlb.framework.ModelDriven;
import com.xlb.util.PageBean;

/**
 * book增删改查
 * @author 波哥
 *
 * 2022年6月29日 上午9:51:56
 */
public class BookAction extends ActionSupport implements ModelDriven<Book>{

	private Book b = new Book();
	private BookDao bd=new BookDao();
	
	/**
	 * 封装方法
	 */
	@Override
	public Book getModel() {
		return b;
	}
	
	
	/**
	 * 增
	 * @param req
	 * @param resp
	 * @return
	 */
	public String add(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bd.add(b);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//toList代表跳到查询界面
		return "toList";
	}
	
	/**
	 * 删除
	 * @param req
	 * @param resp
	 * @return
	 */
	public String del(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bd.del(b);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//toList代表跳到查询界面
		return "toList";
	}
	
	/**
	 * 修改
	 * @param req
	 * @param resp
	 * @return
	 */
	public String edit(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bd.edit(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//toList代表跳到查询界面
		return "toList";
	}
	
	/**
	 * 查询
	 * @param req
	 * @param resp
	 * @return
	 */
	public String list(HttpServletRequest req, HttpServletResponse resp) {
		try {
			PageBean pagebean = new PageBean();
			//初始化
			pagebean.setRequest(req);
			List<Book> list = bd.list(b, pagebean);
			req.setAttribute("list", list);
			req.setAttribute("pageBean", pagebean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//执行查询展示
		return "list";
	}
	
	/**
	 * 跳转到新增或修改界面
	 * @param req
	 * @param resp
	 * @return
	 */
	public String preEdit(HttpServletRequest req, HttpServletResponse resp) {
		try {
			//拿到bid,当点增加bid=0
			int bid = b.getBid();
			if(bid != 0) {
				List<Book> list = bd.list(b, null);//只查一条,因为id唯一
				req.setAttribute("b", list);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//toEdit代表跳到编辑界面
		return "toEdit";
	}
	
}

使用测试类测试BookDaoTest.java

package com.xlb.dao;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.xlb.entity.Book;
import com.xlb.util.PageBean;

/**
 * 测试类
 * @author 波哥
 *
 * 2022年6月29日 上午9:14:03
 */
public class BookDaoTest {

	private PageBean pageBean=new PageBean();
	private BookDao bd=new BookDao();
	
	
	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	/**
	 * 查询
	 */
	@Test
	public void testListBookPageBean() {
		Book b = new Book();
		try {
			List<Book> list = bd.list(b, null);
			for (Book book : list) {
				System.out.println(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 增加
	 */
	@Test
	public void testAdd() {
		 Book b = new Book(232311, "sadas", 3921321);
		 try {
			bd.add(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改
	 */
	@Test
	public void testEdit() {
		Book b = new Book(232311, "欧阳修大战鸡对长", 238923);
		try {
			bd.edit(b);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除
	 */
	@Test
	public void testDel() {
		Book b = new Book(232311, "欧阳修大战鸡对长", 238923);
		try {
			bd.del(b);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

测试结果
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

三、写主界面功能

导入主界面
bookList.java

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.xlb.like.cn" prefix="x" %>	
<%@ 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 }/book.action?methodName=list" method="post">
		<div class="form-group mb-2">
			<input type="text" class="form-control-plaintext" name="bname" placeholder="请输入书籍名称">
		</div>
		<button  type="submit" class="btn btn-primary mb-2">查询</button>
		<a  href="${pageContext.request.contextPath }/book.action?methodName=preEdit" class="btn btn-primary mb-2">新增</a>
	</form>

	<table class="table table-striped">
		<thead>
			<tr>
				<th scope="col">书籍ID</th>
				<th scope="col">书籍名</th>
				<th scope="col">价格</th>
				<th scope="col">操作</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach items="${list}" var="b">
				<tr>
					<td>${b.bid}</td>
					<td>${b.bname}</td>
					<td>${b.price}</td>
					<td>
						<a href="${pageContext.request.contextPath }/book.action?methodName=preEdit&bid=${b.bid}">编辑</a>
						<a href="${pageContext.request.contextPath }/book.action?methodName=del&bid=${b.bid}">删除</a>
					</td>	
				</tr>
			</c:forEach>
		</tbody>
	</table>
	
	<x:page pageBean="${pageBean}"></x:page>
		
</body>
</html>

导入修改和增加和修改公共页面
bookEdit.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></title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/book.action?methodName=${empty b ? 'add' : 'edit' }" method="post">
		bid:<input type="text" name="bid" value="${b.bid}"/>
		bname:<input type="text" name="bname" value="${b.bname}"/>
		price:<input type="text" name="price" value="${b.price}"/>
		<input type="submit"/>	
	</form>
</body>
</html>

运行结果
主界面
在这里插入图片描述
模糊查询
在这里插入图片描述
增加
在这里插入图片描述
在这里插入图片描述
删除
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值