【网络转帖】Struts标签库
2010-06-18 00:11
281 查看
用<form>标签时必须遵循一些规则:
标签中必须包含一个action属性,它是这个标签中唯一必需的属性。如果不具
备该属性则JSP页面会抛出一个异常。之后你必须给这个action属性指定一个有效值。一个有效值是指应用程序的Struts配置文件中元素里的任何一
个子元素的访问路径。而且相应的元素中必须有一个name属性,它的值是form bean的名称。
任何包含在<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属性时你需要给它指定元素的名称。
有没有看到这个标签库是如何建立JavaScript来定焦到password元素上的? 这也是该库让人着迷的地方之一。你不用担心如何在客户端进行编程,它会帮你自动生成。
还可以看到,<form>标签中method属性的缺省值是POST。
<text>标签、<hidden>标签、<textarea>标签、<radio>标签、<checkbox>标签、<submit>标签、<reset>标签:
都有一个property属性,最后会被转换成HTML中的name属性,当然还有name和value属性。
<password>标签
该标签中的一个很重要的属性是"redisplay",它用于重新显示以前输入到这个区域中的值。该属性的缺省值为true。然而,为了使password不能被重新显示,你或许希望将该属性的值设为false。
<select>标签和<option>标签:
遗补:1.) <html:link>标签
forward属性:链接到一个global forward上;action属性:链接到一个action mapping上;
href属性:这个链接会转发给控制器,由控制器做决定;page属性:一个相对的链接。
用page属性链接到action上:
注意,上面的代码中你不必指定web的关联。相反的,如果你使用href属性,你就必须像下面所示指出web的关联(这里的关联就是struts-exercise):
很明显,当你在相同的web应用程序中做链接是,它比page属性更加好。你也能用href在不同的服务器上创建链接:
另一种链接到html-link.do的方法是用action属性:
你也可以以硬编码的方式使用参数:
或者使用paramId, paramName, and paramProperty属性:
解析后的代码:
另外,还能使用带name属性的Map来实现传递多个参数:
你也能够链接到Map类型的action上,上面的代码解析后的结果:
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>
标签中必须包含一个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相关联的。 |
(<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> |
还可以看到,<form>标签中method属性的缺省值是POST。
<text>标签、<hidden>标签、<textarea>标签、<radio>标签、<checkbox>标签、<submit>标签、<reset>标签:
都有一个property属性,最后会被转换成HTML中的name属性,当然还有name和value属性。
<password>标签
<html:password property=/"password/" redisplay=/"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> |
forward属性:链接到一个global forward上;action属性:链接到一个action mapping上;
href属性:这个链接会转发给控制器,由控制器做决定;page属性:一个相对的链接。
用page属性链接到action上:
<html:link page="/html-link.do"> Linking with the page attribute. </html:link> |
<html:link href="/struts-exercise-taglib/html-link.do"> Using Href </html:link> |
<html:link href=" http://otherserver/strutsTut/html-link.do "> Using Href </html:link> |
<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> |
<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> |
<% 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> |
<html:messages property="property2" message="true" id="msg" header="messages.header" footer="messages.footer"> <tr><td><%= pageContext.getAttribute("msg") %></td></tr> </html:messages> |
<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>
相关文章推荐
- 总结STRUTS标签【转帖】
- 【转帖】Struts的html:checkBox标签超郁闷的问题本篇文章来
- jsp中用struts的push标签时 ,程序运行报异常Stream closed 解决方案一
- struts2.1.6 datetimepicker标签不能使用的问题解决
- struts2.0的标签库(简介)
- struts中s标签,出现默认的文本框
- Struts标签没有name属性怎么办?
- Struts 2的标签库(1)——标签库基础
- struts2.0的标签库(简介)
- 【JavaWeb-16】静态参数封装、3种动态参数封装、数据类型转换、struts标签、验证器
- Struts 2 <s:hidden> 标签
- Struts 1 Logic 标签 - Forward And Redirect
- [转][struts]标签 HTML:MESSAGES和HTML:ERRORS用法简例
- Struts的FormBean及常见错误和标签的使用
- Struts的Logic标签
- struts的date标签和oracle中时间的转换,生成dto的方法
- Struts 有哪些常用标签库
- JS与Struts标签page
- Struts标签分类
- Struts2.0详细标签库