自定义MVC的进阶使用

文章目录

  • 前言
  • 一、环境配置
    • 1.1 将框架打包成jar包
    • 1.2 将Jar包导入新项目
    • 1.3 将分页标签相关文件、及相关助手类导入
    • 1.4 配置文件
  • 二、前后台编写
    • 2.1 实体类
    • 2.2 dao
    • 2.3 Servlet
    • 2.4 配置mvc.xml
    • 2.5 JSP
    • 2.6 运行结果

前言

通用增删改查、通用分页、XML解析反射建模,包括自定义MVC工作原理与低阶使用等等前面一系列的的文章,其实都是为了这篇博客所作的准备。在本篇博客,会将前面十几篇博客内容串联起来,做出一个简单却不平凡的微项目。

一、环境配置

1.1 将框架打包成jar包

将前面我们所写的通用Servlet——框架打包成Jar包
自定义MVC的进阶使用_第1张图片

自定义MVC的进阶使用_第2张图片
自定义MVC的进阶使用_第3张图片
在这里插入图片描述

1.2 将Jar包导入新项目

将Jar包导入新项目,并导入相关依赖
自定义MVC的进阶使用_第4张图片

1.3 将分页标签相关文件、及相关助手类导入

tag和util
自定义MVC的进阶使用_第5张图片

1.4 配置文件

web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 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>J2EE_Projectdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
   <servlet>
  	<servlet-name>mvcservlet-name>
  	<servlet-class>com.xqx.framework.DispatherServletservlet-class>
  	<init-param>
  		<param-name>configurationLocationparam-name>
  		<param-value>/mvc.xmlparam-value>
  	init-param>
  servlet>
  <servlet-mapping>
  	<servlet-name>mvcservlet-name>
  	<url-pattern>*.actionurl-pattern>
  servlet-mapping>
web-app>

tld文件:自定义标签



<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    
  <description>JSTL 1.1 core librarydescription>
  <display-name>JSTL coredisplay-name>
  <tlib-version>1.1tlib-version>
  <short-name>xshort-name>
  <uri>http://jsp.xqx.cnuri>
  <tag>
        <name>pagename>
        <tag-class>com.xqx.tag.PageTagtag-class>
        <body-content>JSPbody-content>
        <attribute>
            <name>pageBeanname>
            <required>truerequired>
            <rtexprvalue>truertexprvalue>
        attribute>
    tag>
taglib>

二、前后台编写

2.1 实体类

package com.xqx.entity;

/**
 * 
 * @author W许潜行
 *
 */
public class Book {
	private int bid;
	private String bname;
	private float price;


	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + 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() {
		// TODO Auto-generated constructor stub
	}
	

}

2.2 dao

package com.xqx.dao;

import java.util.List;

import com.xqx.entity.Book;
import com.xqx.util.BaseDao;
import com.xqx.util.PageBean;
import com.xqx.util.StringUtils;

/**Book dao方法 继承BaseDao
 * @author W许潜行 
 * 2023年7月4日 上午10:00:32
 */
public class BookDao extends BaseDao<Book> {

	/**新增
	 * @param book
	 * @return
	 * @throws Exception
	 */
	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" });
	}

	/**删除
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int delete(Book book) throws Exception {
		String sql = "delete from t_mvc_book where bid =?";
		return super.executeUpdate(sql, book, new String[] { "bid" });
	}

	/**修改
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int update(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" });
	}

	/**查询
	 * @param book
	 * @param pageBean
	 * @return
	 * @throws Exception
	 */
	public List<Book> list(Book book, PageBean pageBean) throws Exception {
		String sql = "select * from t_mvc_book where 1=1 ";
		int bid = book.getBid();
		String bname = book.getBname();
		if (StringUtils.isNotBlank(bname)) {
			sql += " where bname like '%" + bname + "%'";
		}
		if (bid != 0) {
			sql += " and where bid=" + bid;
		}
		return super.executeQuery(sql, Book.class, pageBean);
	}

}

2.3 Servlet

package com.xqx.web;

import java.util.List;

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

import com.xqx.dao.BookDao;
import com.xqx.entity.Book;
import com.xqx.framework.Action;
import com.xqx.framework.ModelDriver;
import com.xqx.util.PageBean;

/**子控制类
 * @author W许潜行
 * 2023年7月4日 上午10:04:25
 */
public class BookAction extends Action implements ModelDriver<Book> {
	private Book book = new Book();
	BookDao bd = new BookDao();

	/**
	 * 查询数据
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public String list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageBean pageBean = new PageBean();
		// 初始化
		pageBean.setRequest(req);
		List<Book> list = bd.list(book, pageBean);
		req.setAttribute("list", list);
		req.setAttribute("pageBean", pageBean);
		System.out.println("调用了");
		return "list";
	}

	/**
	 * 传入编辑页面的值
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public String toEdit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		List<Book> list = bd.list(book, null);
		req.setAttribute("edit", list.get(0));
		return "toEdit";
	}

	/**新增
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public String add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		bd.add(book);
		return "toList";
	}

	/**删除
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public String delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		bd.delete(book);
		return "toList";
	}

	/**修改
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public String update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		bd.update(book);
		return "toList";
	}

	@Override
	public Book getModel() {
		return book;
	}

}

2.4 配置mvc.xml

conf/mvc.xml


<config>
	<action path="/book" type="com.xqx.web.BookAction">
		<forward name="list" path="/bookList.jsp" redirect="false" />
		<forward name="toList" path="/book.action?methodName=list" redirect="true" />
		<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
	action>
config>

所需要操作的类和方法在该文件配置

2.5 JSP

booList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="x" uri="http://jsp.xqx.cn"%>
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=ISO-8859-1">
<title>Insert title heretitle>
<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;
}
.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=toEdit">新增a>
		
	form>

	<table class="table table-striped ">
		<thead>
			<tr>
				<th scope="col">书籍IDth>
				<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=toEdit&bid=${b.bid}">修改a>
					<a href="${pageContext.request.contextPath }/book.action?methodName=delete&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"%>
<%@ taglib uri="http://jsp.xqx.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>
head>
<body>
	<form class="form-inline"
		action="${pageContext.request.contextPath }/book.action?methodName=${empty edit ? 'add' : 'edit'}" method="post">
		书籍ID:<input type="text" name="bid" value="${edit.bid }"><br>
		书籍名称:<input type="text" name="bname" value="${edit.bname }"><br>
		书籍价格:<input type="text" name="price" value="${edit.price }"><br>
		<input type="submit">
	form>


body>
html>

2.6 运行结果

查询

自定义MVC的进阶使用_第6张图片

新增

自定义MVC的进阶使用_第7张图片
自定义MVC的进阶使用_第8张图片


修改
自定义MVC的进阶使用_第9张图片
自定义MVC的进阶使用_第10张图片
好啦,本篇博客就到此为止!希望你看完本篇文章有所收获,祝你变得更强!!!

你可能感兴趣的:(软件架构与设计模式,mvc)