更新時間:2021-03-02 16:13:00 來源:動力節(jié)點 瀏覽2176次
Struts2框架是MVC流程框架,適合分層開發(fā)??蚣軕?yīng)用實現(xiàn)不依賴于Servlet,使用大量的攔截器來處理用戶請求,屬于無侵入式的設(shè)計。
Struts2框架的流程原理

1)請求先到達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)路勁。
<?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類;默認情況下執(zhí)行類中的execute方法,如果想指定方法,需要在<action>標簽中添加method屬性;<result>標簽配置結(jié)果跳轉(zhuǎn)路徑。根據(jù)Action類中方法返回的String字符串,去匹配result標簽中的name值,進行跳轉(zhuǎn)。
3)在web.xml文件中配置核心控制器
<!-- 配置框架的核心調(diào)度器 -->
<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>
零基礎(chǔ)學(xué)習(xí)Struts2,動力節(jié)點推出的經(jīng)典Struts2視頻教程全套免費下載,這是一套超級詳細的Struts2入門教程,本套Struts2視頻教程適合Struts2框架零基礎(chǔ)學(xué)員學(xué)習(xí),這套入門教程通俗易懂,是你學(xué)習(xí)Struts2框架的好幫手
以上就是動力節(jié)點Java培訓(xùn)機構(gòu)的小編針對“Struts2學(xué)習(xí)視頻,通俗易懂的入門教程”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。