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 String execute() throws Exception {
System.out.println("ForeStudent执行了");
return "success";
}
}
BackStudent如下:
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";
}
}
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