2.struts2 namespace

1.package用來解決action重名的情況,類似java中的包名

2.result標籤默認名字爲success

3.若package中的namespace沒寫,則默認namespace爲”“

  • url發現一個action時,先找匹配的namespace對應的package中是否有該action,如果沒有,最後查找namespace爲空的package中是否有該action存在

4.複製一個新的web項目

  • 右鍵項目–ctrl+c–ctrl+v–右鍵新項目–properties–MyEclipse–Web–修改Web Context-root中的內容

5.action找到result的流程

  • action中有一個class屬性,如果不填寫時,默認class值爲ActionSupport這個類
  • 訪問到action時,先找到其class屬性對應的類,調用該類的execute方法,獲取一個String類型的返回值,通過該值匹配result的name屬性,返回對應result的內容
  • 自己寫action中的class類的三種方式
    a.直接寫一個類,裏面含有execute方法,裏面包含有execute方法,返回值爲String
    b.implements Action(函數式接口) 重寫其execute方法
    c.extends ActionSupport
  • 真正開發只用第三種,因爲ActionSupport已經封裝了一堆可以直接調用的方法,比較方便

6.修改jsp文件編碼格式

  • Window–Preferences–jsp–encoding改爲Chinese,National Standard–IANA改爲GB18030

7.路徑問題的說明

  • 如果服務器根據url無法找到正確的namespace,會返回web.xml中的welcome-file-list中的welcome-file對應的頁面,默認爲index.jsp
  • jsp文件中的相對路徑,不是相對該jsp文件的路徑,而是相對當前的action的路徑
    例:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
        <action name="hello">
            <result>
                /Hello.jsp
            </result>
        </action>
    </package>
</struts>

如果此時在/Hello.jsp中有連接

<a href="index.jsp">index.jsp</a>

則此時路徑爲http://localhost:8080/Struts2_0100_Package/user/index.jsp,而不是http://localhost:8080/Struts2_0100_Package/index.jsp

<a href="/index.jsp">index.jsp</a>

8.jsp文件中路徑的正確寫法

  • 在jsp文件中添加如下代碼

    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    

    basePath =request.getScheme()(“http”)+”://”+request.getServerName()(“localhost”)+”:”+request.getServerPort()(“8080”)+request.getContextPath()(“Struts2_0100_Package”)+”/”;
    即basePath爲:“http://localhost:8080/Struts2_0100_Introduction/”。

  • 修改方式1

<a href="<%=basePath %>index.jsp">index.jsp</a>

此時該路徑意思爲:“http://localhost:8080/Struts2_0100_Introduction/index.jsp

  • 修改方式2
    在head標籤內添加代碼
<base href="<%=basePath%>">

鏈接處代碼不變,此時下面左右的相對路徑都是相對”basePath”的路徑

9.動態方法調用(dmi)

  • 當url匹配到對應的action時,不一定非執行action對應的class的execute方法,可以在action中添加method屬性,則調用該指定方法返回result的名字
  • 可以使用動態方法調用,user爲namespace,hello爲action的name,executewsh爲action對應類中的一個方法例:”http://localhost:8080/Struts2_0100_Introduction/user/hello!executewsh“即可以直接調用該方法而不需在action中添加method屬性
    但要注意的是必須先開啓動態調用選項,即在struts.xml中添加如下代碼
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
  • 好處爲可以不用改變配置文件直接改變調用的方法

10.通配符的配置

<package name="default" namespace="/user" extends="struts-default">
        <action name="student*" method="student{1}" class="StartMethod">
            <result>
                /student{1}_success.jsp
            </result>
</package>
  • 當url路徑爲:“http://localhost:8080/Struts2_0100_Introduction/user/studentAdd/”時,struts2匹配到namespace爲”/user”,進而匹配其action,發現student*,即此時“”代替Add,此時{1}就代表第一個代表的字符串,即此時系統調用類”StartMethod”的studentAdd方法來獲取result的值,若studentAdd方法的返回值爲success,此時匹配到對應的result,返回頁面studentAdd_success.jsp

11.通配符比動態調用的優勢

  • 雖然動態調用可以調用指定方法,但是比如我想輸入studentAdd時返回studentAdd.jsp,輸入studentDel時返回studentDel.jsp這是做不到的,通配符處理方式只要約定好url地址,可以大大簡化struts.xml中的配置

12.url與action的匹配順序

  • 如果package中的action有多個,而且url中的action哪個都可以匹配,優先匹配最接近的,帶*的都屬於同級,同級比較,誰的代碼在前先匹配誰

13.eclipse上發佈web項目和myeclipse上發佈的區別

  • Myeclipse發佈web工程時,會將工程發佈到tomcat文件夾下的webapps文件夾下。而eclipse發佈web工程時,它默認不是發佈在tomcat下面的,所以在tomcat文件夾下的webapps裏沒有相應的工程。其實eclipse有自己的tomcat配置文件server.xml,其中定義了工程發佈的位置,不是在webapps下,這其中的原理跟tomcat的虛擬路徑類似。
  • eclipse的tomcat相關配置如下
    這裏寫圖片描述
  • 如果你想在eclipse中將工程直接發佈到tomcat安裝目錄之下,那麼就可以按照下面的方式來修改
    這裏寫圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章