【基於初學者】struts xml配置,初學者strutsxml

來源:互聯網
上載者:User

【基於初學者】struts xml配置,初學者strutsxml

01:匯入Jar包

:http://struts.apache.org/

將下好的jar包放導WEB-INF下的lib檔案夾下

02:建立Action:com.action.LoginAction(2.0版本以上無須加Action)

繼承ActionSupport(ActionSupport類是一個工具類,它已經實現了Action介面。除此之外,它還實現了Validateable介面,提供了資料校正功能。通過繼承該ActionSupport類,可以簡化Struts 2的Action開發。)

實現預設方法execute(必須有傳回值,String資料類型)

03:action配置

01):在src檔案夾下面建立一個struts.xml

 

02):匯入DTD檔案(可以在struts核心包中的struts-default.xml檔案裡找到):
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

03):建立根節點<struts>

04):在<struts>節點裡面建立節點<package name="名稱" extends="struts-default"(值固定) >

05):在<package>節點裡面建立<action name="訪問名稱(不需要加action,訪問的時候需要加上action,不能重複)"  class="com.action.LoginAction"(實作類別全路徑)  method="方法名"(預設執行execute方法)>

06):在<action>節點裡面建立<result>(沒有傳回值不需要配置):

<result name="execute方法的傳回值"  type="dispatcher"(設定轉寄還是重新導向redirect,預設是轉寄dispatcher)>跳轉的頁面</result>

07):萬用字元(有多個方法)

<action name="訪問名稱*" class="全路徑名" method="{1}"( name的名稱的第一個星號,星號可以有多個)></action>

04:配置struts2的過濾器(在web.xml裡面配置)

<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>*.action</url-pattern>
</filter-mapping>

05):Action擷取表單資料:

01):ActionContext類

建立:ActionContext  actionContext=ActionContext.getContext();

擷取表單資料:Map Arrays=actionContext.getParameters()

讀取值:Arrays.tostring();

02):ServletActionContext類:

HttpServletRequest request = ServletActionContext.getRequest();

和servlet使用request一致,採用requert擷取值

03):使用介面注入:

實現介面ServletRequestAware

06):一個簡單的偽登入:

LoginAction:

 

struts.xml:

介面就是一個普通的form提交,action地址為:loginlogin.action,比較簡單就沒放出來了

希望對看的人有協助

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.