Struts2基礎(chǔ)學(xué)習(xí)(二)

Struts2中的核心文件

1.web.xml
任何MVC框架都需要與Web應(yīng)用整合,這就不得不借用于web.xml文件,只有配置web.xml文件中Servlet才會(huì)被應(yīng)用加載

Model2:JSP+JavaBean+Servlet

通常,所有MVC框架都需要Web應(yīng)用加載一個(gè)核心控制器,對(duì)于Struts2框架而言,需要加載StrutsPerpareAndExecuteFilter,只負(fù)責(zé)Web應(yīng)用加載StrutsPerpareAndExecuteFilter,StrutsPerpareAndExecuteFilter將會(huì)加載Struts2框架。遇到.acton文件就會(huì)攔截并進(jìn)行處理

2.struts.xml
struts2的核心配置文件,在開(kāi)發(fā)過(guò)程中利用率最高。
該文件主要負(fù)責(zé)管理Action的映射,以及該Action包含的Result定義等。

struts.xml中包含的內(nèi)容:
1、全局屬性
2、用戶請(qǐng)求和相應(yīng)Action之間的對(duì)應(yīng)關(guān)系
3、Action可能會(huì)用到的參數(shù)和返回結(jié)果
4、各種攔截器的配置

3.struts.properties
struts2框架的全局屬性文件,自動(dòng)加載和strusts.xml在一個(gè)路徑
該文件包含很多key-value鍵值對(duì)。
這個(gè)文件可以不要,可以在structs.xml中進(jìn)行配置,使用constant元素可以替換
.dtd就是約束struts.xml中可以有哪些標(biāo)簽不能有哪些標(biāo)簽

可以通過(guò) <include file=“”>包含其他文件
可以把每個(gè)功能模塊獨(dú)立到一個(gè)xml配置文件中,然后用Include節(jié)點(diǎn)引用

<package>
package提供了將多個(gè)Action組織成為一個(gè)模塊的方式
package的名字必須是唯一的,可以在這個(gè)包上加一些拓展的包
<package name="包名" extends="繼承的父類的名稱" abstract設(shè)置package的屬性為抽象,抽象的package不能定義action的值, ture或false namespace 包的命名空間>

<interceptors>為攔截器
可以為攔截器定義name(名稱)和class(類路徑)
<interceptor-stack>攔截器棧

<default-interceptor-ref name="">定義默認(rèn)的攔截器,每個(gè)Action都會(huì)自動(dòng)引用如果

Action搜索順序

1、判斷 package是否存在

2、判斷action是否存在,若存在 , 則打開(kāi)。

3、action若不存在,則向上一級(jí)路徑中查找該action。直到默認(rèn)的namespace, 找不到,則報(bào)錯(cuò)


image.png

動(dòng)態(tài)方法調(diào)用

動(dòng)態(tài)方法調(diào)用是為了解決一個(gè)Action對(duì)應(yīng)多個(gè)請(qǐng)求的處理,以免Action太多。

三種方式:指定method屬性、感嘆號(hào)方式、通配符

1:繁雜不建議用(struts2.xml指明那個(gè)action執(zhí)行那個(gè)method=add)

    <action name="add" method="add" class="com.Action.hellowAction">
    <action name="update" method="update" class="com.Action.hellowAction">

2:不建議用(!后接執(zhí)行方法)

    <constant name="struts.enable.DynamicMethodInvocation" value="true">                                                </constant>
    <action name="hellowword" class="com.Action.hellowAction">
          <result >/result.jsp</result>
          <result name="add">/add.jsp</result>
          <result name="update">/update.jsp</result>
          </action>
    http://localhost:8080/struts_hellowworld/aaa/hellowworld!xxx.action
    http://localhost:8080/struts_hellowworld/aaa/hellowworld!add.action
    http://localhost:8080/struts_hellowworld/aaa/hellowworld!update.action

3:常用的動(dòng)態(tài)方法調(diào)用 (第一個(gè)代表{1}第二個(gè)={2}....)

    <constant name="struts.enable.DynamicMethodInvocation" value="false">                                                </constant>
    <action name="*_*" method="{2}" class="com.Action.{1}Action">
          <result >/{2}.jsp</result>
          <result name="add">/{2}.jsp</result>
          <result name="update">/{2}.jsp</result>
          </action>
    http://localhost:8080/struts_hellowworld/aaa/hellow_add.action
    http://localhost:8080/struts_hellowworld/aaa/hellow_update.action

