【SSH进阶之路】一步步重构MVC实现Struts框架——封装业务逻辑和跳转路径(四)
目录:
【SSH进阶之路】Struts基本原理 + 实现简单登录(二)
【SSH进阶之路】一步步重构MVC实现Struts框架——从一个简单MVC开始(三)
【SSH进阶之路】一步步重构MVC实现Struts框架——封装业务逻辑和跳转路径(四)
【SSH进阶之路】一步步重构MVC实现Struts框架——彻底去掉逻辑判断(五)
【SSH进阶之路】一步步重构MVC实现Struts框架——完善转向页面,大功告成(六)
Struts的第二篇博客【SSH进阶之路】Struts基本原理 + 实现简单登录(二),我们介绍了MVC和Struts的基本理论,上篇博客【SSH进阶之路】一步步重构MVC实现Struts框架——从一个简单MVC开始(三),我们实现了一个MVC模型,这篇博客我们解决一下上篇博客留下的第一个问题:封装业务逻辑和跳转路径。
我们先回顾一下上篇博客中的TestServlet的代码:
package com.liang.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 使用servlet做相关的控制,转向多个(V)视图 * @author liang * */ public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //取得访问的URI String reqeuestURI = request.getRequestURI(); System.out.println(reqeuestURI); //截取URI,获得路径 String path = reqeuestURI.substring(reqeuestURI.indexOf("/",1), reqeuestURI.indexOf(".")); System.out.println(path); //获得表单数据 String username = request.getParameter("username"); UserManager userManager = new UserManager(); String forward =""; //根据url执行相关的功能 //等于添加,调用添加方法,添加成功转到添加页面 if("/servlet/addUser".equals(path)){ //调用添加的业务逻辑 userManager.add(username); forward ="/add_success.jsp"; //删除,调用删除方法,删除成功转到删除 }else if("/servlet/delUser".equals(path)){ //调用删除的业务逻辑 userManager.del(username); forward ="/servlet/delUser"; //修改,调用修改方法,修改成功转到修改页面 }else if("/servlet/modifyUser".equals(path)){ //调用修改的业务逻辑 userManager.modify(username); forward ="/modify_success.jsp"; //查询,调用查询方法,查询成功转到查询成功页面 }else if("/servlet/queryUser".equals(path)){ //调用查询的业务逻辑 List userList = userManager.query(username); request.setAttribute("userList", userList); forward ="/query_success.jsp"; }else{ throw new RuntimeException("请求失败"); } //统一完成转向 request.getRequestDispatcher(forward).forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
从TestServlet中我们发现,它就是一个简单的Servlet,它是Struts的前端控制器,负责把所有的URL截取下来,通过IF语句根据URL的不同执行相关的功能,并且添加,删除,修改和查询各自有各自的业务逻辑和跳转路径,我们可以各自的业务逻辑和跳转路径封装成不同的类,并抽象出一个公共接口,如下所示:
下面,我们看一下接口和实现类的代码:
接口Action
package com.liang.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public interface Action { public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception; }
添加用户AddUserAction
package com.liang.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddUserAction implements Action { @Override public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { //获取参数 String username = req.getParameter("username"); UserManager userManager = new UserManager(); //调用业务逻辑 userManager.add(username); //返回跳转页面 return "/add_success.jsp"; } }
删除用户DelUserAction
package com.liang.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DelUserAction implements Action { @Override public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { <span style="white-space:pre"> </span>//获取参数 String username = req.getParameter("username"); UserManager userManager = new UserManager(); //调用业务逻辑 userManager.del(username); //返回跳转页面 return "/del_success.jsp"; } }
修改用户ModifyUserAction
package com.liang.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ModifyUserAction implements Action { @Override public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { //获取参数 String username = req.getParameter("username"); //调用业务逻辑 UserManager userManager = new UserManager(); userManager.modify(username); //返回跳转页面 return "/modify_success.jsp"; } }
查询用户QueryUserAction
package com.liang.servlet; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class QueryUserAction implements Action { @Override public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { //获取参数 String username = req.getParameter("username"); //调用业务逻辑 UserManager userManager = new UserManager(); List userList = userManager.query(username); req.setAttribute("userList", userList); userManager.query(username); //返回跳转页面 return "/query_success.jsp"; } }
我们再对比看一下我们封装之后的TestServlet
package com.liang.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.liang.action.Action; import com.liang.action.AddUserAction; import com.liang.action.DelUserAction; import com.liang.action.ModifyUserAction; import com.liang.action.QueryUserAction; /** * 使用servlet做相关的控制,转向多个(V)视图 * @author liang * */ public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //取得访问的URI String reqeuestURI = request.getRequestURI(); System.out.println(reqeuestURI); //截取URI,获得路径 String path = reqeuestURI.substring(reqeuestURI.indexOf("/",1), reqeuestURI.indexOf(".")); System.out.println(path); Action action = null; //等于添加,调用AddUserAction if ("/servlet/addUser".equals(path)) { action = new AddUserAction(); //等于删除,调用DelUserAction }else if ("/servlet/delUser".equals(path)) { action = new DelUserAction(); //等于修改,调用ModifyUserAction }else if ("/servlet/modifyUser".equals(path)) { action = new ModifyUserAction(); //等于查询,调用QueryUserAction }else if ("/servlet/queryUser".equals(path)) { action = new QueryUserAction(); }else { throw new RuntimeException("请求失败"); } String forward = null; //返回不同的转向页面 try { forward = action.execute(request, response); } catch (Exception e) { e.printStackTrace(); } //根据路径,统一完成转向 request.getRequestDispatcher(forward).forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
通过对比,我们可以发现简单了很多,但是实现的道理基本一样,仍然是将path截取出来,做相应的判断,进入对应的分支,New一个刚写的Action,New谁调谁,但是此时仅仅是完成了功能,系统并不灵活,IF...ELSE并没有去掉,扩展性不好,不能适用需求。
下篇博客【SSH进阶之路】一步步重构MVC实现Struts框架——彻底去掉判断逻辑(五),我们解决第二个问题:IF...ElSE的问题。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。