一文了解自定義MVC框架實現(xiàn)_第1頁
一文了解自定義MVC框架實現(xiàn)_第2頁
一文了解自定義MVC框架實現(xiàn)_第3頁
一文了解自定義MVC框架實現(xiàn)_第4頁
一文了解自定義MVC框架實現(xiàn)_第5頁
已閱讀5頁,還剩6頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第一文了解自定義MVC框架實現(xiàn)解決方案:建一個模型驅(qū)動接口,使BookAction實現(xiàn)該接口,在中央控制器中將所有要接收的參數(shù)封裝到模型接口中,從而達(dá)到簡便的效果。

ModelDriven類(驅(qū)動接口類):

packagecom.ycx.framework;

*模型驅(qū)動接口,接收前臺JSP傳遞的參數(shù),并且封裝到實體類中

*@author楊總

*@paramT

publicinterfaceModelDrivenT{

//拿到將要封裝的類實例ModelDriven.getModel()newBook();

TgetModel();

}

DispatcherServlet中央控制器類:

@Override

protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{

//http:localhost:8080/mvc/book.actionmethodName=list

Stringuri=req.getRequestURI();

//拿到/book,就是最后一個“/”到最后一個“.”為止

uri=uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));

//Actionaction=actions.get(uri);

//相比于上一種從map集合獲取子控制器,當(dāng)前需要獲取config.xml中的全路徑名,然后反射實例化

ActionModelactionModel=configModel.pop(uri);

