3.反射优化版
版本三:反射机制优化if条件判断
package com.xw.servlet; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 模拟版本三mvc增删改查反射机制优化if条件判断 */ @WebServlet("/BookClass.do") public class BookClassCRUDServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收请求参数 String CrudName = request.getParameter("CrudName"); try { //获取该servlet的所有方法 Method m = this.getClass().getDeclaredMethod(CrudName, HttpServletRequest.class, HttpServletResponse.class); //打开访问权限 m.setAccessible(true); //调用该类的方法 m.invoke(this,request,response ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void list(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本三反射机制优化的查询"); } private void del(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本三反射机制优化的删除"); } private void upd(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本三反射机制优化的修改"); } private void add(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本三反射机制优化的新增"); } }
不足之处:虽然只需要写一个servlet,也不需要写if条件判断,但是放到项目目中来看,我们不止只有一个book可能还有Goods,Student等 也是需要写多个Servlet,一个Servlet对应一张表。
4.反射增强版
创建DispatchServlet中央控制器接收请求并将请求发送到对应的子控制器并完成init方法中的请求路径与子控制器类的对应配置与doPost方法的请求处理。
package com.xw.framework; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xw.servlet.BookAction; import com.xw.servlet.GoodsAction; /** * 中央控制器拦截请求根据请求找到子控制器 */ @WebServlet("*.do") public class DispatchServlet extends HttpServlet { //保存子控制器 private Map<String, ActionSupport> actionMap = new HashMap<String, ActionSupport>(); @Override public void init() throws ServletException { //初始化子控制值 actionMap.put("/book", new BookAction()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取到url请求 String url = request.getRequestURI(); //截取指定需要操作的表 url=url.substring(url.lastIndexOf("/"),url.lastIndexOf(".")); //根据请求截取到key找到对应的子控制器 ActionSupport actionSupport = actionMap.get(url); //调用子控制器处理业务逻辑 actionSupport.execute(request, response); } }
创建Action抽象子控制器并定义execute方法,用于处理具体的核心业务逻辑处理。
package com.xw.framework; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /**子控制器并定义execute方法,用于处理具体的核心业务逻辑处理 * @author Java方文山 * */ public interface Action { public String execute(HttpServletRequest req, HttpServletResponse resp); }
创建ActionSupport并继承抽象类Action,重写execute方法,通过反射找到对应的Map集合保存的Servlet子控制器实现业务逻辑层处理。
package com.xw.framework; import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.corba.se.spi.orbutil.fsm.FSM; import com.sun.corba.se.spi.orbutil.fsm.Input; /**重写execute方法,实现业务逻辑处理 * @author Java方文山 * */ public class ActionSupport implements Action { public String execute(HttpServletRequest req, HttpServletResponse resp) { String invoke = null; try { //接收请求参数 String CrudName = req.getParameter("CrudName"); //获取该servlet的所有方法 Method m = this.getClass().getDeclaredMethod(CrudName, HttpServletRequest.class, HttpServletResponse.class); //打开访问权限 m.setAccessible(true); //调用该类的方法 invoke = (String) m.invoke(this, req, resp); } catch (Exception e) { e.printStackTrace(); } return invoke; } }
创建BookAction继承ActionSuppor,实现业务逻辑处理。
package com.xw.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xw.framework.ActionSupport; public class BookAction extends ActionSupport { private void list(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的查询——book"); } private void del(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的删除——book"); } private void upd(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的修改——book"); } private void add(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的新增——book"); } }
到这里我们就完成我们第四代MVC架构啦!!
当我们需要对另一个表进行操作的时候我们只需要在DispatchServlet中央控制器进行子控制器的配置以及最后子控制器的业务逻辑层编写即可。
例如:
我还需要对Goods表进行增删改查操作
①配置子控制器
②编写子控制器
package com.xw.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xw.framework.ActionSupport; public class GoodsAction extends ActionSupport { private void list(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的查询——goods"); } private void del(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的删除——goods"); } private void upd(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的修改——goods"); } private void add(HttpServletRequest request, HttpServletResponse response) { System.out.println("我是版本四反射机制优化的新增——goods"); } }
接下来测试一下看看打印结果
是不是相对前三代更为简单了呢?看完过后你对MVC的用途有没有更为理解?
至此我的首篇自定义MVC就分享到这里啦!!!