第09講 struts2的struts.xml的配置
阿新 • • 發佈:2018-11-10
1,修改HeadFirstStruts2chapter02-02,改名:HeadFirstStruts2chapter02-03,修改web Project settings檔案,ForeStudent類繼承ActionSupport,BackStudent類繼承ActionSupport,
ForeStudent如下:
package com.cruise.action;
import com.opensymphony.xwork2.Action;
public class ForeStudent implements Action{
@Override
public
System.out.println("ForeStudent執行了");
return "success";
}
}
BackStudent如下:
package com.cruise.action;
import com.opensymphony.xwork2.Action;
public
@Override
public String execute() throws Exception {
System.out.println("BackStudent執行了");
return "success";
}
}
2,修改struts.xml檔案,增加名稱空間,標籤的的name包名沒有什麼特殊的含義,namespace名稱空間,定義了名稱空間,需要在瀏覽器的位址列上增加名稱空間的字首,
3,刪除多餘的包和jsp,保留success.jsp 檔案,新建ForeAaction和BackAshley
4,struts.xml檔案:
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>
<package name="foreground" namespace="/fore" extends="struts-default">
<action name="studentList" class="com.cruise.action.ForeStudent">
<result name="success">${pageContext.request.comtextPath}/success.jspresult>
action>
package>
<package name="background" namespace="/back" extends="struts-default">
<action name="studentList" class="com.cruise.action.BackStudent">
<result name="success">${pageContext.request.comtextPath}/success.jspresult>
action>
package>
struts>
4, 測試瀏覽器輸入http://localhost:8080/HeadFirstStruts2chapter02_03/back/studentList
http://localhost:8080/HeadFirstStruts2chapter02_03/fore/studentList
5,修改struts.xml檔案,在back包下的action標籤中增加method屬性,用於指定哪個方法。
<?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>
<package name="foreground" namespace="/fore" extends="struts-default">
<action name="studentList" class="com.cruise.action.ForeStudent">
<result name="success">${pageContext.request.comtextPath}/success.jsp</result>
</action>
</package>
<package name="background" namespace="/back" extends="struts-default">
<action name="studentList" class="com.cruise.action.BackStudent" method="show">
<result name="success">${pageContext.request.comtextPath}/success.jsp</result>
</action>
</package>
</struts>
6,修改BackStudent類,增加show方法,
package com.cruise.action;
import com.opensymphony.xwork2.Action;
public class BackStudent implements Action{
@Override
public String execute() throws Exception {
System.out.println("BackStudent執行了");
return "success";
}
public String show()throws Exception{
System.out.println("執行了BackStudent的show()方法");
return "success";
}
}
7, 測試瀏覽器輸入http://localhost:8080/HeadFirstStruts2chapter02_03/back/studentList