Struts2 ognl <s:action.../>標簽詳解(兩種實現方式)
最終目錄結構:

<s:action> 直意即可:用action標簽在jsp頁面中直接調用Action。
Description
This tag enables developers to call actions directly from a JSP page by specifying the action name and an optional namespace.The body content of the tag is used to render the results from the Action. Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified.
Parameters

給出源碼:
showActionTag.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";
<html xmlns=";
<body>
<s:action name="action" executeResult="true" >
<s:param name="name">LiuKuan</s:param>
<h3>包含結果的頁面</h3>
</s:action>
</body>
</html></pre>
注解:<s:action name="要被執行的action,即struts.xml中的<action>標簽的name屬性名" executeResult=“true”(即包含執行的結果頁面result.jsp中顯示的內容) >
傳遞參數<s:param name="參數名">參數值</s:param>,要注意經過服務器跳轉的頁面showActionTag.jsp傳遞的參數經過Action類處理,即set方法使Action"實例化",
故而一定要定義一個set方法,其后綴與傳參同名,即在下面的Action類中定義setName方法,通過execute方法返回SUCCESS實現頁面的跳轉。
ActionTag.java:
package action;
import com.opensymphony.xwork2.ActionSupport;
public class ActionTag extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
return SUCCESS;
}
}</pre>
struts.xml:
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ";
<struts>
<constant name="struts.i18n.encoding" value="gb2312"/>
<package name="OGNLTAG" extends="struts-default">
<action name="action" class = "action.ActionTag">
<result name="success">/jsp/result.jsp</result>
</action>
</package>
</struts></pre>
result.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<h5>所包含的頁面:</h5>
<s:property value="name" />
</body>
</html> 注解:因為傳遞的參數是直接成為Action類的實例屬性的,故為OGNL根對象,不用加符號“#”于屬性"name"前。
web.xml中配置FilterDispatcher在此省略。
運行 http://localhost:8080/OGNLTAG/jsp/showActionTag.jsp 結果:
包含結果的頁面
所包含的頁面:
LiuKuan
-------------------------------------------------------------------------------------------------------------
在showActionTag.jsp中,若 executeResult="false"(即不顯示結果視圖),則輸出結果:
包含結果的頁面
--------------------------------------------------------------------------------------------------------------
若executeResult="true",ignoreContextParams="true"(即忽略傳遞到Action中的參數);
輸出結果竟與最開始的一樣,這是因為<s:param name="name">LiuKuan</s:param>(name變量的值為LiuKuan字符串,而非LiuKuan變量),
可能傳遞的參數不是"request parameters"吧,可能就是直接傳遞的參數吧。
若換成是<s:param name="name" value="LiuKuan" />(LiuKuan是個對象值),則輸出結果如下:(包含了頁面,但不包含傳參)
包含結果的頁面
所包含的頁面:
---------------------------------------------------------------------------------------------------------------
本人運行環境:myeclipse8.6+jboss5.1+jvm1.6。
----------------------------------------------------------------------------------------------------------------
下面是另一種實現方式:
showActionTag.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>顯示Action視圖和參數?</title>
</head>
<body>
<h3 align="left">
顯示結果視圖
</h3>
<s:action name="actionTag" executeResult="true"></s:action>
<h3 align="left">
不顯示結果視圖
</h3>
<s:action name="actionTag" executeResult="false"></s:action>
<h3 align="left">
忽略傳遞的Action參數
</h3>
<s:action name="actionTag" executeResult="true" ignoreContextParams="true"></s:action>
</body>
</html> struts.xml:
<?xml version="1.0" encoding="gb2312"?>
<!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.i18n.encoding" value="gb2312"/>
<package name="OGNLTAG" extends="struts-default">
<action name="actionTag" class="action.ActionAction">
<result name="success">/jsp/actionTag.jsp</result>
</action>
</package>
</struts> 注釋:其實上面是用bean標簽來直接與Action類交互,只不過這里是用struts.xml來進行導航罷了。
ActionAction.java:
package action;
import com.opensymphony.xwork2.ActionSupport;
public class ActionAction extends ActionSupport {
//傳入的Action參數值
private String param;
public String execute() throws Exception{
return SUCCESS;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
} actionTag.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>action標簽使用范例</title>
</head>
<body>
<!-- 顯示Action參數 -->
<s:property value="param"/>
</body>
</html> 在地址欄輸入:http://localhost:8080/OGNLTAG/jsp/showActionTag.jsp?param=frank (注意自己手動給出參數?param=frank)結果為:
顯示結果視圖
frank
不顯示結果視圖
忽略傳遞的Action參數