内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

自定義MVC框架-創(chuàng)新互聯(lián)

一、 實現(xiàn)自定義MVC的體系結(jié)構(gòu)圖
自定義MVC框架

創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站設(shè)計、成都做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)石林,十多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

1、ModelI模式開發(fā)Web應(yīng)用時,分兩種情況:

純JSP技術(shù)方式開發(fā)

JSP+JavaBean方式開發(fā)

2、ModelI模式開發(fā)的不足:

JSP頁面中嵌入大量的Java代碼,可讀性差。

大量代碼在JSP中難以復(fù)用。

后期維護及擴展的難度大。

3、為了克服ModelI模式的缺陷,引入了ModelII的模式開發(fā)

ModelII模式體現(xiàn)了基于MVC(Model-View-Controller,模型-視圖-控制器)的設(shè)計模式,簡單的說,ModelII模式就是將數(shù)據(jù)顯示、流程控制和業(yè)務(wù)邏輯處理分離,使之相互獨立。

4、MVC設(shè)計模式由3個部分組成各部分的作用。

Model:模型,主要用于數(shù)據(jù)和業(yè)務(wù)的處理。

View:視圖,用于數(shù)據(jù)顯示。

Controller:控制器,用于流程控制。

5、MVC設(shè)計模式的特點

一個模型可以對應(yīng)多個視圖。

顯示與邏輯控制分離。

分層控制,減低了代碼間的耦合。

二、 我們?nèi)绾蝿?chuàng)建一個自己的MVC框架??

(一)我們要在lib里面準備一個夾包

dom4j-1.6.1.jar主要作用:解析xml文件

(二)準備配置文檔(在src下)

<!DOCTYPEmyframework[

<!ELEMENTmyframework(actions)>

<!ELEMENTactions(action*)>

<!ELEMENTaction(result*)>

<!ATTLISTactionnameCDATA#REQUIRED

classCDATA#REQUIRED

>

<!ATTLISTresultnameCDATA#IMPLIED

redirect(true|false)"false"

>

]>

解釋:自定義MVC框架

解釋:根據(jù)上述約束完成的“*”代表該節(jié)點可以出現(xiàn)多次

<myframework>

<actions>

<actionname="LoginAction"class="cn.action.LoginAction">

<resultname="success">success.jsp</result>

<resultname="login">login.jsp</result>

</action>

</actions>

</myframework>

(三)自己準備一個Action接口,用于放入結(jié)果集和執(zhí)行方法

packagecn.framework;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

publicinterfaceAction{

publicfinalStringSUCCESS="success";

publicfinalStringERROR="error";

publicfinalStringLOGIN="login";

publicfinalStringINPUT="input";

publicStringexecute(HttpServletRequestrequest,

HttpServletResponseresponse);

}

(四)定義一個ResultMapping用來存放result節(jié)點

packagecn.framework;

