一:struts.xml中的namespace
<package>标签中的namespace决定URL里“项目名之后的路径”,
比如:
<package name="default" namespace="/1/2/3" extends="struts-default">
访问路径就是:http://127.0.0.1:8080/Struts2First/1/2/3/hello
(Struts2First是项目名)
namespace=""的时候,项目名后边的路径可以随意写,只要action名写对就行了
<package name="default" namespace="" extends="struts-default">
访问路径是:http://127.0.0.1:8080/Struts2First/11111/22222/hello也能访问得到
二:自定义测试action
src下增加一个自定义的action
package com.rt.struts2.actionDemo;
import com.opensymphony.xwork2.ActionSupport;
//继承于ActionSupport是为了得到其中定义好的方法
public class MyAction_01 extends ActionSupport
{
@Override
public String execute()
{
System.out.println("__MyAction_01__");
return "success";
}
}
struts.xml中增加一个action的配置
<package name="ma1" namespace="/" extends="struts-default">
<action name="myAction01" class="com.rt.struts2.actionDemo.MyAction_01"><!-- 包.类名称 -->
<result >
/helloStruts2.jsp
</result>
</action>
</package>
自定义的MyAction_01继承于ActionSupport。
在访问到某个action时,Struts2会new一个新的对应类的对象出来,
调用其中的execute()方法,接口规定这个方法需要返回字串“success”
没有自定义action的时候(比如helloworld),自动执行ActionSupport
ActionSupport的API:http://struts.apache.org/2.0.9/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html