自定义MVC的进阶使用

前言

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

一、环境配置

1.1 将框架打包成jar包

将前面我们所写的通用Servlet——框架打包成Jar包
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.2 将Jar包导入新项目

将Jar包导入新项目,并导入相关依赖
在这里插入图片描述

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

tag和util
在这里插入图片描述

1.4 配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_Project</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
   <servlet>
  	<servlet-name>mvc</servlet-name>
  	<servlet-class>com.xqx.framework.DispatherServlet</servlet-class>
  	<init-param>
  		<param-name>configurationLocation</param-name>
  		<param-value>/mvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>mvc</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

tld文件:自定义标签

<?xml version="1.0" encoding="UTF-8" ?>

<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 library</description>
  <display-name>JSTL core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>x</short-name>
  <uri>http://jsp.xqx.cn</uri>
  <tag>
        <name>page</name>
        <tag-class>com.xqx.tag.PageTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>pageBean</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </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

<?xml version="1.0" encoding="UTF-8"?>
<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 here</title>
<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">书籍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=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 运行结果

查询

在这里插入图片描述

新增

在这里插入图片描述
在这里插入图片描述


修改
在这里插入图片描述
在这里插入图片描述
好啦,本篇博客就到此为止!希望你看完本篇文章有所收获,祝你变得更强!!!

内容概要:本文介绍了一种基于蒙特卡洛模拟和拉格朗日优化方法的电动汽车充电站有序充电调度策略,重点针对分时电价机制下的分散式优化问题。通过Matlab代码实现,构建了考虑用户充电需求、电网负荷平衡及电价波动的数学模【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)型,采用拉格朗日乘子法处理约束条件,结合蒙特卡洛方法模拟大量电动汽车的随机充电行为,实现对充电功率和时间的优化分配,旨在降低用户充电成本、平抑电网峰谷差并提升充电站运营效率。该方法体现了智能优化算法在电力系统调度中的实际应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源汽车、智能电网相关领域的工程技术人员。; 使用场景及目标:①研究电动汽车有序充电调度策略的设计与仿真;②学习蒙特卡洛模拟与拉格朗日优化在能源系统中的联合应用;③掌握基于分时电价的需求响应优化建模方法;④为微电网、充电站运营管理提供技术支持和决策参考。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注目标函数构建、约束条件处理及优化求解过程,可尝试调整参数设置以观察不同场景下的调度效果,进一步拓展至多目标优化或多类型负荷协调调度的研究。
内容概要:本文围绕面向制造业的鲁棒机器学习集成计算流程展开研究,提出了一套基于Python实现的综合性计算框架,旨在应对制造过程中数据不确定性、噪声干扰面向制造业的鲁棒机器学习集成计算流程研究(Python代码实现)及模型泛化能力不足等问题。该流程集成了数据预处理、特征工程、异常检测、模型训练与优化、鲁棒性增强及结果可视化等关键环节,结合集成学习方法提升预测精度与稳定性,适用于质量控制、设备故障预警、工艺参数优化等典型制造场景。文中通过实际案例验证了所提方法在提升模型鲁棒性和预测性能方面的有效性。; 适合人群:具备Python编程基础和机器学习基础知识,从事智能制造、工业数据分析及相关领域研究的研发人员与工程技术人员,尤其适合工作1-3年希望将机器学习应用于实际制造系统的开发者。; 使用场景及目标:①在制造环境中构建抗干扰能力强、稳定性高的预测模型;②实现对生产过程中的关键指标(如产品质量、设备状态)进行精准监控与预测;③提升传统制造系统向智能化转型过程中的数据驱动决策能力。; 阅读建议:建议读者结合文中提供的Python代码实例,逐步复现整个计算流程,并针对自身业务场景进行数据适配与模型调优,重点关注鲁棒性设计与集成策略的应用,以充分发挥该框架在复杂工业环境下的优势。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值