|
2010-10-18
用JSP+Servlet+JavaBean實現(xiàn)MVC設(shè)計模式的流程MVC是三個單詞的縮寫:M,Model(模型);V,View( 視圖 ),C,Control(控制)。 MVC模式的目的就是實現(xiàn)Web系統(tǒng)的職能分工, Model層:實現(xiàn)系統(tǒng)的業(yè)務(wù)邏輯,即javaBean部分 View層:負責(zé)與用戶交互,即在界面上展示數(shù)據(jù)對象給用戶,即html,jsp Control層:Model與View之間溝通的橋梁,它可以分派用戶的請求并選擇恰當(dāng)?shù)囊晥D以用于顯示,同時它也可以解釋用戶的輸入并將它們映射為模型層可執(zhí)行的操作,當(dāng)然就是Servlet的職責(zé)了
下面我們用MVC設(shè)計模式來實現(xiàn) 簡單的用戶登錄過程 1、控制器Servlet的實現(xiàn) 系統(tǒng)中只有一個servlet即ControlServlet,所有頁面發(fā)起的以" *.do "的請求,都被web.xml配置給ControlServlet進行處理,在ControlServlet中根據(jù)‘ * ’的字符串(即解析用戶請求的路徑),調(diào)用ActionFactory生成的制定Action對象,在將處理后的URL轉(zhuǎn)發(fā)給用戶。
package cn.netjava.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 cn.netjava.action.Action;
import cn.netjava.action.ActionFactory;
/**
* Servlet implementation class ControlServlet
*/
public class ControlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到當(dāng)前Servlet的請求路徑
String pathName =request.getServletPath();
// request.getContextPath();得到項目名字
System.out.println("pathName:"+pathName);
//得到請求的Action名字
int index = pathName.indexOf(".");
String ActionName = pathName.substring(1, index);
System.out.println(ActionName);
//獲取運行時參數(shù)
String ActionClassName = this.getInitParameter(ActionName);
//得到Action對象
Action action = ActionFactory.getActionFactory().getAction(ActionClassName);
//執(zhí)行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);
}
}
}
2、Action對象工廠類實現(xiàn):
ActionFactory是一個單實例類(整個系統(tǒng)只需要使用其一個對象),它只提供一個Action對象,通過getAction(String ActionClassName) 的方法調(diào)用創(chuàng)建一個Action對象。這個方法在Control中被調(diào)用。代碼如下:
package cn.netjava.action;
/**
* 根據(jù)Action名字,創(chuàng)建Action對象
* @author Administrator
*
*/
public class ActionFactory {
//單例模式:不需要創(chuàng)建對象
private ActionFactory(){
}
//單實例訪問方法,得到ActionFactory對象
public static ActionFactory getActionFactory(){
if(af == null){
af = new ActionFactory();
}
return af;
}
/**
* 根據(jù)具體的Action類名字創(chuàng)建Action對象
* @param ActionClassName :具體的Action類全名
* @return:Action類型對象
*/
public Action getAction(String ActionClassName){
Action action = null;
try{
action = (Action) Class.forName(ActionClassName).newInstance();
}catch(Exception e){
e.printStackTrace();
}
return action;
}
private static ActionFactory af;
}
3、Action接口類定義: 所有的事件處理(即Action)類都必須實現(xiàn)這個接口
package cn.netjava.action;
public interface Action {
/**
* 所有的具體Action實現(xiàn)這個接口
* @param request 請求對象
* @param response 應(yīng)答對象
* @return :結(jié)果頁面
*/
public String execute(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response);
}
4、web.xml中配置請求發(fā)送給控制器Servlet
最后,我們只需要在wex.xml中對MVC結(jié)構(gòu)的配置: 視圖頁面中的請求都是以<動作名字>.do結(jié)尾,當(dāng)這個請求到達web服務(wù)器后,會被服務(wù)器轉(zhuǎn)向給控制器處理,控制器在根據(jù)解析出的動作名,調(diào)用對應(yīng)的Action對象,處理結(jié)果,并輸出結(jié)果頁面,所以web.xml中必須有如下配置:
<servlet> <servlet-name>controlServlet</servlet-name> <servlet-class>cn.netjava.servlet.ControlServlet</servlet-class> <init-param> <param-name>loginAction</param-name> <param-value>cn.netjava.action.LoginAction</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>controlServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> 4、具體的Action類實現(xiàn)(即對登錄動作進行處理的類)
package cn.netjava.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginAction implements Action {
public String execute(HttpServletRequest request,
HttpServletResponse response) {
// 得到用戶名和密碼
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
if (userName.equals("netjava") && userPwd.equals("netjava")) {
request.setAttribute("userName", userName);
return "main.jsp";
} else {
return "login.jsp";
}
}
}
如果登錄成功,跳轉(zhuǎn)到 main.jsp頁面,否則,返回login,jsp頁面
以下是main.jsp和login.jsp頁面: main.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1 style="color:red"><%=request.getAttribute("userName") %>登錄成功</h1>
</body>
</html>
login.jsp
|
|
|