分享

jsp页面中包含<s:action></s:action>标签的含义

 英昌知识汇聚馆 2011-11-21

数据标签主要用于提供各种数据访问相关的功能,包含显示一个Action里的属性,以及生成国际化输出等功能。
action
标签
 
使用Action标签可以允许在JSP页面中直接调用Action,因为需要调用Action,故可以指定需要被调用Actionname namespace,如果指定了executeResult参数的属性值为true,该标签还会把Action的处理结果(视图资源)包含到本页面中来。 action标签有如下几个属性:
  id
:可选属性,该属性将会作为该Action的引用ID
  name
:必填属性,通过该属性指定该标签调用哪个Action
  namespace
:可选属性,指定该标签调用的Action所在的namespace
  executeResult
:可选属性,指定是否需要将action的处理结果页面包含到本页面中,默认值为false不包含
  ignoreContextParams
:可选属性,指定该页面中的请求参数是否需要传入

例子如下:
      Action
代码如下:
      package lee;
      import com.opensymphony.xwork2.ActionSupport;
      import org.apache.struts2.ServletActionContext;
      public class TagAction extends ActionSupport
      {
      private String author;
      public void setAuthor(String author)
      {
      this.author = author;
      }
      public String getAuthor()
      {
      return author;
      }
      public String execute() throws Exception
      {
      return "done";
      }
      public String login() throws Exception
      {
      ServletActionContext.getRequest().setAttribute("au thor", getAuthor());
      return "done";
      }
      }
      struts.xml
配置文件中的配置如下:
      <?xml version="1.0" encoding="GBK"?>
      <!DOCTYPEstrutsPUBLIC
      "-//Apache Software Foundation//DTDStrutsConfiguration 2.0//EN"
      "http://struts./dtds/struts-2.0.dtd">
      <struts>
      <constant name="struts.custom.i18n.resources" value="messageResource"/>
      <constant name="struts.i18n.encoding" value="GBK"/>
      <constant name="struts.date.format" value="yyyy
MMdd"/>
      <package name="lee" extends="struts-default">
      <actionname="tag1" class="lee.TagAction">
      <result name="done">succ.jsp</result>
      </action>
      <actionname="tag2" class="lee.TagAction" method="login">
      <result name="done">loginSucc.jsp</result>
      </action>
       <actionname="*"> <result>/{1}.jsp</result> </action> </package> </struts>
上面两个Action的返回视图资源如下:
      succ.jsp
      <%@ page contentType="text/html; charset=GBK" language="java"%>
      <%@taglib prefix="s" uri="/struts-tags"%>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
      <title>s:action
的成功页面</title>
      </head>
      <body>
      <br>
     
执行成功!!
      </body>
      </html>
      loginSucc.jsp
      <%@ page contentType="text/html; charset=GBK" language="java"%>
      <%@taglib prefix="s" uri="/struts-tags"%>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
      <title>s:action
的成功页面</title>
      </head>
      <body>
      <br>
      <s:property value="#attr.author"/>,
登陆成功!!
      </body>
      </html>
     
调用Action的页面代码如下:
      <%@ page contentType="text/html; charset=GBK" language="java"%>
      <%@taglib prefix="s" uri="/struts-tags"%>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
      <title>
使用s:action标签在页面中调用Action</title>
      </head>
      <body>
     
下面调用第一个Action,并将结果包含到本页面中。<br>
      <s:actionname="tag1" executeResult="true"/>
      <hr/>
     
下面调用第二个Action,并将结果包含到本页面中。<br>
     
并且阻止本页面请求参数传入Action<br>
      <s:actionname="tag2" executeResult="true" ignoreContextParams="true"/>
      <hr/>
     
下面调用第二个Action,且并不将结果包含到本页面中。<br>
      <s:actionname="tag2" executeResult="false"/>
      <s:property value="#attr.author"/>
      </body>
      </html>
      bean
标签
      bean
标签用于创建一个JavaBean的实例,创建实例时,可以在该标签体内使用param标签为该JavaBean实例传入属性。可以使用如下两个属性:
   name
:必填属性,指定要实例化的JavaBean的实现类
   id
:可选属性,如果指定了该属性则JavaBean的实例会放入pageContext中,否则只在此标签内有效
     
例子如下:
      JavaBean
代码如下:
      package lee;
      public class Person
      {
      private String name;
      private int age;
      public void setName(String name)
      {
      this.name = name;
      }
      public String getName()
      {
      return (this.name);
      }
      public void setAge(int age)
      {
      this.age = age;
      }
      public int getAge()
      {
      return (this.age);
      }
      }
     
页面代码如下:这个页面中注入的Bean只在bean标签内有效
      <%@ page contentType="text/html; charset=GBK" language="java"%>
      <%@taglib prefix="s" uri="/struts-tags"%>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
      <title>
使用s:bean标签创建JavaBean的实例</title>
      </head>
      <body>
      <s:bean name="lee.Person">
      <s:param name="name" value="'yeeku'"/>
      <s:param name="age" value="29"/>
      <s:property value="name"/><br>
      <s:property value="age"/>
      </s:bean>
      </body>
 
    </html>
 
   
下面的页面代码中,注入的Bean可以在此页面中有效:
      <%@ page contentType="text/html; charset=GBK" language="java"%>
      <%@taglib prefix="s" uri="/struts-tags"%>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
      <title>
使用s:bean标签创建JavaBean的实例</title>
      </head>
      <body>
       <s:bean name="lee.Person" id="p"> <s:param name="name" value="'yeeku'"/> <s:param name="age" value="29"/> </s:bean> <s:property value="#p.name"/><br> <s:property value="#p.age"/> </body> </html>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多