在上篇博文《深入淺出學習Struts架構(一):一個簡單mvc模式程式碼範例開始》中簡單說了一下MVC模式代碼的實現和MVC模式和三層架構的區別,並且留下一個問題—如何去掉TestServlet中的IF-Else語句塊。
因為在TestServlet中出現了If-Else語句塊,所以讓程式變得不再靈活,讓應付需求變化時變得笨拙。所以就承接上篇文章來重構一下TestServlet代碼,主要是用繼承多肽來進一步對TestServlet進行重構。雖然這一篇文章最後沒有將if-else徹底去掉,但是比起上篇文章的代碼顯得更加靈活,也為下一篇徹底去掉If-else埋下伏筆。
下面進入重構階段:
先看一下上篇文章的TestServlet代碼:
package com.cjq.servlet;</p><p>import java.io.IOException;<br />import java.util.List;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>public class TestServlet extends HttpServlet {</p><p>protected void doGet(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {</p><p>String requestURI=request.getRequestURI();<br />System.out.println("request="+requestURI);<br />String path=requestURI.substring(requestURI.indexOf("/",1),requestURI.indexOf("."));<br />System.out.println("path="+path);</p><p>String username=request.getParameter("username");<br />UserManager userManager=new UserManager();<br />//userManager.addUser(username);<br />String forward="";<br />if("/servlet/delUser".equals(path)){<br />userManager.delUser(username);<br />forward="/del_success.jsp";<br />}else if("/servlet/addUser".equals(path)){<br />userManager.addUser(username);<br />forward="/add_success.jsp";<br />}else if("/servlet/modifyUser".equals(path)){<br />userManager.modifyUser(username);<br />forward="/modify_success.jsp";<br />}else if("/servlet/queryUser".equals(path)){<br />List userList=userManager.queryUser(username);<br />request.setAttribute("userList", userList);<br />forward="/query_success.jsp";<br />}else{<br />throw new RuntimeException("請求失敗");<br />}<br />request.getRequestDispatcher(forward).forward(request, response);<br /> }<br />}
首先我們看到了在每個語句塊中都出現了給forward賦值,其實也就是給頁面跳轉的路徑賦值,針對每個請求路徑判斷來賦值跳轉路徑。另外每個IF-Else語句塊中都有業務處理,我們要把這些業務處理分別放到類裡面,讓職責更加單一,這樣更加符合物件導向的思路。
就從這裡我們開始重構,我們可以將這個跳轉路徑和商務邏輯封裝起來。
既然封裝,那麼我們就抽象出來一個借口,主要完成一個方法,這個方法主要的功能就是要完成商務邏輯封裝和路徑跳轉的返回。隨後建立四個類,主要實現相應的增刪改查的業務處理和處理之後的跳轉路徑返回。
代碼如下:
介面Action:
package com.cjq.servlet;</p><p>import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>public interface Action {</p><p>public String execute(HttpServletRequest request,HttpServletResponse response)<br />throws Exception;<br />}
增刪改查實作類別:
添加使用者實作類別:
<pre name="code" class="java">package com.cjq.servlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AddUserAction implements Action {public String execute(HttpServletRequest request,HttpServletResponse response) throws Exception {String username=request.getParameter("username");UserManager userManager=new UserManager();userManager.addUser(username);return "/add_success.jsp";}}</pre><p>
刪除使用者實作類別:
package com.cjq.servlet;</p><p>import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>public class DelUserAction implements Action {</p><p>public String execute(HttpServletRequest request,<br />HttpServletResponse response) throws Exception {<br />String username=request.getParameter("username");<br />UserManager userManager=new UserManager();<br />userManager.delUser(username);<br />return "/del_success.jsp";<br />}</p><p>}
更新使用者實作類別:
package com.cjq.servlet;</p><p>import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>public class ModifyUserAction implements Action {</p><p>@Override<br />public String execute(HttpServletRequest request,<br />HttpServletResponse response) throws Exception {<br />String username=request.getParameter("username");<br />UserManager userManager=new UserManager();<br />userManager.modifyUser(username);<br />return "/modify_success.jsp";<br />}</p><p>}
查詢使用者實作類別:
package com.cjq.servlet;</p><p>import java.util.List;</p><p>import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>public class QueryUserAction implements Action {</p><p>@Override<br />public String execute(HttpServletRequest request,<br />HttpServletResponse response) throws Exception {<br />String username=request.getParameter("username");<br />UserManager userManager=new UserManager();<br />List userList=userManager.queryUser(username);<br />request.setAttribute("userList", userList);<br />return "/query_success.jsp";<br />}</p><p>}
TestServlet類重構如下:
package com.cjq.servlet;</p><p>import java.io.IOException;<br />import java.util.List;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>public class TestServlet extends HttpServlet {</p><p>protected void doGet(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {</p><p>String requestURI=request.getRequestURI();<br />System.out.println("request="+requestURI);<br />String path=requestURI.substring(requestURI.indexOf("/",1),requestURI.indexOf("."));<br />System.out.println("path="+path);</p><p>Action action=null;<br />if("/servlet/delUser".equals(path)){<br />action=new DelUserAction();<br />}else if("/servlet/addUser".equals(path)){<br />action=new AddUserAction();<br />}else if("/servlet/modifyUser".equals(path)){<br />action=new ModifyUserAction();<br />}else if("/servlet/queryUser".equals(path)){<br />action=new QueryUserAction();<br />}else{<br />throw new RuntimeException("請求失敗");<br />}<br />String forward=null;<br />try{<br />forward=action.execute(request, response);<br />}catch(Exception e){<br />e.printStackTrace();<br />}<br />request.getRequestDispatcher(forward).forward(request, response);<br /> }<br />}
運行結果:
這樣TestServlet類雖然沒有徹底去掉If-Else,但是這樣的代碼變得更加簡練,利用多肽實現商務邏輯處理和路徑跳轉返回。職責更加清晰,讓維護變得更加輕鬆。
問題遺留:
If-else語句塊沒有徹底剔除,而且程式中依舊出現了過多的字串,所以程式依舊是不靈活,而且字串過多增加調試的複雜性。所以下一篇文章就把if-else語句塊剔除,並且把字串放到設定檔來用dom4j來動態讀取。
我們離Struts架構越來越近,當我們把if-else和字串剔除之後就會出現Struts架構的雛形。期待下一篇文章。