1. 程式人生 > >struts2框架學習筆記2:配置詳解

struts2框架學習筆記2:配置詳解

true class 規範 開發規範 刪除用戶 建議 類名 esp 需要

核心配置文件:

<?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>
    <!-- package:將Action配置封裝.就是可以在Package中配置很多action.
            name屬性: 給包起個名字,起到標識作用.隨便起.不能其他包名重復.
            namespace屬性:給action的訪問路徑中定義一個命名空間
            extends屬性(必填): 繼承一個 指定包(這裏使用的是默認)
            abstract屬性(補充):包是否為抽象的; 標識性屬性.標識該包不能獨立運行.專門被繼承
      
--> <package name="hello" namespace="/hello" extends="struts-default" > <!-- action元素:配置action類 name屬性: 決定了Action訪問資源名,註意不是標識作用 class屬性: action類的完整類名 method屬性: 指定調用Action類中的哪個方法來處理請求 --> <action name="HelloAction"
class="hello.HelloAction" method="hello" > <!-- result元素:結果配置 name屬性: 標識結果處理的名稱.與action方法的返回值對應. type屬性: 指定調用哪一個result類來處理結果,默認使用轉發dispatcher. 標簽體:填寫頁面的相對路徑 --> <result name="success" type="dispatcher"
>/hello.jsp</result> </action> </package> </struts>

引入其他配置文件:

    <include file="dynamic/struts.xml"></include>

src下的相對路徑

常量配置:

在struts2核心包下有默認的properties配置文件,當我們需要修改的時候,

第一種方式示例:

自己新建一個配置文件即可

struts.properties:

struts.i18n.encoding=UTF-8

第二種方式示例:

在核心配置文件中寫:

    <constant name="struts.i18n.encoding" value="UTF-8"></constant>

第三種方式示例:

在web.xml種:

  <context-param>
      <param-name>struts.i18n.encoding</param-name>
      <param-value>UTF-8</param-value>
  </context-param>

實際開發中,只使用第二種,其他兩種了解即可

其他常量配置(在核心配置文件中):

    <!-- i18n:國際化. 解決post提交亂碼(不解決get亂碼,自行解決) -->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <!-- 指定訪問action時的後綴名,必須訪問Action.do 
        http://localhost:8080/struts2/hello/HelloAction.do
    -->
    <constant name="struts.action.extension" value="do"></constant>
    <!-- 指定struts2是否以開發模式運行
            1.熱加載主配置.(不需要重啟即可生效)
            2.提供更多錯誤信息輸出,方便開發時的調試
     -->
    <constant name="struts.devMode" value="true"></constant>

還有幾個常量配置在Spring中使用,這裏暫不介紹

動態方法調用:

這裏一個Action中有多個方法:

package dynamic;

//動態方法調用
public class Demo1Action {
    public String add() {
        System.out.println("添加用戶!");
        return "success";
    }

    public String delete() {
        System.out.println("刪除用戶!");
        return "success";
    }

    public String update() {
        System.out.println("修改用戶!");
        return "success";
    }

    public String find() {
        System.out.println("查找用戶!");
        return "success";
    }

}

當前包下新建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>
        <!--動態方法調用方式1(不建議)
         配置動態方法調用是否開啟常量
                默認是關閉的(false),需要開啟
        配置完成後訪問....../dynamic/Demo1Action!add即可(!格式固定)
         -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
    
        <package name="dynamic" namespace="/dynamic" extends="struts-default" >
            <!-- 動態方法調用方式2:通配符方式(推薦)
                 使用{1} 取出第一個星號通配的內容
                 訪問:/dynamic/Demo1Action_add
              -->
            <!--第一種方式
            <action name="Demo1Action" class="dynamic.Demo1Action">
            -->
            <action name="Demo1Action_*" class="dynamic.Demo1Action" method="{1}" >
                <result name="success" >/hello.jsp</result>
            </action>
        </package>
</struts>

Struts2的默認配置:

package default;

//測試默認配置
public class Demo2Action {
    
    public String execute(){
        
        System.out.println("default");
        
        return "success";
    }
    
}

<?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>
        <package name="default" namespace="/default" extends="struts-default" >
            <!-- 找不到包下的action,會使用Demo2Action作為默認action處理請求 -->
            <default-action-ref name="Demo2Action"></default-action-ref>
            <!-- method的默認屬性:execute  -->
            <!-- result的name默認屬性:success  -->
            <!-- result的type默認屬性:dispatcher 轉發  -->
            <!-- class默認屬性:com.opensymphony.xwork2.ActionSupport -->
            <action name="Demo2Action">
                <result>/hello.jsp</result>
            </action>
        </package>
</struts>

這裏沒有配置method,但是默認尋找execute方法

class默認值:找到這個類並調用它的execute方法,這個方法直接返回"success"

struts2框架學習筆記2:配置詳解