Struts2---指定多個(gè)配置文件

1.如果有很多個(gè)Action的配置文件,則需要在struts.xml中使用<include file="fileName.xml"/>來(lái)包含其他的配置文件

2.struts文件中添加<constant name="struts.i18n.encoding" value="UTF-8"></constant>以防亂碼問(wèn)題的出現(xiàn)

3.配置文件和struts.xml的格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
...
</struts>

默認(rèn)action

為了提升體驗(yàn)度和網(wǎng)站的正常運(yùn)行配置默認(rèn)action。當(dāng)遇到突發(fā)狀況時(shí)均有默認(rèn)action來(lái)?yè)螆?chǎng)面。
<default-action-ref name="name"></default-action-ref>

在名為name的action中配置后臺(tái)action和前端展示

    <default-action-ref name="error"></default-action-ref>
    <action name="error">
        <result>/error.jsp</result>
    </action>

這兩個(gè)name要相同,訪問(wèn)錯(cuò)誤的action時(shí)跳轉(zhuǎn)到error.jsp

struts2后綴:

三種設(shè)置struts2后綴方式
1.struts.properties中

    struts.action.extension=action,do,struts2

2.xml中增加常量constant:

    <constant name="struts.action.extension" value="action,do,struts2"></constant>

3.在過(guò)濾器中配置intt-param參數(shù):

    <init-param>
    <param-name>struts.action.extension</param-name>
    <param-value>do,action,strtus2</param-value>
    </init-param>

接收參數(shù)的三種方式

1.使用Action的屬性接受參數(shù),在Action中定義需要接受的屬性,并寫(xiě)它的set/get方法。
直接在action類中創(chuàng)建相應(yīng)的屬性和getter和setter,和前端的name名字相同。eg:前端的username,在action類中就要建立一個(gè)private String username; Struts會(huì)自動(dòng)映射為這個(gè)屬性賦值

2.使用DomainModel接受參數(shù),創(chuàng)建實(shí)體類定義需要接受的屬性,并set/get方法,在Action中創(chuàng)建實(shí)體類名屬性。并在界面進(jìn)行指定。
使用DomainModel,將username 和password兩個(gè)屬性封裝為一個(gè)類User(必須是標(biāo)準(zhǔn)的JavaBean),在action中聲明這個(gè)屬性:private User user; 同時(shí)為user設(shè)置getter和setter;在前端中的name需要設(shè)置為user.name和user.password,才能映射成功

3.使用ModelDriver接受參數(shù),在Action中實(shí)現(xiàn)ModelDriver<實(shí)體類名>接口,并實(shí)現(xiàn)方法返回當(dāng)前需要轉(zhuǎn)換的對(duì)象,刪除set/get方法,并對(duì) 對(duì)象 進(jìn)行實(shí)例化,并取消指定。
使用ModelDriven<T>接口,這個(gè)action必須實(shí)現(xiàn)這個(gè)接口的public T getModel()方法。Action類實(shí)現(xiàn)ModelDriven <T>接口,重寫(xiě)getModle()方法,Action中不需要getter,setter此時(shí)聲明的屬性必須實(shí)例化,eg: private User user = new User(); 同時(shí)不需要getter和setter。前端的name也只需要寫(xiě)username和password就可以,不需要再加域了。這種方法時(shí)最推薦的方法,因?yàn)榭梢詼p少前后端的耦合

4.request

5.獲取List集合中的參數(shù)。獲取多個(gè)參數(shù)。

struts2處理流程

用戶請(qǐng)求(前提在web.xml中配置攔截,通過(guò)路徑)——>Struts框架(通過(guò)struts.xml配置)——>控制器Action(返回string類型邏輯視圖字符串)——>Struts框架(通過(guò)result標(biāo)簽)——>視圖資源。

Struts1返回結(jié)果類型:ActionForward。

Struts2返回結(jié)果類型:String(提供代碼復(fù)用性,有利于框架分離)。


