1. 程式人生 > >01-struts2配置詳解

01-struts2配置詳解

調試 dev efault nbsp config patch 錯誤 public include

1 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>
    <!-- i18n:國際化. 解決post提交亂碼 ,get提交亂碼原來怎麽解決還是怎麽解決-->
    <constant 
name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 指定訪問action時的後綴名 http://localhost:8080/struts2_day01/hello/HelloAction.do --> <constant name="struts.action.extension" value="action,,"></constant> <!-- 指定struts2是否以開發模式運行 1.熱加載主配置.(不需要重啟即可生效) 2.提供更多錯誤信息輸出,方便開發時的調試
--> <constant name="struts.devMode" value="true"></constant> <!-- package:將Action配置封裝.就是可以在Package中配置很多action. name屬性: 給包起個名字,起到標識作用.隨便起.不能與其他包名重復. namespace屬性:給action的訪問路徑中定義一個命名空間,可以直接就一個/ extends屬性: 繼承一個 指定包 abstract屬性:包是否為抽象的; 標識性屬性.標識該包不能獨立運行.專門被繼承
--> <package name="hello" namespace="/" extends="struts-default"> <!-- action元素:配置action類 name屬性: 決定了Action訪問資源名. class屬性: action的完整類名 method屬性: 指定調用Action中的哪個方法來處理請求 --> <action name="HelloAction" class="www.test.a_hello.HelloAction" method="hello"> <!-- result元素:結果配置 name屬性: 標識結果處理的名稱.與action方法的返回值對應. type屬性: 指定調用哪一個result類來處理結果,默認使用轉發. 標簽體:填寫頁面的相對路徑 --> <result name="success" type="dispatcher">/hello.jsp</result> </action> </package> <!-- 引入其他struts配置文件 --> <include file="www/test/b_dynamic/struts.xml"></include> <include file="www/test/c_default/struts.xml"></include> </struts>

2 struts2配置進階-動態方法調用

<?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>
    <!-- 配置動態方法調用是否開啟常量
            默認是關閉的,需要開啟
    -->
    <!-- <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <package name="dynamic" namespace="/dynamic" extends="struts-default">
        <action name="Demo1Action" class="www.test.b_dynamic.Demo1Action">
            <result name="success">/hello.jsp</result>
        </action>
    </package> -->
    
    <package name="dynamic" namespace="/" extends="struts-default">
        <action name="Demo1Action_*" class="www.test.b_dynamic.Demo1Action" method="{1}">
            <result name="success" type="dispatcher">/hello.jsp</result>
        </action>
    </package>
</struts>

3 struts2 配置進階-默認值

<?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="/" 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>

01-struts2配置詳解