if(actionModel==null){

thrownewRuntimeException("action配置錯誤");

Stringtype=actionModel.getType();

//type是Action子控制器的全路徑名

try{

Actionaction=(Action)Class.forName(type).newInstance();

//action是bookAction

if(actioninstanceofModelDriven){

ModelDrivenmd=(ModelDriven)action;

//model指的是bookAction中的book實例

Objectmodel=md.getModel();

//要給model中的屬性賦值,要接收前端jsp參數(shù)req.getParameterMap()

//PropertyUtils.getProperty(bean,name)

//將前端所有的參數(shù)值封裝進(jìn)實體類

BeanUtils.populate(model,req.getParameterMap());

System.out.println(model);

//正式調(diào)用方法前,book中的屬性要被賦值

action.execute(req,resp);

}catch(Exceptione){

e.printStackTrace();

}

BookAction類:(注意,在上一張同一個類里,沒有實現(xiàn)ModelDriver接口,而如下bookaction類實現(xiàn)了ModelDriver接口,把接受多個參數(shù)的屬性值語句注釋,達(dá)到了簡便的效果)

packagecom.ycx.web;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importcom.ycx.entity.Book;

importcom.ycx.framework.Action;

importcom.ycx.framework.ActionSupport;

importcom.ycx.framework.ModelDriven;

publicclassBookActionextendsActionSupportimplementsModelDrivenBook{

privateBookbook=newBook();

privatevoidload(HttpServletRequestreq,HttpServletResponseresp){

System.out.println("在同一個servlet中調(diào)用load方法");

privatevoidlist(HttpServletRequestreq,HttpServletResponseresp){

System.out.println("在同一個servlet中調(diào)用list方法");

privatevoidedit(HttpServletRequestreq,HttpServletResponseresp){

System.out.println("在同一個servlet中調(diào)用edit方法");

privatevoiddel(HttpServletRequestreq,HttpServletResponseresp){

System.out.println("在同一個servlet中調(diào)用del方法");

privatevoidadd(HttpServletRequestreq,HttpServletResponseresp){

//Stringbid=req.getParameter("bid");

//Stringbname=req.getParameter("bname");

//Stringprice=req.getParameter("price");

Bookbook=newBook();

//book.setBid(Integer.valueOf(bid));

//book.setBname(bname);

//book.setPrice(Float.valueOf(price));

//bookDao.add(book);

System.out.println("在同一個servlet中調(diào)用add方法");

@Override

publicBookgetModel(){

returnnull;

Debug運行效果如下:

其中關(guān)于解決參數(shù)冗余問題關(guān)鍵代碼是:

BeanUtils.populate(bean,req.getParameterMap());

???????//將該對象要接受的參數(shù)值封裝到對應(yīng)的對象中

三、對于方法執(zhí)行結(jié)果轉(zhuǎn)發(fā)重定向優(yōu)化

解決跳轉(zhuǎn)方式問題:

在我們跳轉(zhuǎn)到另一個界面時,需要許很多關(guān)于跳轉(zhuǎn)方式的代碼,有重定向,有轉(zhuǎn)發(fā),例如:

重定向:resp.sendRedirect(path);

轉(zhuǎn)發(fā):req.getRequestDispatcher(path).forward(req,resp);

這些代碼往往要寫很多次,因此通過配置config文件來解決此類問題;

例如這一次我跳轉(zhuǎn)增加頁面時是轉(zhuǎn)發(fā),跳轉(zhuǎn)查詢界面是重定向:

主界面代碼如下:

ahref="${pageContext.request.contextPath}/book.actionmethodName=addbid=989898bname=laoliuprice=89"增加/a

ahref="${pageContext.request.contextPath}/book.actionmethodName=list"查詢/a

config.xml:

xmlversion="1.0"encoding="UTF-8"

config

actionpath="/book"type="com.ycx.web.BookAction"

forwardname="success"path="/demo2.jsp"redirect="false"/

forwardname="failed"path="/demo3.jsp"redirect="true"/

/action

/config

思路:

1、當(dāng)點擊增加或者編輯時,首先跳轉(zhuǎn)的是中央控制器類(DispatchServlet):獲取到url,url將決定要跳到哪一個實體類,

2、之后進(jìn)入ActionSupport(子控制器接口實現(xiàn)類)通過methodname要調(diào)用什么方法,

3、再然后進(jìn)入到BookAction中調(diào)用methodname方法,找到對應(yīng)的返回值,

4、通過返回值在進(jìn)入到config文件找到path屬性,之后在中央控制器中進(jìn)行判斷,來決定是重定向還是轉(zhuǎn)發(fā)。

中央控制器(DispatcherServlet):

Stringresult=action.execute(req,resp);

ForwardModelforwardModel=actionModel.pop(result);

//if(forwardModel==null)

//thrownewRuntimeException("forwardconfigerror");

Stringpath=forwardModel.getPath();

//拿到是否需要轉(zhuǎn)發(fā)的配置

booleanredirect=forwardModel.isRedirect();

if(redirect)

//${pageContext.request.contextPath}

resp.sendRedirect(req.getServletContext().getContextPath()+path);

else

req.getRequestDispatcher(path).forward(req,resp);

}catch(Exceptione){

e.printStackTrace();

}

ActionSupport(子控制器接口實現(xiàn)類):

packagecom.ycx.framework;

importjava.lang.reflect.Method;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

publicclassActionSupportimplementsAction{

@Override

publicStringexecute(HttpServletRequestreq,HttpServletResponseresp){

StringmethodName=req.getParameter("methodName");

//methodName可能是多種方法

//前臺傳遞什么方法就調(diào)用當(dāng)前類的對應(yīng)方法

try{

Methodm=this.getClass()//BookServlet.Class

.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);

m.setAccessible(true);

//調(diào)用當(dāng)前類實例的methodName方法

return(String)m.invoke(this,req,resp);

}catch(Exceptione){

e.printStackTrace();

returnnull;

}

BookAction(圖中標(biāo)記的為返回值):

config文件(圖中name為返回值,path為要跳轉(zhuǎn)的界面路徑名,redirect為跳轉(zhuǎn)方式):

demo2界面:

%@pagelanguage="java"contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%

!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""/TR/html4/loose.dtd"

html

head

metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"

titleInserttitlehere/title

/head

body

/body

/html

demo3界面:

%@pagelanguage="java"contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%

!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""/TR/html4/loose.dtd"

html

head

metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"

titleInserttitlehere/title

/head

body

/body

/html

運行結(jié)果:

1、當(dāng)點擊增加時,跳轉(zhuǎn)到demo2界面,顯示效果如下

2.當(dāng)點擊查詢時,跳轉(zhuǎn)到demo3界面,顯示效果如下:

四、框架配置可變

如果config.xml文件名改成mvc.xml,該程序是否還可因運行呢?答案肯定是不行的。

因為ConfigModelFactory類里就定義了它的默認(rèn)路徑名如下:

我們可以在DispatcherServlet類里的init初始化里設(shè)置它的配置地址:

@Override

publicvoidinit()throwsServletException{

//actions.put("/book",newBookAction());

//actions.put("/order",newBookAction());

try{

//配置地址

//getInitParameter的作用是拿到web.xml中的servlet信息配置的參數(shù)

StringconfigLocation=this.getInitParameter("configLocation");

if(configLocation==null||"".equals(configLocation))

configModel=ConfigModelFactory.build();

else

co

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論