struts的五类标签库

本文详细介绍了Struts框架中的各类标签库,包括Bean标签、HTML标签、Logic标签等的功能和使用方法。Bean标签用于创建及操作bean,HTML标签用于创建表单元素,而Logic标签则用于逻辑判断和流程控制。


一、Struts标签库概述

Struts的五类标签库
	Bean Tags :用来创建bean、访问bean
	HTML Tags :用来创建 html 页面的动态元素,对html进行了封装;
	Logic Tags:逻辑判断、集合迭代和流程控制。
	Nested Tags:该标签库建立在前三个标签库的基础上,具有前三个标签库的所有功能,只是允许标签间的嵌套。
	Tiles Tags :该标签库包含的标签可以用来创建tiles样式的页面。

重点学习前3类标签库


二、Bean标签
	可以看成 <jsp:useBean> 的增强版,
	它可以定义bean :获取某些数据(cookie,请求参数,请求头),将之定义成一个脚本变量,并同时置于某个作用域(缺省pagaContext)中;

bean标签的公共属性:
	id - 定义一个变量
	name - 引用一个存在的bean或对象的名字
	property - 被引用的bean的属性
	scope - 放置或搜索bean的范围,若没有制定,则依次 page--request---session--application

1.<bean:define/>
	作用: 把一个bean 或其属性, 定义成一个变量

	* 通过<bean:define/>定义的变量可以通过JSP脚本、EL以及Struts本身的<bean:write/>标记访问。
	Example1:
	1)定义一个Java bean:student,并且对其属性进行赋值
	2)通过<bean:define/>定义stuName、stuAge、stuGender三个变量,将student这个对象的属性值赋值给这些变量
		<bean:define id="stuName" name="student" property="name"></bean:define>
		<bean:define id="stuAge" name="student" property="age"></bean:define>
		<bean:define id="stuGender" name="student" property="gender"></bean:define>
	3)输出
		<%=stuName%>
		${stuAge }
		<bean:write name="stuGender"/>


	* 如果JavaBean的属性是List等类型,可以指定type属性
	Example2:
	1)在Java Bean中添加List属性,并提供get/set方法。
		private List songs;
		public List getSongs() {
			List list = new ArrayList();
			list.add("我爱北京天安门");
			list.add("我和你");
			list.add("我不做大哥好多年");
			return list;
		}
	2)<bean:define id="songs" name="student" property="songs"></bean:define>
	3)输出
		${mySongs[0] }
	
	* 定义新变量,例如:
		<bean:define id="bookName" value="Effective Java"></bean:define>


2.<bean:write/>
	作用: 输出 bean 或bean属性;
	等价于:
	${} 或者 <%= %>



3.<bean:message/>
	作用:读取属性静态文本内容,支持国际化(i18n)

	Example:
	1)确认在类路径上含有
	com/ApplicationResources.properties
	2)在文件中加入key,value对
	page.title=/u9875/u9762/u6807/u9898
	3)提供message.jsp
		使用<bean:message key="page.title"/>


4.<bean:size/>
	作用:获得一个集合或者数组的大小

	Example:
	1)定义Java Bean : student
	2)读取student这个java bean的songs的size
		<jsp:useBean id="student" class="com.bean.Student"></jsp:useBean>
  		<bean:size id="songsize" name="student" property="songs"/>
	3)输出songsize值

	Exapmple:定义一个列表,输出其size,要求使用<bean:size/>获取该值
	1)<%定义一个List,并初始化%>
	2)使用
		<bean:size id="listsize1" collection="<%=list %>"/>
	  获取值
		注意:如果使用${},必须要把list放置到范围对象中
	3)输出

5.<bean:cookie/>
	作用:读取请求头中cookie的信息
	* <bean:cookie id="cid" name="customid">
		获得指定的名为 "customid" 的coockie,并将其赋值给脚本变量 cid
	* 若找不到id为costCookie这个cookie,所以系统创建一个cookie,并将它的值设置为$100
	  <bean:cookie id="cost" name="costCookie" value="$100"/>
	 输出:
		<bean:write name="cost" property="value"/>

