幹了很久的項目都是使用SSH架構的,現在團隊有位搞美工的美女突然想要搞java了,讓我做下指導並打個基本的架構,需要使用最基本的架構,這樣說來那SSH只能滾得遠遠的了;
最基本的架構也就jsp+servlet+javabean,但心裡感覺還是不爽,搞這個不是純屬浪費時間啊。後來一想,對了我自己可以不用架構來設計出一個簡單的mvc架構來實現架構帶來的分層效果:
這裡我就以登入狀態例子來講解下,講解的順序為v--->c---->m
頁面端:login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>amdinLogin.html</title> </head> <body> <form action="../AdminLogin.action" method="post"> <input type = "text" name = "username"/> <input type = "password" name = "password"/> <input type = "submit" name = "login" value = "註冊"/> </form> </body></html>
設定檔web.xml
<servlet> <servlet-name>controlServlet</servlet-name> <servlet-class>xidian.sl.equipment.Servlet.ControlServlet</servlet-class><!-- 這裡的配置與一般的servlet一樣,但由於整個項目只會有一個servlet,所有的訪問都將訪問這個servlet,因此不同的訪問只需要添加下面的參數即可--> <init-param> <!-- 頁面請求的地址--> <param-name>AdminLogin</param-name> <!-- 處理action的後台Action地址--> <param-value>xidian.sl.equipment.action.LoginAction</param-value> </init-param> <init-param> <param-name>picListAction</param-name> <param-value>xidian.sl.equipment.action.PicListAction</param-value> </init-param> <init-param> <param-name>newListAction</param-name> <param-value>xidian.sl.equipment.action.NewListAction</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>controlServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
充當控制層的servlet:xidian.sl.equipment.Servlet.ControlServlet(很重要)
package xidian.sl.equipment.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 xidian.sl.equipment.action.interfaces.Action;import xidian.sl.equipment.actionfactory.ActionFactory;public class ControlServlet extends HttpServlet{ /** * 任何請求都會到這個servlet中,這個servlet就是充當MVC模式中的C(控制層) */ private static final long serialVersionUID = 1L; @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 { /** * 得到當前Servlet的請求路徑 * */ String pathName = request.getServletPath(); //System.out.println("pathName:"+pathName); /** * 得到請求的Action名字 * */ int index = pathName.indexOf("."); String ActionName = pathName.substring(1, index); //System.out.println(ActionName); /** * 擷取運行時參數 * */ String ActionClassName = this.getInitParameter(ActionName); //System.out.println("actionclassName "+ ActionClassName); /** * 得到Action對象 * */ Action action = ActionFactory.getActionFactory().getAction(ActionClassName); //System.out.println("action "+action); /** * 執行Action的execute得到要返回的URL路徑 * */ String url = action.execute(request, response); if(url == null){ request.getRequestDispatcher("error.jsp").forward(request, response); }else{ request.getRequestDispatcher(url).forward(request, response); } }}
Action:面向介面編程,提供一個統一的Action介面,裡面就一個方法execute();類比struts1.x
package xidian.sl.equipment.action.interfaces;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public interface Action { /** * 所有的具體Action實現這個介面 * @param request 請求對象 * @param response 應答對象 * @return :結果頁面 */ public String execute(HttpServletRequest request, HttpServletResponse response); }
LoginAction:
package xidian.sl.equipment.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import xidian.sl.equipment.action.interfaces.Action;import xidian.sl.equipment.dao.impl.AdminDAOImpl;import xidian.sl.equipment.dao.interfaces.AdminDAO;public class LoginAction implements Action { @Override public String execute(HttpServletRequest request, HttpServletResponse response) { /** * 從request中得到相應的值 * */ String username = request.getParameter("username"); String password = request.getParameter("password"); /** * 執行個體化DAO層,但其實這樣做還是不完美的,最好自己寫一個依賴注入的類 * */ AdminDAO adminDAO = new AdminDAOImpl(); /** * 返回尋找後的二維數組 * */ String[][] data = adminDAO.findAdmin(username, password); if(data == null|| (data != null&& data.length == 0)){ //數組為空白,表示無此帳號存在 return "admin/login.html"; } else { return "admin/index.html"; } }}
在實作類別LoginAction中執行個體化了AdminDAO進行持久化操作,這裡為了方便起見是直接使用new進行執行個體化,但這樣寫入程式碼帶來耦合,如需要改進可以參考類比spring的ioc
http://www.cnblogs.com/shenliang123/archive/2012/05/10/2494412.html 討論簡單原廠模式時有簡單的實現,大家可以進行改進使其更加完美
AdminDAO:
package xidian.sl.equipment.dao.interfaces;public interface AdminDAO { /** * 根據使用者名稱與密碼來尋找相應使用者 * */ public String[][] findAdmin(String username, String password);}
AdminDAOImpl:
package xidian.sl.equipment.dao.impl;import xidian.sl.equipment.dao.interfaces.AdminDAO;import xidian.sl.equipment.util.DbConn;public class AdminDAOImpl implements AdminDAO{ @Override public String[][] findAdmin(String username, String password) { String sql = "select * from admin as ad where ad.aId = '"+username+"' and ad.aPassword = '"+password+"'"; String[][] data = DbConn.query(sql); return data; }}
該實作類別中使用了已封裝好的資料庫操作類,詳細見:http://www.cnblogs.com/shenliang123/archive/2012/05/10/2494874.html
資料庫的設計比較簡單:只需要主鍵id, 使用者帳號:username, 帳號密碼:password
這樣就基本實現了mvc的效果,你還可以根據項目的複雜程度向DAO與Action之間再插入一層Service