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/>
此行代码解析后:
<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/> </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"/>
转载请注明原文地址: https://ibbs.8miu.com/read-16736.html