当前位置: 首页 > 图文教程 > Java技术 > Web框架 > WEB框架:小编祥谈Struts2的数据标签中Action与Bean标签的使用

Web框架
struts2(三)
struts2(四)
struts2(五)
struts2(六)
Spring中注入概念的简单介绍
深入探讨struts的ActionMessages消息机制(转)
DWR 的 Converter 实现原理简单分析及应用
详解Spring中bean的作用域
AJAX实现级联下拉框
Spring AOP面向方面编程原理:AOP概念
Spring结合Hibernate声明式事务配置
Struts 概述
Ajax 入门简介
Struts配置文件详解
Struts in Spring
Struts 开发的最佳实践
Struts核心标签
开发框架:利用STRUTS实现国际化支持
Struts自定义标签的过程
Struts 框架的基本概念及实现MVC模式的原理

Web框架 中的 WEB框架:小编祥谈Struts2的数据标签中Action与Bean标签的使用


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 139 ::
收藏到网摘: n/a

数据标签主要用于提供各种数据访问相关的功能,包含显示一个Action里的属性,以及生成国际化输出等功能。

action标签

使用Action标签可以允许在JSP页面中直接调用Action,因为需要调用Action,故可以指定需要被调用Actionnamenamespace,如果指定了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("author", getAuthor());

   return "done";

}

}

 

struts.xml配置文件中的配置如下:

 

<?xml version="1.0" encoding="GBK"?>

<!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.custom.i18n.resources" value="messageResource"/>

<constant name="struts.i18n.encoding" value="GBK"/>

<constant name="struts.date.format" value="yyyyMMdd"/>

 

    <package name="lee" extends="struts-default">

 

   <action name="tag1" class="lee.TagAction">

    <result name="done">succ.jsp</result>

   </action>

 

   <action name="tag2" class="lee.TagAction" method="login">

    <result name="done">loginSucc.jsp</result>

   </action>

 

   <action name="*">

    <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:action name="tag1" executeResult="true"/>

<hr/>

下面调用第二个Action,并将结果包含到本页面中。<br>

并且阻止本页面请求参数传入Action<br>

<s:action name="tag2" executeResult="true" ignoreContextParams="true"/>

<hr/>

下面调用第二个Action,且并不将结果包含到本页面中。<br>

<s:action name="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>