Struts2 ognl <s:action.../>标签详解(两种实现方式)

jopen 8年前

最终目录结构:

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

Struts2 ognl <s:action.../>标签详解(两种实现方式)

给出源码:

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">   <body>        <s:action name="action" executeResult="true" >    <s:param name="name">LiuKuan</s:param>            <h3>包含结果的页面</h3>       </s:action>     </body>  </html>

注解:<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;    }  }

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="action" class = "action.ActionTag">       <result name="success">/jsp/result.jsp</result>      </action>            </package>  </struts>

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参数