目录
2、将导出的jar包导入到现在所要使用的项目中并要将其他的jar包导入
一、搭建自定义mvc框架环境
1、将上一次写的框架导出成jar包
选择jar file
在这里可以选择导出的位置和给你的jar包命名
2、将导出的jar包导入到现在所要使用的项目中并要将其他的jar包导入
3、将之前的tag标签导入
4、引入通用分页之前的代码
二、基础的增删改
1、实体类
package com.shishirong.entity;
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(int bid, String bname, float price) {
super();
this.bid = bid;
this.bname = bname;
this.price = price;
}
public Book() {
super();
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
}
增加:
// 增
// public int add(Book book) throws Exception {
// Connection con = DBAccess.getConnection();
// String sql="insert into t_mvc_book values(?,?,?)";
// PreparedStatement pst = con.prepareStatement(sql);
// pst.setObject(1, book.getBid());
// pst.setObject(2, book.getBname());
// pst.setObject(3, book.getPrice());
// return pst.executeUpdate();
// }
删除:
// 删
// public int del(Book book) throws Exception {
// Connection con = DBAccess.getConnection();
// String sql = "delete from t_mvc_book where bid = ?";
// PreparedStatement pst = con.prepareStatement(sql);
// pst.setObject(1, book.getBid());
// return pst.executeUpdate();
// }
修改:
// 改
// public int edit(Book book) throws Exception {
// Connection con = DBAccess.getConnection();
// String sql = "update t_mvc_book set bname = ?,price = ? where bid = ?";
// PreparedStatement pst = con.prepareStatement(sql);
// pst.setObject(3, book.getBid());
// pst.setObject(1, book.getBname());
// pst.setObject(2, book.getPrice());
// return pst.executeUpdate();
// }
三、通用的增删改
public int executeUpdate(String sql,T t,String[] attrs) throws Exception {
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
// 将t 的某一个属性对应的值加到pst对象中
for (int i = 0; i < attrs.length; i++) {
Field f = t.getClass().getDeclaredField(attrs[i]);
f.setAccessible(true);
f.get(t);
pst.setObject(i + 1 , f.get(t));
}
// pst.setObject(2, book.getBname());
// pst.setObject(3, book.getPrice());
return pst.executeUpdate();
}
调用这个通用的增删改
package com.shishirong.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.shishirong.entity.Book;
import com.shishirong.util.BaseDao;
import com.shishirong.util.DBAccess;
import com.shishirong.util.PageBean;
import com.shishirong.util.StringUtils;
public class BookDao extends BaseDao<Book>{
//查询
public List<Book> list(Book book,PageBean pageBean) throws Exception{
String sql = "select * from t_mvc_book where 1=1";
String bname = book.getBname();
if(StringUtils.isNotBlank(bname)) {
sql += " and bname like '%"+bname+"%'";
}
int bid = book.getBid();
//前台jsp传递到后台,只要传了就有值,没传就是默认值,默认值就是0
if(bid !=0) {
sql += " and bid = "+bid;
}
return super.executeQuery(sql, pageBean, rs ->{
List<Book> list = new ArrayList<>();
try {
while(rs.next()) {
list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
});
}
// 增
public int add(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, book, new String[] {"bid","bname","price"});
}
// 删
// public int del(Book book) throws Exception {
// Connection con = DBAccess.getConnection();
// String sql = "delete from t_mvc_book where bid = ?";
// PreparedStatement pst = con.prepareStatement(sql);
// pst.setObject(1, book.getBid());
// return pst.executeUpdate();
// }
public int del(Book book) throws Exception {
String sql = "delete from t_mvc_book where bid = ?";
return super.executeUpdate(sql, book, new String[] {"bid"});
}
// 改
// public int edit(Book book) throws Exception {
// Connection con = DBAccess.getConnection();
// String sql = "update t_mvc_book set bname = ?,price = ? where bid = ?";
// PreparedStatement pst = con.prepareStatement(sql);
// pst.setObject(3, book.getBid());
// pst.setObject(1, book.getBname());
// pst.setObject(2, book.getPrice());
// return pst.executeUpdate();
// }
public int edit(Book book) throws Exception {
String sql = "update t_mvc_book set bname = ?,price = ? where bid = ?";
return super.executeUpdate(sql, book, new String[] {"bname","price","bid"});
}
}
四、增删改查及表单提交问题
1、xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/book" type="com.shishirong.web.BookAction">
<forward name="List" path="/bookList.jsp" redirect="false" />
<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
<forward name="toList" path="/book.action?methodName=list" redirect="true" />
</action>
</config>
2、web层
package com.shishirong.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shishirong.dao.BookDao;
import com.shishirong.entity.Book;
import com.shishirong.util.PageBean;
import com.shishirong.framework.ActionSupport;
import com.shishirong.framework.ModelDriven;
public class BookAction extends ActionSupport implements ModelDriven<Book>{
private Book book = new Book();
private BookDao bookDao = new BookDao();
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
// 增
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.add(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// toList代表跳到查询界面
return "toList";
}
// 删
public String del(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.del(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// toList代表跳到查询界面
return "toList";
}
// 修
public String edit(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.edit(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// toList代表跳到查询界面
return "toList";
}
// 查
public String list(HttpServletRequest req, HttpServletResponse resp) {
try {
PageBean pageBean = new PageBean();
pageBean.setRequset(req);
List<Book> list = bookDao.list(book,pageBean);
req.setAttribute("list", list);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 执行查询展示
return "List";
}
// 跳转到新增、修改界面
// 修
public String perEdit(HttpServletRequest req, HttpServletResponse resp) {
try {
int bid = book.getBid();
if(bid != 0 ) {
// 传递bid到后台,有且之恩能够查出一条数据,那也就意味着list集合中只有一条
List<Book> list = bookDao.list(book, null);
req.setAttribute("b", list.get(0));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// toList代表跳到编辑界面
return "toEdit";
}
}
3、主界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://jsp.veryedu.cn" prefix="s"%>
<%@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 class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/book.action?methodName=perEdit">新增</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=perEdit&bid=${b.bid }">编辑</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del&bid=${b.bid }">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<script type='text/javascript'>
function gotoPage(page) {
document.getElementById('pageBeanForm').page.value = page;
document.getElementById('pageBeanForm').submit();
}
function skipPage() {
var page = document.getElementById('skipPage').value;
if (!page || isNaN(page) || parseInt(page) < 1
|| parseInt(page) > 1122) {
alert('请输入1~N的数字');
return;
}
gotoPage(page);
}
</script>
<s:page pageBean="${pageBean }"></s:page>
</body>
</html>
4、编辑界面
<%@ 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 }/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>
5、解决中文乱码
/**
* 中文乱码处理
*
*/
@WebFilter("*.action")
6、效果截图