1.思维导图:
![](https://i-blog.csdnimg.cn/blog_migrate/1f5191b16c46c4f82df8d14a0b2b21d3.png)
2.前台传递methodNam到后台,实际就是想调用当前类对象的methodNam方法:
public class BookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String methodName = req.getParameter("methodName");
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//打开权限
m.setAccessible(true);
//调用
m.invoke(this,req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.list()...");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.edit()...");
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.delete()...");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.add()...");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
//修改页面数据回显
System.out.println("bookDao.load()...");
}
private void ref(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.ref()...");
}
2.1初始化子控制器容器(集合),经过初始化,actions容器内部就有了子控制器:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
//action=BookAction
//ActionSupport action=new BookAction();
ActionSupport action = actions.get(uri);
action.execute(req, resp);
}
3.视图层:
<%@ 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>
目前多数人的增删改查的代码
<a href="${pageContext.request.contextPath }/book/add">增加</a>
<a href="${pageContext.request.contextPath }/book/delete">删除</a>
<a href="${pageContext.request.contextPath }/book/edit">修改</a>
<a href="${pageContext.request.contextPath }/book/list">出现</a>
<hr>
增删改查的代码V2.0
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=delete">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">出现</a>
<hr>
增删改查的代码V3.0
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=ref">关联</a>
</body>
</html>
4.编写过程中遇到的报错
![](https://i-blog.csdnimg.cn/blog_migrate/6a783e5dba771920094db3834ca82d30.png)
4.1 问题出现在“edit”编写错误
![](https://i-blog.csdnimg.cn/blog_migrate/5e92f3ff8c831aa92b496f64009b7ba0.jpeg)
5. 运行的结果:
![](https://i-blog.csdnimg.cn/blog_migrate/f9ba8cddacf286eda82a4d0a6067e964.png)
5.1
![](https://i-blog.csdnimg.cn/blog_migrate/5b72a876fc3c9a92b9cb30f1ced453f8.png)