publiccla***esultMapping{

//result節(jié)點名字

privateStringname;

//是否重定向

privatebooleanredirect;

//跳轉(zhuǎn)的頁面

privateStringurl;

publicResultMapping(){

}

publicResultMapping(Stringname,booleanredirect,Stringurl){

this.name=name;

this.redirect=redirect;

this.url=url;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicbooleanisRedirect(){

returnredirect;

}

publicvoidsetRedirect(booleanredirect){

this.redirect=redirect;

}

publicStringgetUrl(){

returnurl;

}

publicvoidsetUrl(Stringurl){

this.url=url;

}

}

(五)定義一個ActionMapping用來存放Action節(jié)點

packagecn.framework;

importjava.util.HashMap;

importjava.util.Map;

publicclassActionMapping{

//Action名稱

privateStringname;

//Action名稱對應(yīng)的Action的類的全稱

privateStringclassName;

//result集合

privateMap<String,ResultMapping>results=newHashMap<String,ResultMapping>();

publicActionMapping(){

}

publicActionMapping(Stringname,StringclassName,Map<String,ResultMapping>results){

super();

this.name=name;

this.className=className;

this.results=results;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetClassName(){

returnclassName;

}

publicvoidsetClassName(StringclassName){

this.className=className;

}

publicMap<String,ResultMapping>getResults(){

returnresults;

}

publicvoidsetResults(Map<String,ResultMapping>results){

this.results=results;

}

}

(六)準備一個ActionMappingManager是用來管理ActionMapping的

packagecn.framework;

importjava.io.InputStream;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importorg.dom4j.Attribute;

importorg.dom4j.Document;

importorg.dom4j.Element;

importorg.dom4j.io.SAXReader;

publicclassActionMappingManager{

Map<String,ActionMapping>actionMapping=newHashMap<String,ActionMapping>();

publicActionMappingManager(){

init();

}

publicActionMappinggetActionMapping(StringactionName){

returnactionMapping.get(actionName);

}

publicActionMappingManager(StringfileName){

}

publicMap<String,ActionMapping>getActionMapping(){

returnactionMapping;

}

publicvoidsetActionMapping(Map<String,ActionMapping>actionMapping){

this.actionMapping=actionMapping;

}

publicstaticvoidmain(String[]args){

newActionMappingManager().init();

}

publicvoidinit(){

InputStreamis=this.getClass().getResourceAsStream("/myframework.xml");

SAXReadersr=newSAXReader();

try{

Documentdoc=sr.read(is);

ElementelRoot=doc.getRootElement();

List<Element>listActions=elRoot.elements();

for(ElementelActions:listActions){

List<Element>listAction=elActions.elements();

for(ElementelAction:listAction){

ActionMappingaMapping=newActionMapping();

AttributeattName=elAction.attribute("name");

AttributeattClass=elAction.attribute("class");

aMapping.setName(attName.getValue());

aMapping.setClassName(attClass.getValue());

List<Element>listResult=elAction.elements();

for(ElementelResult:listResult){

ResultMappingrMapping=newResultMapping();

AttributeattResultName=elResult.attribute("name");

AttributeattResultRedirect=elResult.attribute("redirect");

rMapping.setName(attResultName.getValue());

rMapping.setRedirect(Boolean.parseBoolean(attResultRedirect.getValue()));

rMapping.setUrl(elResult.getTextTrim());

aMapping.getResults().put(rMapping.getName(),rMapping);

}

actionMapping.put(aMapping.getName(),aMapping);

}

}

}catch(Exceptione){

e.printStackTrace();

}

}

}

(七)利用反射機制找到自己的實列

packagecn.framework;

publicclassActionManager{

publicstaticActioncreateAction(StringclassName){

Class<?>clz=null;

try{

clz=Thread.currentThread().getContextClassLoader().loadClass(className);

}catch(Exceptionex){

ex.printStackTrace();

}

try{

if(clz==null){

clz=Class.forName(className);

}

}catch(Exceptionex){

ex.printStackTrace();

}

Actionaction=null;

try{

action=(Action)clz.newInstance();

}catch(Exceptionex){

ex.printStackTrace();

}

returnaction;

}

}

(八)寫一個業(yè)務(wù)邏輯

packagecn.action;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importcn.framework.Action;

publicclassLoginActionimplementsAction{

@Override

publicStringexecute(HttpServletRequestrequest,HttpServletResponseresponse){

return"success";

}

}

(九)核心控制器Servlet

packagecn.framework;

importjava.io.IOException;

importjavax.servlet.ServletConfig;

importjavax.servlet.ServletException;

importjavax.servlet.annotation.WebServlet;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

publicclassMVCServletextendsHttpServlet{

privatestaticfinallongserialVersionUID=1L;

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

}

ActionMappingManageramanager=null;

@Override

publicvoidinit(ServletConfigconfig)throwsServletException{

amanager=newActionMappingManager();

}

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

ActionMappingam=amanager.getActionMapping(getActionName(request));

Actionaction=ActionManager.createAction(am.getClassName());

Stringr=action.execute(request,response);

ResultMappingrm=am.getResults().get(r);

if(rm.isRedirect()){

response.sendRedirect(rm.getUrl());

}else{

request.getRequestDispatcher(rm.getUrl()).forward(request,response);

}

}

publicStringgetActionName(HttpServletRequestrequest){

StringactionName=null;

Stringuri=request.getRequestURI();

StringcontentPath=request.getContextPath();

StringactionPath=uri.substring(contentPath.length());

actionName=actionPath.substring(1,actionPath.indexOf(".")).trim();

returnactionName;

}

}

(十)修改web.xml

<servlet>

<servlet-name>MVCServlet</servlet-name>

<servlet-class>cn.framework.MVCServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MVCServlet</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

(十一)準備一個login.jsp頁面
自定義MVC框架
(十二)發(fā)布到Tomcat運行自定義MVC框架

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁題目:自定義MVC框架-創(chuàng)新互聯(lián)
URL分享:http://www.rwnh.cn/article38/jhjpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站內(nèi)鏈、關(guān)鍵詞優(yōu)化、網(wǎng)站收錄、全網(wǎng)營銷推廣、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計
宜都市| 甘南县| 克东县| 巴里| 衡水市| 凤阳县| 呼伦贝尔市| 金乡县| 西昌市| 石嘴山市| 中卫市| 巴彦淖尔市| 张家港市| 兴安县| 太仆寺旗| 唐海县| 临江市| 桂林市| 阿合奇县| 武清区| 乾安县| 海晏县| 绍兴县| 凤山市| 托克逊县| 会同县| 罗山县| 海宁市| 永丰县| 连南| 通州市| 钟祥市| 夏邑县| 鲁甸县| 海兴县| 崇左市| 二手房| 淳化县| 溧水县| 盈江县| 友谊县|