什么是Struts2 Action呢?
實現了
public String execute() throws Exception;
的任何一個java類都可以被認為是Struts2 Action。
注意點:
1:方法名默認為execute,也可以自定義方法名。
2:返回值應該為:success,none,error,input和login;
Struts2 Action的實現方法?(三種)
1:
public class IndexAction1 {
public String execute(){
return "success";
}
}
2:
import com.opensymphony.xwork2.Action;
public class IndexAction2 implements Action{
public String execute(){
return SUCCESS;
}
}
3:
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction3 extends ActionSupport {
private static final long serialVersionUID = 1L;
@Override
public String execute(){
return ERROR;
}
}
請參考:com.opensymphony.xwork2.Action接口,com.opensymphony.xwork2.ActionSupport類。
Action接口定義了Struts2 Action應該實現的方法和5個字符串常量作為方法的返回值。
ActionSupport類是Struts2提供的一個具有基本實現的父類,方便用戶的使用。
視圖層的跳轉
在struts-core jar包中打開struts-default.xml文件,它是一個struts2的默認配置文件。在里面可以找到:
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
它說明了向視圖層跳轉時有10種方式:
默認方式為
dispatcher,不寫時就代表默認方式;
redirectAction:Struts2 Action的跳轉;
redirect: 與response.sendRedirect()相同;
dispatcher:與
Struts2 Action方法的調用(三種):
1:在
<action />中沒有定義method屬性,表示調用當前Action的execute();
<action name="index3" class="com.action.IndexAction3">
<result name="error">
/hello3.jsp
</result>
</action>
2: Wildcards
As an application grows in size, so will the number of action mappings. Wildcards can be used to combine similar mappings into one more generic mapping.
<action name="index1*" class="com.action.IndexAction1" method="{1}" >
<result name="success" type="redirectAction" >
index3
</result>
</action>
method中的1代表name中的第一個*;
3:Dynamic Method Invocation
Dynamic Method Invocation (DMI) will use the string following a "!" character in an action name as the name of a method to invoke (instead of
execute). A reference to "
Category!create.action", says to use the "Category" action mapping, but call the
create method instead.
<action name="index2!*" class="com.action.IndexAction2" method="{1}">
<result name="success">
/hello2.jsp
</result>
</action>
method中的1代表name中的第一個*;
實例:
package com.action;
public class IndexAction1 {
public String msg(){
return "success";
}
}
package com.action;
import com.opensymphony.xwork2.Action;
public class IndexAction2 implements Action{
public String execute(){
return SUCCESS;
}
public String succ(){
return SUCCESS;
}
}
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction3 extends ActionSupport {
@Override
public String execute(){
return ERROR;
}
}
<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="index1*" class="com.action.IndexAction1" method="{1}" >
<result name="success" type="redirectAction" >
index3
</result>
</action>
<action name="index2!*" class="com.action.IndexAction2" method="{1}">
<result name="success">
/hello2.jsp
</result>
</action>
<action name="index3" class="com.action.IndexAction3">
<result name="error">
/hello3.jsp
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
測試:
分別用以下URL,注意觀察和思考:
http://localhost:8080/struts2_0200_action/index3
http://localhost:8080/struts2_0200_action/index2!succ
http://localhost:8080/struts2_0200_action/index2
http://localhost:8080/struts2_0200_action/index1msg
注意:
<result name="error">
/hello3.jsp
</result>
name代表Action方法的返回值,缺省代表success;
type代表Action向視圖跳轉時的跳轉類型,缺省代表plainText。