6.<bean:header/>
	作用:获取请求头的属性信息
	<bean:header id="userAgent" name="User-Agent"/>
	<bean:header id="host" name="host"/>
	<bean:header id="dummy" name="UNKNOWN-HEAD" value="no defined Header"/>


7.<bean:include/>
	作用:对指定url(由forward、href或page确定)处的资源做一个请求,
	将响应数据作为一个String类型的bean绑定到page作用域,
	同时创建一个scripting变量。我们可以通过id值访问它们。

	Example:
	1)在根目录下定义一个文件:include.txt
	2)获取文件的内容数据,并赋于words
	<bean:include id="words" page="/include.txt"/>
	3)输出内容

8.<bean:resource/>
	作用:获取指定的资源,以String或者InputStream的方式来读取,其中input属性是决定了对应的方式。
	默认(false)是以字符串的方式来读取。
	例如:
	<bean:resource id="r1" name="/include.txt" />
	<bean:resource id="r2" name="/include.txt" input="true" />

9.<bean:parameter/>
	作用:取出url中queryString中指定参数名称的值
	例子:
		<bean:parameter id="target" name="action" />
		只会读取第一个名字为"action"的参数

		<bean:parameter id="ps" name="hobby" multiple="true" />
		把url中queryString中名字叫hobby的所有值赋值给变量ps,所以ps应该是一个数组

10.<bean:page/>
	作用:把pageContext中的特定的隐含对象(application, request, response, config, session)取出来,
	绑定到某个id中,本页的其他地方就可以使用id来操纵这些隐含对象了。

	Example:
	<bean:page id="res" property="response" />
	<bean:page id="sess" property="session" />

	<bean:write name="res" property="contentType"/>
	<bean:write name="res" property="characterEncoding"/>
	<bean:write name="sess" property="id"/>
	<bean:write name="sess" property="maxInactiveInterval"/>
	

三、HTML标签库
使用taglib指令引入标签库
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

通常是配合bean标签一起使用,是struts中最常用的标签之一。

1.<html:form/>
	作用:对html的form表单进行简单的封装,满足struts中表单请求的处理
	<html:form action="/login">
		
	</html:form>
	如果你有上述一个标签,那么你的Struts配置文件的元素中必须有一个如下内容:
	<action-mappings>
		<action input="/login.jsp" name="loginForm" path="/login"
			type="action.LoginAction" validate="true">
			<forward name="success" path="/success.jsp" />
			<forward name="error" path="/error.jsp" />
		</action>
	</action-mappings>
	这就是说一个form标签是和form bean相关联的。
	任何包含在<form>中用来接收用户输入的标签
	(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)
	必须在相关的form bean中有一个指定的属性值。<form>标签中method属性的缺省值是POST 

2.<html:link>
	作用:超文本连接
	属性:page,指定一个页面的路径,必须以/开始。

	Example:
	当前页面跳转到/bean/parameter.jsp
	需要提供参数:
	<bean:define id="beanName" value="beanValue"></bean:define>
  	<html:link page="/bean/parameter.jsp"
  		paramId="action" paramName="beanName">
  		<html:param name="hobby" value="sports"></html:param>
  		跳转到页面bean:parameter.jsp
  	</html:link>

	这等价于:
	http://localhost:8080/strutsTaglib/bean/parameter.jsp?hobby=sports&action=beanValue



