数据标签主要用于提供各种和数据访问相关的功能,如输出信息和显示调试信息等。常用的数据标签有 <s:property>、<s:a>、<s:debug>、<s:include> 和 <s:param> 等。
<s:property> 标签的作用是输出指定的值,通常输出的是 value 属性指定的值,<s:property> 标签的属性及属性说明如下。
为了使读者更好地掌握 <s:property> 标签的使用,下面编写案例进行演示。在项目的 WebContent 目录下创建一个名称为 propertyTags.jsp 的文件,编辑后如下所示。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>property标签</title>
</head>
<body>
输出字符串:
<s:property value="'this is a string'"/><br/>
输出默认值:
<s:property value="" default="default_value"/><br/>
忽略HTML代码:
<s:property value="'<h2>www.w3school.com.cn</h2>'" escape="true"/><br/>
不忽略HTML代码:
<s:property value="'<h2>www.w3school.com.cn</h2>'" escape="false"/><br/>
</body>
</html>
上述代码中,分别对 <s:property> 标签的 value、default 和 escape 属性的使用进行了演示。启动项目后,在浏览器的地址栏中输入地址 http://localhost:8080/struts2Demo04/propertyTags.jsp 访问 propertyTags.jsp,浏览器的显示结果如图 1 所示。