Struts 2 Data Tags

下面我们将看到怎样使用property标签,set标签和push标签,他们是Struts2数据标签的一部分,在我们学习每个标签的语法之前我们需要知道什么是ActionContextAction上下文)和ValueStack(值栈)。


·ActionContext是一个持有请求处理相关的数据的全局存储区。

·ActionContext是局部线程这使得Struts2 Actions是线程安全的。

·ValueStackActionContext的一部分。

Property标签:

Property标签用于从值栈取得数据或是取得一些ActionContext的对象,想application或是session。下面看一个Property的例子。

Set标签:

Set标签是用来给一个属性值指定另一个名称,这有助于我们更快和更容易访问一个属性。然后通过#objectName取得属性的值。

Push标签

使用push标签,可以把一个值压入值栈,使用push标签进入值栈的值将会被存放在栈顶。我们可以通过first-level OGNL表达式很容易的引用。

定义一个AlbumInfoAction 类:

package com.zcl.struts.datatags;

import com.opensymphony.xwork2.ActionSupport;

public class AlbumInfoAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String title ;
	private Artist artist ;
	
	public String populate(){
		title = "Thriller" ;
		artist = new Artist("Michael Jackson","King of pop") ;
		return "populate" ;
	}
	
	public String execute(){
		return SUCCESS ;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Artist getArtist() {
		return artist;
	}

	public void setArtist(Artist artist) {
		this.artist = artist;
	}
}


需要Artister类:

package com.zcl.struts.datatags;

public class Artist {
	private String name ;
	private String bio ;
	
	public Artist(String name, String bio) {
		this.name = name;
		this.bio = bio;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBio() {
		return bio;
	}

	public void setBio(String bio) {
		this.bio = bio;
	}
}


下面通过Property标签取得Action类的属性,建立albumDetails.jsp文件。

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>hello world</title>
  </head>
  <body>
    <b>Album Title :</b>
    <s:property value="title"/>
    <br/>
    <b>Artist Name :</b>
    <s:property value="artist.name"/>
    <br/>
    <b>Artist Bio :</b>
    <s:property value="artist.bio"/><br/>
    <br/>
    <b>Set Tag Example</b>
    <hr />
    <s:set name="artistName" value="artist.name" />
    <s:set name="artistBio" value="artist.bio" />
    <b>Album Title :</b> <s:property value="title"/><br/>
    <b>Artist Name :</b> <s:property value="#artistName"/><br/>
    <b>Artist Bio :</b> <s:property value="#artistBio"/><br/>
   	<br/>
   	<b>Push Tag Example</b>
   	<hr/>
   	<b>Album Title :</b> <s:property value="title" /> <br>
	<s:push value="artist">
	<b>Artist Name :</b> <s:property value="name" /> <br>
	<b>Artist Bio :</b> <s:property value="bio" /> <br>
	</s:push>
  </body>
</html>
配置struts.xml文件加入如下代码:

<package name="datatag" extends="struts-default">
		<action name="*AlbumAction" class="com.zcl.struts.datatags.AlbumInfoAction" method="{1}">
			<result name="populate">/albumDetails.jsp</result>
		</action>
	</package>

部署到tomcat服务器在输入:http://localhost:8888/struts/populateAlbumAction访问,这根据你自己的配置,访问路径也不一样。如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值