本文為大家講解了struts2框架的入門知識,供大家參考,具體內容如下
1、struts2框架介紹
struts2框架是mvc流程框架,適合分層開發。框架應用實現不依賴于servlet,使用大量的攔截器來處理用戶請求,屬于無侵入式的設計。
2、struts2框架的流程原理
1)請求先到達filter中央控制器
2)然后為action創建代理類
3)將各個服務存放在攔截器中,執行完攔截器后再去執行action類行action類,action類調用service,再調用dao
4)得到結果字符串,創建result對象
5)轉向相應的視圖。
程序流程圖如下:
3、框架的使用
框架為我們做好了封裝,使用起來就按照步驟,配置幾個xml文件就行拉。
1)導入jar包
2)拷貝struts.xml文件
將拷貝的配置文件放在根目錄src下。struts.xml文件主要是配置請求路徑對應action類的,以及結果跳轉路勁。
1
2
3
4
5
6
7
8
9
10
11
|
<?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對應執行struts2.action包下面的helloworldaction類;默認情況下執行類中的execute方法,如果想指定方法,需要在<action>標簽中添加method屬性;<result>標簽配置結果跳轉路徑。根據action類中方法返回的string字符串,去匹配result標簽中的name值,進行跳轉。
3)在web.xml文件中配置核心控制器
1
2
3
4
5
6
7
8
9
|
<!-- 配置框架的核心調度器 --> <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> |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。