3.<html:errors/>
	作用:输出错误信息
	异常处理会介绍

	Example:
	1)在资源文件中定义
		property1.error1=Property1 Error1
		property2.error1=Property2 Error1
		property2.error2=Property2 Error2
		property2.error3=Property2 Error3
		property3.error1=Property3 Error1
		property3.error2=Property3 Error2

		globalError=Global Error

		property1.message1=Property1 Message1
		property2.message1=Property2 Message1
		property2.message2=Property2 Message2
		property2.message3=Property2 Message3
		property3.message1=Property3 Message1
		property3.message2=Property3 Message2

		globalMessage=Global Message

		messages.header=<table border=1><tr><td>错误变量</td><td>错误信息</td></tr>
		messages.footer=</table>
	2)添加jsp文件并编辑
		<%@ page import="org.apache.struts.action.*"%>
		<%@page import="org.apache.struts.Globals"%>
		<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
		<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

		<%
			ActionMessages errors = new ActionMessages();
			errors.add("property1", new ActionMessage("property1.error1"));
			errors.add("property2", new ActionMessage("property2.error1"));
			errors.add("property2", new ActionMessage("property2.error2"));
			errors.add("property2", new ActionMessage("property2.error3"));
			errors.add("property3", new ActionMessage("property3.error1"));
			errors.add("property3", new ActionMessage("property3.error2"));
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("globalError"));
			request.setAttribute(Globals.ERROR_KEY, errors);

			ActionMessages messages = new ActionMessages();
			messages.add("property1", new ActionMessage("property1.message1"));
			messages.add("property2", new ActionMessage("property2.message1"));
			messages.add("property2", new ActionMessage("property2.message2"));
			messages.add("property2", new ActionMessage("property2.message3"));
			messages.add("property3", new ActionMessage("property3.message1"));
			messages.add("property3", new ActionMessage("property3.message2"));
			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("globalMessage"));
			request.setAttribute(Globals.MESSAGE_KEY, messages);
		%>

	3)输出相应的(出错)信息:
		<table border=1>
			<tr>
				<td>错误变量</td>
				<td>错误信息</td>
			</tr>
			<tr>
				<td>property1</td>
				<td><html:errors property="property1" /></td>
			</tr>
			<tr>
				<td>property2</td>
				<td><html:errors property="property2" /></td>
			</tr>
			<tr>
				<td>property3</td>
				<td><html:errors property="property3" /></td>
			</tr>
			<tr>
				<td>org.apache.struts.action.GLOBAL_MESSAGE</td>
				<td><html:errors
					property="org.apache.struts.action.GLOBAL_MESSAGE" /></td>
			</tr>
			<tr>
				<td>All</td>
				<td><html:errors /></td>
			</tr>
		</table>

		<html:messages message="true" id="msg"
			header="messages.header" footer="messages.footer">
			<tr>
				<td></td>
				<td><bean:write name="msg" /></td>

			</tr>
		</html:messages>
4.<html:image>与<html:img>
	作用:在页面中产生图像的输出
	最重要的属性page:图象文件的路径,前面必须带有一个斜线。
	其它属性:heignt、width、alt。

	Example:
	<html:image page="/f4icmu.jpg" alt="软件工程之通俗版"></html:image>

  	<html:img page="/f4icmu.jpg"/>


5.<html:checkbox/>
	生成一个checkbox。这里的value值可以是true,yes或on。
	checkboxForm的属性:
	private boolean one = false; 
	private boolean two = false; 
	private boolean three = false;

	<html:checkbox name="checkboxForm" property="one">One</html:checkbox> 
	<html:checkbox name="checkboxForm" property="two">Two</html:checkbox> 
	<html:checkbox name="checkboxForm" property="three">Three</html:checkbox> 
	如果选中后被提交则相应的属性的值为true。

6.<html:radio/>
	生成一个radio。主要的用法有两种。
	下面的代码示例了html:radio标签的一般用法,
	如果被提交则选中的radio的value值将被提交到radioForm中的id中。
	<html:radio name="radioForm" property="id" value="00001"> One </html:radio>
	<html:radio name="radioForm" property="id" value="00002"> Two </html:radio> 

