package com.hd.web;
import java.io.IOException;
import java.lang.reflect.Field;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Book;
import com.lzh.framework.ActionSupport;
import com.lzh.framework.ModelDriver;
public class BookAction extends ActionSupport implements ModelDriver<Book>{
private Book book = new Book();
// execute
private String delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.delete()...");
/*
* 需求:删除数据后跳转到书籍展示页面
* 缺陷:1.出现大量重复代码 2.不好管理(增删改重新跳转到查询界面)
* 解决方案:在一个地方统一管理重定向还是转发,并且是定位跳转到哪一个页面
* 预测结果
* BookAction调用bookDao.delete()...
* BookAction调用bookDao.list()...
*
*/
// try {
// req.getRequestDispatcher("/bookList.jsp").forward(req, resp);
resp.sendRedirect("/bookList.jsp");
// this.list(req, resp);
// } catch (Exception e) {
// e.printStackTrace();
// }
return "toList";
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.add()...");
}
private String list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.list()...");
// try {
// req.getRequestDispatcher("/bookList.jsp").forward(req, resp);
// } catch (Exception e) {
// e.printStackTrace();
// }
return "list";
}
private void ref(HttpServletRequest req, HttpServletResponse resp) {
// 解决参数实体类封装的问题
/*
* 缺陷:对于前端传参,实体类属性值封装req.getParammeter("")代码 是重复的
* 需求:做一个book的新增功能
* 思考:将JSP参数塞进实体类的属性中,问题是每个实体类的属性是不一样的,没办法做到代码的完全重复
* 思路:
* 不知道是哪一个实体类->泛型T
* 不知道这个实体类有哪些属性,但是又要给这些属性赋值->反射动态设置属性值
* 解决:泛型+反射
* 编码:
* 模型驱动接口:ModelDriver
*/
// Book book = new Book();
// book.setBid(req.getParameter("bid"));
// book.setBname(req.getParameter("bname"));
// book.setPrice(req.getParameter("price"));
// book.setAuthor(req.getParameter("author"));
// book.setPublish(req.getParameter("publish"));
System.out.println(book);
System.out.println("BookAction调用bookDao.ref(book)...");
}
@Override
public Book getModel() {
return book;
}
}