自定义MVC的V1.0和V2.0版本
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
/**
* 增删改查缺陷
* 当需求发生改变或者新增需求,需要改动下面代码
*/
String methodName = req.getParameter("methodName");
if ("add".equals(methodName)) {
add(req,resp);
}else if ("edit".equals(methodName)) {
edit(req,resp);
}else if ("delete".equals(methodName)) {
delete(req,resp);
}else if ("list".equals(methodName)) {
list(req,resp);
}else if ("load".equals(methodName)) {
load(req,resp);
}
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.add()...");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.edit()...");
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.delete()...");
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.list()...");
}
//修改页面数据回显
private void load(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.load()...");
}
* 增删改查缺陷
* 当需求发生改变或者新增需求,需要改动下面代码
如图上的load在最下面回显后需要在上面添加
else if ("load".equals(methodName)) {
load(req,resp);
}
* 解决方案
* 做判断的目的是为了调用方法
* 比如浏览器发了个请求最终目的是掉方法,我们想要做的是能不能跳过他们直接调用当前类对象的方法
* this.methodName
String methodName = req.getParameter("methodName");
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
}
/**
if ("add".equals(methodName)) {
add(req,resp);
}else if ("edit".equals(methodName)) {
edit(req,resp);
}else if ("delete".equals(methodName)) {
delete(req,resp);
}else if ("list".equals(methodName)) {
list(req,resp);
}else if ("load".equals(methodName)) {
load(req,resp);
}*/
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.add()...");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.edit()...");
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.delete()...");
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.list()...");
}
//修改页面数据回显
private void load(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.load()...");
}
然后这样就我们的V3.0版本基本上也出来了
增删改查代码V3.0
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=ref">关联查询</a>
运行一下
看看方法
//关联查询
private void ref(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.list()...");
}
//修改页面数据回显
private void load(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.load()...");