struts2框架學習總結

2021-06-09 05:30:39 字數 1828 閱讀 3801

一、struts2的作用:實現檢視和請求分開;

二、在瀏覽器中任意輸入後面路徑的位址時,為了防止找不到頁面或出現錯誤,則可以設定乙個預設頁面,比如首頁,可以在

struts.xml中配置:

<

default-action-ref

name

="index"

>

default-action-ref

>

<

action

name

="index"

>

<

result

>

/index.jsp

result

>

action

>

三、呼叫action中某個方法時可以有兩種方法:

a)通過action標籤中的method方法,如:

<

action

name="

hello"

class

="com.liyang.action.studentaction"

method

="myexecute"

>

b)通過dmi即動態方法呼叫,在瀏覽器中指定要呼叫的方法,比如:

則預設呼叫myexecute方法。

但不能這樣呼叫加上.action:

注意:當兩種情況都使用時:dmi呼叫優先,即呼叫dmi指定的方法.

四、在配置action時萬用字元的使用:

<

action

name

="*_*"

class

="com.liyang.action.action"

method=""

>

其中每個*用來匹配從瀏覽器傳來的值,這樣可以動態指定action和action中的方法。

代表第乙個*的值。

注意:在做專案時一定要進行命名規範和相應的約定,這樣可以使配置檔案大大簡化,

即:約定優於配置。

五、struts.xml中的

<

constant

name=""

value=""

>

constant

>

其中的name都在

default.properties

已經定義了並設定了初始值,但可以通過此標籤進行更改。

比如解決字元編碼問題:

struts.i18n.encoding=utf-8

開發模式:

struts.devmode = false

六.struts路徑問題:

應該使用絕對路徑,通過request物件可以得到當前路徑,如:

<%

string path = request.getcontextpath();

string basepath = request.getscheme()+

"://"

+request.getservername()+

":"+request.getserverport()+path+

"/";

%>

這樣在任意介面返回首頁時可以通過此變數可以輕鬆返回。

七、當jsp

編碼為utf-8

時,如果是

get方法進行中文傳參,會出現亂碼,但當進行

post

方法進行中文傳參時不會出現亂碼。

Struts2框架校驗總結

1.找到login validation.xml 檔案struts2 blank web inf classes example 2.將login 修改為對應的 action名稱 3.is null 4.配置對應的 struts.xml 的input頁面 5.配置對應的 jsp頁面 對應的name ...

struts2學習總結

struts.properties配置 struts.properties檔案在web inf classes目錄下存放。這個檔案用來配置struts2系統的一些基本規約,所有在struts.properties中配置的內容都可以在struts.xml中配置,或者web.xml中在struts2 f...

寶寶Struts2學習總結

success.jsp fail.jsp global a2 methodtest add.jsp methodtest update.jsp methodtest delete.jsp methodtest add.jsp methodtest update.jsp user.jsp a2 suc...