生活随笔
收集整理的這篇文章主要介紹了
Struts 2
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1,Struts2框架介紹
? Struts2框架是MVC流程框架,適合分層開發(fā)。框架應(yīng)用實現(xiàn)不依賴于Servlet,使用大量的攔截器來處理用戶請求,屬于無侵入式的設(shè)計。
?
?
2,Struts2框架的流程原理
1)請求先到達(dá)Filter中央控制器
2)然后為Action創(chuàng)建代理類
3)將各個服務(wù)存放在攔截器中,執(zhí)行完攔截器后再去執(zhí)行action類行action類,action類調(diào)用service,再調(diào)用dao
4)得到結(jié)果字符串,創(chuàng)建result對象
5)轉(zhuǎn)向相應(yīng)的視圖。
程序流程圖如下:
?
?
3,框架的使用
? 框架為我們做好了封裝,使用起來就按照步驟,配置幾個xml文件就行拉。
? 1)導(dǎo)入jar包
??
? 2)拷貝struts.xml文件
? 將拷貝的配置文件放在根目錄src下。struts.xml文件主要是配置請求路徑對應(yīng)action類的,以及結(jié)果跳轉(zhuǎn)路勁。
[html]?view plaincopy print?
<?xml?version="1.0"?encoding="UTF-8"??>?? <!DOCTYPE?struts?PUBLIC?? ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"?? ????"http://struts.apache.org/dtds/struts-2.0.dtd">?? <struts>?? ????<package?name="example"?namespace="/example"?extends="struts-default">?? ????????<action?name="HelloWorld"?class="struts2.action.HelloWorldAction">?? ????????????<result?name="success">/success.jsp</result>?? ????????</action>?? ????</package>?????? </struts>?? ? 代碼說明:訪問/example/HelloWorld.action對應(yīng)執(zhí)行struts2.action包下面的HelloWorldAction類;默認(rèn)情況下執(zhí)行類中的execute方法,如果想指定方法,需要在<action>標(biāo)簽中添加method屬性;<result>標(biāo)簽配置結(jié)果跳轉(zhuǎn)路徑。根據(jù)Action類中方法返回的String字符串,去匹配result標(biāo)簽中的name值,進(jìn)行跳轉(zhuǎn)。
? ?
? 3)在web.xml文件中配置核心控制器
[html]?view plaincopy print?
?? <filter>?? ????<filter-name>struts2</filter-name>?? ????<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>?? </filter>?? <filter-mapping>?? ????<filter-name>struts2</filter-name>?? ????<url-pattern>/*</url-pattern>?? </filter-mapping>?? ? ?
總結(jié)
以上是生活随笔為你收集整理的Struts 2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。