ction中的属性名的含义:
name:对应一个struts2的请求的名字(或对一个servletPath,但去除‘/’和扩展名),不包含拓展名
class:需要调用的Action,默认值为:com.opensymphony.xwork2.ActionSupport
method:的默认值为:execute , 可以自定义方法,如"SaveAction"action中的save方法
result:结果,表示action方法执行后可能返回的一个结果,所以一个action节点可能会有多个result子节点,多个result子节点使用name来区分
创建Action的写法:
1.直接新建一个class,在class中创建一个execute()方法,在execute()方法里写具体的操作,然后在struts.xml的action添加class的属性值为"包名.类名"。
public class TestAction1 {
public String execute(){
System.out.println("TestAction1 execute.....");
return "success";
}
}
struts.xml
<span style="font-size:18px;">struts.xml</span>
<span style="font-size:18px;">
</span>
<package name="default" namespace="/" extends="struts-default">
<action name="test1" class="com.zucc.action.TestAction1" >
<result >/Hello.jsp</result>
</action>
</package>
2.新建一个class并实现Action接口,实现Action接口的execute()方法,在execute()方法里写具体的操作,然后在
struts.xml的action添加class的属性值为"包名.类名"。
import com.opensymphony.xwork2.Action;
public class TestAction2 implements Action{
@Override
public String execute() throws Exception {
System.out.println("TestAction2 execute.....");
return SUCCESS;
}
}
struts.xml
<span style="font-size:18px;">struts.xml</span>
<span style="font-size:18px;">
</span>
<package name="default" namespace="/" extends="struts-default">
<action name="test2" class="com.zucc.action.TestAction2" >
<result >/Hello.jsp</result>
</action>
</package>
3.新建一个class并继承ActionSupport类,重写ActionSupport类中的execute()方法,在execute()方法里写具体的操作,然后在struts.xml的action添加class的属性值为"包名.类名"。
import com.opensymphony.xwork2.ActionSupport;
public class TestAction3 extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("TestAction3 execute.....");
return SUCCESS;
}
}
struts.xml
<span style="font-size:18px;">struts.xml</span>
<span style="font-size:18px;">
</span>
<package name="default" namespace="/" extends="struts-default">
<action name="test3" class="com.zucc.action.TestAction3" >
<result >/Hello.jsp</result>
</action>
</package>