image.png

處理結(jié)果類型(com.opensymphony.xwork2.Action)

SUCCESS:Action正確的執(zhí)行返回,返回相應(yīng)試圖,success是name屬性的默認(rèn)值。

NONE:表示Action正確的執(zhí)行完成,但并不返回任何視圖。

ERROR:表示Action執(zhí)行失敗,返回到錯(cuò)誤處理視圖。

LOGIN:Action因?yàn)橛脩魶](méi)有登陸的原因沒(méi)有正確執(zhí)行,將返回該登陸視圖,要求用戶進(jìn)行登陸驗(yàn)證。

INPUT:Action的執(zhí)行,需要從前端界面獲取參數(shù),一般在應(yīng)用中,會(huì)對(duì)這些參數(shù)進(jìn)行驗(yàn)證,如果驗(yàn)證沒(méi)有通過(guò),將自動(dòng)返回到該視圖。


image.png

自動(dòng)跳轉(zhuǎn)到input界面方式:
* 1.當(dāng)參數(shù)類型轉(zhuǎn)換錯(cuò)誤時(shí),如age輸入框中的類型是字母等情況,方法自動(dòng)返回input

image.png

image.png
  • 2.當(dāng)action中存在addFiledError時(shí):
    * 1)addFileError放在一般執(zhí)行方法,addFieldError("", "");語(yǔ)句后面有返回input的語(yǔ)句
    * 2)addFileError放在validate()中

*3.FileError的表現(xiàn)形式:
* 在jsp頁(yè)面中使用<s:fielderror/>標(biāo)簽,該標(biāo)簽name屬性為addFieldError方法中的參數(shù)fieldName,在jsp頁(yè)面中使用struts標(biāo)簽,
* 需要導(dǎo)入標(biāo)簽庫(kù) 語(yǔ)句:<%@ taglib prefix="s" uri="/struts-tags" %>

處理結(jié)果類型(處理結(jié)果是通過(guò)在struts.xml使用<result/>標(biāo)簽配置結(jié)果)

根據(jù)位置的不同,分為兩種結(jié)果:
局部結(jié)果:將<result>標(biāo)簽作為<action/>元素的子元素來(lái)處理。

全局結(jié)果:將<result/>作為<global-result/>元素的子元素的來(lái)處理,<global-result/>相當(dāng)于公共的處理結(jié)果集。
例如:

<package name="">

        <global-results>
        
              <result name="">..</result>
        
              <result  name="">..</result>
        
        <global-results>
        
        </package>
    <result>標(biāo)簽的子元素<param name="loaction" parse="">resource</param>:

name屬性的location:定義了該視圖對(duì)應(yīng)的實(shí)際視圖資源。
parse屬性:是否可以在實(shí)際視圖名字中使用OGNL表達(dá)式,struts默認(rèn)是true。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 什么是Struts2的框架Struts2是Struts1的下一代產(chǎn)品,是在 struts1和WebWork的...
    inke閱讀 2,340評(píng)論 0 50
  • 本文包括: 1、Struts 2 概述2、Struts 2 快速入門3、Struts 2 的執(zhí)行流程4、配置 st...
    廖少少閱讀 3,204評(píng)論 3 13
  • 第一節(jié):Struts2概述 Struts2是一個(gè)基于MVC設(shè)計(jì)模式的Web應(yīng)用框架,它本質(zhì)上相當(dāng)于一個(gè)servle...
    Seapp閱讀 1,465評(píng)論 0 1
  • Java是一種可以撰寫(xiě)跨平臺(tái)應(yīng)用軟件的面向?qū)ο蟮某绦蛟O(shè)計(jì)語(yǔ)言。Java 技術(shù)具有卓越的通用性、高效性、平臺(tái)移植性和...
    Java小辰閱讀 1,152評(píng)論 0 14
  • 昨晚上翻到了一個(gè)微博主到我的出生地拍攝的照片。我很是感慨,一邊像朋友吐槽我人生前幾年在這里度過(guò)的時(shí)光,一邊又在津津...
    30df555bcf8b閱讀 396評(píng)論 0 0

友情鏈接更多精彩內(nèi)容