struts1.x☞Empty,present标签

struts1.x有很多方便的标签,提供我们使用,简单的介绍2个:empty,present;

首先搭建好struts1.x环境:

在jsp页面导入:

<%@ taglib prefix="logic" uri="http://jakarta.apache.org/struts/tags-logic" %>

 这个信息在struts.jar包下的META-INF下的tlds下struts-logic.tld中找到;prefix指的是:shortname;uri指的是:uri;

写一个测试的java类:

package com.keith;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class EmptyPresentTest extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("att2", new ArrayList());
		request.setAttribute("att3", "hello,empty_taglib,present_taglib");
		request.setAttribute("att4", "");
		return mapping.findForward("emptyPresentTest");
	}

}

 struts-config.xml中这样配置:

<action-mappings>                
   <action path="/emptyPresentTest"
	        type="com.keith.EmptyPresentTest">
		<forward name="emptyPresentTest" path="/emptyPresent.jsp" />
    </action>
</action-mappings>

 在jsp中:

<body>
	<h2>Empty,Present测试</h2>
	<hr color="orange" size="4">
	<logic:empty name="att1">
		<font color="green">att1为空</font>
	</logic:empty>
	<logic:notEmpty name="att1">
		<font color="red">art1不为空</font>
	</logic:notEmpty>
	<logic:present name="att1">
		<font color="green">att1存在</font>
	</logic:present>
	<logic:notPresent name="att1">
		<font color="red">att1不存在</font>
	</logic:notPresent>
	<hr>
	
	<logic:empty name="att2">
		<font color="green">att2为空</font>
	</logic:empty>
	<logic:notEmpty name="att2">
		<font color="red">att2不为空</font>
	</logic:notEmpty>
	<logic:present name="att2">
		<font color="green">att2存在</font>
	</logic:present>
	<logic:notPresent name="att2">
		<font color="red">att2不存在</font>
	</logic:notPresent>
	<hr>
	
	<logic:empty name="att3">
		<font color="green">att3为空</font>
	</logic:empty>
	<logic:notEmpty name="att3">
		<font color="red">att3不为空</font>
	</logic:notEmpty>
	<logic:present name="att3">
		<font color="green">att3存在</font>
	</logic:present>
	<logic:notPresent name="att3">
		<font color="red">att2不存在</font>
	</logic:notPresent>
	<hr>
	
	<logic:empty name="att4">
		<font color="green">att4为空</font>
	</logic:empty>
	<logic:notEmpty name="att4">
		<font color="red">att4不为空</font>
	</logic:notEmpty>
	<logic:present name="att4">
		<font color="green">att4存在</font>
	</logic:present>
	<logic:notPresent name="att4">
		<font color="red">att4不存在</font>
	</logic:notPresent>
	<hr>
</body>
 

运行下就知道empty,present是什么作用了?

你可能感兴趣的:(apache,jsp,xml,servlet,struts)