7.<html:select>标签和<html:option>标签 
	作用:对html中的下拉选择标签与选项标签做了封装

	单选下拉
	Example1:
	 <html:select property="singleSelect" size="3">
		 <html:option value="Single 0">Single 0</html:option>
		 <html:option value="Single 1">Single 1</html:option>
		 <html:option value="Single 2">Single 2</html:option>
	 </html:select>

	 Example2:性别
	 1)提供label和value的数组
		 <%
			String[] label = { "男", "女", "未知" };
			String[] value = { "male", "female", "unknown" };

			pageContext.setAttribute("label", label);
			pageContext.setAttribute("value", value);
		%>
	2)标签使用:
			<html:select property="sex" size="1">
				<html:options labelName="label" name="value" />
			</html:select>
	
	Example3:兴趣爱好多选
	1)提供列表
		<%
			List options = new ArrayList();
			options.add(new LabelValueBean("电脑游戏", "PCGame"));
			options.add(new LabelValueBean("看电视", "TV"));
			options.add(new LabelValueBean("阅读", "Reading"));
			options.add(new LabelValueBean("唱歌", "Singing"));
			pageContext.setAttribute("options", options);
		%>

	其中,
		public class LabelValueBean {
			private String label;
			private String value;
		}

	2)提供多选标签
			<html:select property="favors" multiple="true">
				<html:options collection="options" property="value"
					labelProperty="label" />
			</html:select>
	
	Example4:联系方式多选
	1)提供多选列表
		<%
			List myPhones=new ArrayList();
			myPhones.add(new LabelValueBean("小灵通","33213322"));
			myPhones.add(new LabelValueBean("固话","80512010"));
			myPhones.add(new LabelValueBean("手机","13711221113"));
			pageContext.setAttribute("myPhones",myPhones);
		 %>
	2)标签的使用
		<html:select property="phones" multiple="true">
			<html:optionsCollection name="myPhones"/>
		</html:select>
	
8.<html:submit>标签
	<html:submit value="Submit" />


9.<hmtl:text>标签 
	文本输入框
	<html:text property="username" />

10.<html:password>标签
	<html:password property="password"/>

11.<html:cancel/>取消标签


四、Logic标记 : 
	该标签库包含的标签可以用来进行逻辑判断、集合迭代和流程控制。 
1.<logic:iterate/>
	作用:<logic:iterate/>标签用来迭代集合
	Example:
	<%
	List stuList=new ArrayList();
	     Student stu=new Student();
	     stu.setName("Alice");
	     ...
	     stuList.add(stu);
	     request.setAttribute("stuList",stuList);
	%>
	<logic:iterate id="stu" name="stuList">
	     <tr>
		<td>${stu.id }</td>
		<td>${stu.name }</td>
		<td>${stu.sex }</td>
		<td>${stu.age }</td>
		<td>${stu.desc }</td>
	     </tr>
	</logic:iterate>

(2)logic:empty  
	用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。该标签用于以下情况:
	当Java对象为null时 
	当String对象为""时 
	当java.util.Collection对象中的isEmpty()返回true时 
	当java.util.Map对象中的isEmpty()返回true时 
	下面的代码示例了logic:empty标签判断集合persons是否为空:

	<logic:empty name="stu" property = "books"> 

	<div>集合books为空!</div> </logic:empty>
	 logic:notEmpty标签的应用正好和logic:empty标签相反。



(3)logic:equal    
	这里要介绍的不只是logic:equal(=)标签,而是要介绍一类标签,这类标签完成比较运算,
	包括:
	logic:equal(=) 
	logic:notEqual(!=) 
	logic:greaterEqual(>=) 
	logic:lessEqual(<=) 
	logic:graterThan(>) 
	logic:lessThan(<) 

	Example:
		<logic:equal name="stu" property="name"	value="Alice">
		I am Alice.
		</logic:equal>

		<bean:define id="count" value="168"/>
		<logic:equal name="count" value="168">
		Good lucky number.
		</logic:equal>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值