---
title: "Struts的Empty和Present等标签"
author: "wode66"
date: "2009-06-12T23:12:58Z"
tags: []
categories: ["编程语言"]
slug: "struts-empty-present"
draft: false
lastmod: "2011-09-29T16:12:50Z"
id: 407580
url: "https://suo.iteye.com/blog/407580"
---

**对Struts中的Empty和Present等标签做下测试（具体过程可以参见尚学堂的视频）**

**前提：好像必须要导入struts的国际化标签，有待验证。**

**第一步：添加EmptyPresentTestAction类**

 

 

```
package com.bjsxt.struts;
/**
 * 测试empty,notEmpty,present,notPresent
 */
public class EmptyPresentTestAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("attr1", null);
		request.setAttribute("attr2", "");
		request.setAttribute("attr3", new ArrayList());
		return mapping.findForward("success");
	}
}
```

 

**第二步**：添加测试的jsp页面（emptypresent.jsp）

```
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>    
<!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=GB18030">
<title>测试empty,notEmpty,present,notPresent</title>
</head>
<body>
	<h1>测试empty,notEmpty,present,notPresent</h1>
	<hr>
	<logic:empty name="attr1">
		attr1为空<br>
	</logic:empty>
	<logic:notEmpty name="attr1">
		attr1不为空<br>
	</logic:notEmpty>
	<logic:present name="attr1">
		attr1存在<br>
	</logic:present>
	<logic:notPresent name="attr1">
		attr1不存在<br>
	</logic:notPresent>
	
	<p>
	<logic:empty name="attr2">
		attr2为空<br>
	</logic:empty>
	<logic:notEmpty name="attr2">
		attr2不为空<br>
	</logic:notEmpty>
	<logic:present name="attr2">
		attr2存在<br>
	</logic:present>
	<logic:notPresent name="attr2">
		attr2不存在<br>
	</logic:notPresent>
	
	<p>
	<logic:empty name="attr3">
		attr3为空<br>
	</logic:empty>
	<logic:notEmpty name="attr3">
		attr3不为空<br>
	</logic:notEmpty>
	<logic:present name="attr3">
		attr3存在<br>
	</logic:present>
	<logic:notPresent name="attr3">
		attr3不存在<br>
	</logic:notPresent>
</body>
</html>
```

 

 

**第三步**：显示出来的结果如下：

![](/upload/attachment/114132/b1c33939-3058-3e64-9539-b8b17acffd3c.jpg)