JSP Struts之HTML标签库详解
jsp struts之html标签库详解
struts提供了五个标签库,即:html、bean、logic、template和nested。
标签库 说明
html 标签 用来创建能够和struts 框架和其他相应的html 标签交互的html 输入表单
bean 标签 在访问javabeans 及其属性,以及定义一个新的bean 时使用
logic 标签 管理条件产生的输出和对象集产生的循环
template 标签 随着tiles框架包的出现,此标记已开始减少使用
nested 标签 增强对其他的struts 标签的嵌套使用的能力
标签的公共特征
使用固定属性名称的struts 标签:
属性 说明
id 命名自定义标签创建时的脚本变量名。
name 指出关键字值,在该关键字下可以找到一个存在的bean 。如果给出了scope属性,则仅仅在scope中查找。否则,根据标准的顺序在各种scope中查找:(page, request, session, or application)。
property 指出bean 中的某个属性,可以在其中检索值。如果没有标明,则使用对象本身的值。
scope 定义了bean在哪个范围(page, request, session, or application)中被查找。如果没有标明按顺序查找。脚本变量(见id)将在相同的范围中创建。
struts 标签也支持嵌套引用,例如:
property="foo.bar.baz"
这相当于进行下面的调用:
getfoo().getbar().getbaz();
或者做为setter:
getfoo().getbar().setbaz(value);
虽然struts 标签的设计原意是为了避免使用scriptlet,scriptlet的表达式还能够提供给所有的struts 标签使用。但请确保使用完整的表达式:
错误:
<html:link href="'<%= "/" + name %>/index.jsp>'>
正确:
<html:link href="'<%= "/" + name + "/index.jsp" %>'> // 表达式必须提供整个属性值
html 标签库
1. <html>标签
它有两个属性:locale和xhtml,两者都不是必需的。
<html:html locale="true">
此行代码解析后:
<html lang="en">
2. 说明:生成的结果取决于struts应用程序所位于的服务器的locale。如果你将应用程序部署到一个不同locale的服务器,你不需要改变代码,locale会自动调整。
3. <base>标签:表示所包含页面的绝对位置。这个标签只有内嵌在head标签中才有效。
<html:base/>[www.iocblog.net 来源]
此行代码解析后:
<base href="http://www.mymain.com/mystrutsapp/testing.jsp">
4. <img>标签
最重要的属性page:图象文件的路径,前面必须带有一个斜线。
其它属性:heignt、width、alt。
<html:img page="/logo.gif" height="50" width="200" alt="web logo"/>
5. <link>标签
<html:link page="/index.html">click demo</html:link>
此行代码解析后:
<a href="/index.html">click demo</a>
6. <errors>标签:通过一个简单的<html:errors/>标签,你就可以在一个jsp页面上显示完全自定义的错误信息。功能超强大!!
说明:这个标签在request对象的属性集合中查找reserved key。如果它找到一个reserved key,它就假设这个key是一个string、或是一个string数组
(它包含在模块的messageresources中查找的message keys)、或是类型为org.apache.struts.action.actionerrors的一个对象。
如果在应用程序资源中存在相应的信息,那么就可以用下面这些可选的message keys:
・ errors.header or errors.prefix:相应的信息在错误信息的单独列表前显示。
・ errors.footer or errors.suffix:相应的信息在错误信息的单独列表后显示。
7. <form>标签系列
使用<form>标签时必须遵循一些规则:
1. 标签中必须包含一个action属性,它是这个标签中唯一必需的属性。如果不具备该属性则jsp页面会抛出一个异常。之后你必须给这个action属性指定一个有效值。一个有效值是指应用程序的struts配置文件中元素里的任何一个子元素的访问路径。而且相应的元素中必须有一个name属性,它的值是form bean的名称。
<html:form action="/login" >
如果你有上述一个标签 ,那么你的struts配置文件的元素中必须有一个如下显示为粗体的元素:
<action-mappings>
<action path="/login"
type="com.javapro.struts.loginaction"
name="loginform"
scope="request"
input="/login.jsp">
<forward name="success" path="/mainmenu.jsp"/>
</action>
.
.
.
</action-mappings> // 这就是说一个form标签是和form bean相关联的。
2.
3. 任何包含在<form>中用来接收用户输入的标签(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)必须在相关的form bean中有一个指定的属性值。比如,如果你有一个属性值被指定为“username”的<text>标签,那么相关的form bean中也必须有一个名为“username”的属性。输入<text>标签中的值会被用于生成form bean的username属性。
<form>标签还有一些不是必须但很有用的“次要”属性。
比如,你可以用focus属性来生成javascript,它会“定焦”(focus)到该form所包含的一个元素上。使用focus属性时你需要给它指定元素的名称。
<body>
<html:form action="/login" focus="password">
user name: <html:text property="username"/>
<br>password: <html:text property="password"/>
<br><html:submit/>[www.iocblog.net 来源]
</html:form>
</body>
代码解析后:
<body>
<form name="loginform" method="post" action="/mystrutsapp/login.do">
user name: <input type="text" name="username" value="">
<br>password: <input type="text" name="password" value="">
<br><input type="submit" value="submit">
</form>
<script language="javascript" type="text/javascript">
<!--
if (document.forms["loginform"].elements["password"].type != "hidden")
document.forms["loginform"].elements["password"].focus()
// -->
</script>
</body>
有没有看到这个标签库是如何建立javascript来定焦到password元素上的? 这也是该库让人着迷的地方之一。你不用担心如何在客户端进行编程,它会帮你自动生成。
还可以看到,<form>标签中method属性的缺省值是post。
<text>标签、<hidden>标签、<textarea>标签、<radio>标签、<checkbox>标签、<submit>标签、<reset>标签:
都有一个property属性,最后会被转换成html中的name属性,当然还有name和value属性。
<password>标签
<html:password property="password" redisplay="false"/>
该标签中的一个很重要的属性是"redisplay",它用于重新显示以前输入到这个区域中的值。该属性的缺省值为true。然而,为了使password不能被重新显示,你或许希望将该属性的值设为false。
<select>标签和<option>标签:
<html:select property="color" size="3">
<html:option value="r">red</html:option>
<html:option value= "g">green</html:option>
<html:option value= "b">blue</html:option>
</html:select>
遗补:1.)<html:link>标签
forward属性:链接到一个global forward上;action属性:链接到一个action mapping上;
href属性:这个链接会转发给控制器,由控制器做决定;page属性:一个相对的链接。
用page属性链接到action上:
<html:link page="/html-link.do">
linking with the page attribute.
</html:link>
注意,上面的代码中你不必指定web的关联。相反的,如果你使用href属性,你就必须像下面所示指出web的关联(这里的关联就是struts-exercise):
<html:link href="/struts-exercise-taglib/html-link.do">
using href
</html:link>
很明显,当你在相同的web应用程序中做链接是,它比page属性更加好。你也能用href在不同的服务器上创建链接:
<html:link href="http://otherserver/strutstut/html-link.do">
using href
</html:link>
另一种链接到html-link.do的方法是用action属性:
<html:link action="/html-link">
using action attribute
</html:link>
你也可以以硬编码的方式使用参数:
<html:link page="/htmllink.do?doubleprop=3.3&longprop=32">
double and long via hard coded changes
</html:link>
或者使用paramid, paramname, and paramproperty属性:
<html:link page="/html-link.do" paramid="booleanproperty" paramname="testbean"
paramproperty="nested.booleanproperty">
boolean via paramid, paramname, and paramvalue
</html:link>
解析后的代码:
<a href="/struts-exercise-taglib/html-link.do?booleanproperty=false">
boolean via paramid, paramname, and paramvalue
</a>
另外,还能使用带name属性的map来实现传递多个参数:
<%
java.util.hashmap newvalues = new java.util.hashmap();
newvalues.put("floatproperty", new float(444.0));
newvalues.put("intproperty", new integer(555));
newvalues.put("stringarray", new string[]
{ "value 1", "value 2", "value 3" });
pagecontext.setattribute("newvalues", newvalues);
%>
...
<html:link action="/html-link" name="newvalues">
float, int, and stringarray via name (map)
</html:link>
你也能够链接到map类型的action上,上面的代码解析后的结果:
<html:messages property="property2" message="true" id="msg" header="messages.header" footer="messages.footer">
<tr><td><%= pagecontext.getattribute("msg") %></td></tr>
</html:messages>
2.) select和option标签
<html:select> 的属性:property-与actionform中的某个属性对应;size-显示option的数目;multiple-默认为fales,表示不能多选,当设定为true时,property对应的actionform的属性必须为数组。
<html:select property="name" size=6 multiple="true">
<html:option>的属性:key、local、bundle-指定resource bundle中的内容。
例如 <html:option value="color1">orange</html:option>
<html:option value="color1" bundle="htmlselect.colors" key="htmlselect.red"/>
它和配置文件中的<message-resources>元素的key属性匹配 --> <message-resource parmeter="htmlselectcolors" key="htmlselect.colors"/>
<message-resource>中配置的资源文件为htmlselectcolors.properties,相关内容为 htmlselect.red=red
<html:options>标签,提供了一组<option>元素,在<html:select>元素中可以包含多个<html:options>元素。非常灵活,可以取得集合或数组中的值。
例1 <html:options collection="coll" property="value" labelproperty="label" /> 这指在coll的集合中存放了options,value指实际能被提交的值,label是显示给用户的值。
例2 <html:options property="value" labelproperty="label" /> collection属性不被指定时,将使用表单相关的form bean,form bean中value属性存放option value,label属性值显示给用户。
例3 <html:options name="valuebean" property="values" labelname="labelsbean" labelproperty="labels" /> 这个意思是value值存放在名为valuebean的bean的vlaues属性中,它是一个collection;label值也是同样的意思。
<html:optionscollection>标签,和<html:options>的用法很相似。
例如 <html:select property="custid"><html:optionscollection property="customers" label="name" value="custid" /></html:select>
这个标签和org.apache.structs.util.labelvaluebean结合的很好,如果把label和value都放到这个对象中,可以很简单的这样应用:
<html:select property="custid"><html:optionscollection property="customers" /></html:select>
jsp struts之bean标签库详解
bean 标签库
此标签库和java bean有很强的关联性,设计的本意是要在jsp 和javabean 之间提供一个接口。struts 提供了一套小巧有用的标签库来操纵javabean和相关的对象:cookie、 header、 parameter、 define、write、message、 include、page、resource、size、struts。
1. bean:cookie、bean:header、bean:parameter
这三个标签用来重新得到cookie, request header和request parameter。
bean:header和bean:parameter标签定义了一个字符串;bean:cookie标签定义了一个cookie对象。你可以使用value属性做为默认值。如果找不到指定的值,且默认值没有设定的话,会抛出一个request time异常。如果你期望返回多个值的话,可把multiple属性设为true。
<bean:cookie id="sessionid" name="jsessionid" value="jsessionid-isundefined"/>
// 这段代码定义了一个名为sessionid的脚本变量,如果找不到一个名为jsessionid的cookie,那sessionid
// 的值就被设置为jsessionid-isundefined。
2.
下面代码会输出一些cookie对象的一些属性:
<jsp:getproperty name="sessionid " property="comment"/> …
<jsp:getproperty name="sessionid" property="domain"/> …
<jsp:getproperty name="sessionid" property="maxage"/> …
<jsp:getproperty name="sessionid" property="path"/> …
<jsp:getproperty name="sessionid" property="value"/> …
<jsp:getproperty name="sessionid" property="version"/> …
3.
下面是在request中输出所有header的例子:
<%
java.util.enumeration names =((httpservletrequest) request).getheadernames();
%>
…
<%
while (names.hasmoreelements()) {
string name = (string) names.nextelement();
%>
<bean:header id="head" name="<%= name %>"/>
… <%= name %>
… <%= head %>
…
<%
}
%>
4.
下面是parameter的例子:
<bean:parameter id="param1" name="param1"/>
<bean:parameter id="param2" name="param2" multiple="true"/> // 此处定义了一个param2[]。
<bean:parameter id="param3" name="param3" value="unknown value"/>
5.
于其它标签结合使用:
<bean:header id="browser" name="user-agent"/>
<p>you are viewing this page with: <bean:write name="browser"/></p>
----------------------------------------------------------------------------------------------------------------------------------
<bean:cookie id="username" name="username" scope="session"
value="new user" />
<p>welcome <bean:write name="username" property="value"/!</p>
// 根据cookie创建一个新的bean,如果用户名称已经存储在cookie中,它就不显示为一个新用户。
6.
7. bean:define:有三个用途。
一是定义新字符串常量:
<bean:define id="foo" value="this is a new string"/>
<bean:define id="bar" value='<%= "hello, " + user.getname() %>'/>
<bean:define id="last" scope="session" value='<%= request.getrequesturi() %>'/>
8.
二是复制一个现有的bean给新的bean:
<bean:define id="foo" name="bar"/>
<bean:define id="baz" name="bop" type="com.mycompany.myclass"/> //定义脚本变量的类型,默认为object
9.
三是复制一个现有的bean的属性给新的bean:
<bean:define id="bop" name="user" property="role[3].name"/>
<bean:define id="foo" name="bar" property="baz" scope="request" toscope="session"/>
//toscope属性指新bean的scope,默认为page
10. 上段代码的意思是把名为bar的bean的baz属性赋值给foo,foo的类型为string(默认)。
11. bean:include
这个标签和bean:include标签和相似,不同点就是它定义了一个可以复用的脚本变量。用id属性命名一个新的脚本变量,还支持forward、href、page和transaction.属性,和html:link中的属性意义一样。
<bean:include id="footerspacer" page="/long/path/footerspacer.jsp"/>
然后你能够在多个地方(scope为page)调用:
<bean:write name="footerspacer" />
12.
13. bean:message
用来实现对国际化的支持的一个标签,配合java.util数据包中定义的locale和resourcebundle类来完成这个任务,用java.text.messageformat类配置消息的格式。
首先要指定资源文件的名称。这个文件会包含用默认语言编写的在程序中会出现的所有消息,这些消息以“关键字-值”的形式存储。文件需要存储在类路径下,路径要作为初始化参数传送给actionservlet。
实现国际化的规定:所有的资源文件必须都存储在基本资源文件所在的目录中。基本资源文件包含的是用默认地区语言-本地语言编写的消息。如果基本资源文件的名称是applicationresources.properties,那么用其他特定语言编写的资源文件的名称就应该是applicationresources_xx.properties(xx为iso编码,如英语是en)。因此这些文件应包含相同的关键字,但关键字的值是用特定语言编写的。
然后,actionservlet的区域初始化参数必须与一个true值一起传送,这样actionservlet就会在用户会话中的action.locale_key关键字下存储一个特定用户计算机的区域对象。现在可以运行一个国际化的web站点,它可以根据用户计算机上的设置的区域自动以相应的语言显示。
使用特定的字符串来替换部分消息:
在资源文件中的定义:info.mykey = the numbers entered are {0},{1},{2},{3}
标记的使用:<bean:message key="info.mykey" arg0="5" arg1="6" arg2="7" arg3="8"/>
jsp页面的显示:the numbers entered are 5,6,7,8 // 最多支持4个参数
14.
15. bean:page:把jsp中的内部对象做为脚本变量。
<bean:page id="requestobj" property="request"/>
16.
17. bean:resource:获得应用程序的资源,这个资源可以是一个string或从java.io.inputstream中读入。使用servletcontext.getresource()servletcontext.getresourceasstream() 方法检索web应用中的资源,如果在检索资源时发生问题,就会产生一个ruquest time异常。
<bean:resource id="webxml" name="/web-inf/web.xml"/>
18. 使用input属性时,资源会做为一个inputstream,如果不指定就被当成一个string。
19. bean:size:得到存储在array、collection或map中的数目,类型为java.lang.integer。
<bean:size id="count" name="employees" />
20.
21. bean:struts:复制struct 对象(三种类型)给新的bean,scope为page。
<bean:struts id="form" formbean="customerform"/>
<bean:struts id="fwd" forward="success"/>
<bean:struts id="map" mapping="/savecustomer"/>
22.
23. bean:write:以字符串形式输出bean的属性值。
filter属性:设为true时,将html保留字转换为实体("<" 转换为 <);
ignore属性:如果对象不存在,不会抛出异常。
<bean:write name="userregistration" property="email" scope="request"/>
Tag: Struts ,标签库
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。