struts标签用法大全(编辑修改稿)内容摘要:

:cancel 下面是对应的 action 中的代码: if(isCancelled(request)){ //action 被取消时要做的事情写在这里 return (cancel)。 }else{ //action 没有被取消时要做的事情写在这里 return (success)。 } html:select html:select 标签生成一个 select 元素。 是单选还是多选取决于该标签的 multiple 属性。 如果指定了 multiple=true则为多选,此时对应的属性应该是一个数组。 如果没有指定 multiple=true则为单选,此时对应的属性应该是标量。 注意:为了正确的处理没有做任何的选择的情况,在 ActionForm中的 reset()方法中必须将标量属性设置为默认值而将数组的长度置为 0。 另外的一个重要问题就是 struts 如何生成 option 元素了,这个任务 struts 交给了 html:option、 html:options 和 html:optionsCollection 三个标签。 html:option 标签 html:option标签生成一个 HTML 的 option 元素。 该标签必须嵌在 html:select 标签中。 它的显示文本来自其标签体,也可以来自于资源文件。 它的 value 属性用来指定什么值将要被提交。 html:option value=oneone/html:option html:option value=twotwo/html:option html:options 标签 html:options 标签生成 多个 HTML 的 option 元素。 该标签必须嵌在 html:select 标签中。 而且工作方式有些特殊,它的 name 与 property 属性和其它标签的 name 与 property 属性意义不一致,让我们具体看一下它的工作方式。  指定 collection 属性  没有指定 collection 属性 指定 collection 属性 让我通过示例来介绍在指定 collection 属性时该标签的工作方式,首先要说明 selectForm中的 persons 和 listForm中的 persons 完全一致。 请参见 bean:define 标签。 下面的代码先利用 bean:define 标签将 selectForm中的 persons取到 page 作用域中,然后html:options标签再依据 collection=personCollection选中这个 persons并将其中的每一个对象 (Person 类型 )生成一个 option 元素。 该标签的 property=id表示 persons 中的对象 (Person 类型 )的 id 属性将作为 option 元素的 value 值。 该标签的 labelProperty=name表示persons 中的对象 (Person 类型 )的 name 属性将作为 option 元素的 label 值。 当这个 select 提交时所选择的值将被提交到 selectForm(name=selectForm)中的 person对象 (这是在 SelectForm中声明的一个 Person 类型的域专门用来接收提交的值 )的 id 属性中 (property=)。 bean:define id=personCollection name=selectForm property=persons/ html:select name=selectForm property= size=1 html:options collection=personCollection property=id labelProperty=name/ /html:select 没有指定 collection 属性 让我通过示例来介绍没有指定 collection 属性时该标签的工作方式,先来看看 ids 和 names的定义: private ListString ids = null。 private ListString names = null。 上面的代码来自 SelectForm,其中 ids 是一个 String 的列表, names 也是一个 String的列表。 我们暂时假定这两个列表含有相同数目的元素。 有了这些让我们开始介绍下面的代码。 html:options 标签用 ids 中的第 i个值作为 option 元素的 value值同时使用 names中相同位置的值 (第 i 个值 )作为 option 元素的 label 值。 如果 ids 比 names 长那么多出的 ids 中的值将即作为 option 的 value 又作为 option 的 label。 如果 ids 比 names 短那么多出的 names的值会被丢掉。 当这个 select 提交时所选择的值将被提交到 selectForm(name=selectForm)中的 person对象 (这是在 SelectForm中声明的一个 Person 类型的域专门用来接收提交的值 )的 id 属性中 (property=)。 html:select name=selectForm property= size=1 html:options property=ids labelProperty=names/ /html:select html:optionsCollection 标签 html:optionsCollection 标签生成多个 HTML 的 option元素。 该标签必须嵌在 html:select 标签中。 它的功能和 html:options 标签的相同,但是它的 name 与 property 属性和其它标签的 name 与 property 属性意义一致,理解起来比较自然。 让我通过示例来介绍 html:optionsCollection 标签的用法。 首先依据 name=selectForm和 property=persons取到 selectForm中的 persons 列表,然后将列表中的对象 (Person类型 )的 id 属性作为 option 元素的 value 值 (value=id),将列表中的对象 (Person 类型 )的 name属性作为 option 元素的 label 值 (label=name)。 html:select name=selectForm property= size=1 html:optionsCollection name=selectForm property=persons label=name value=id/ /html:select 下面是一个多选的示例,虽然示例中使用了 html:options 标签,但是 html:option 和 html:optionsCollection 也能够用来多选。 而且您还必须意识到 html:option、 html:options 和 html:optionsCollection这三个标签可以同时使用。 代码中的 personIds 是 SelectForm中声明的一个 String[]类型的数组用来接收提交的多个值。 html:select name=selectForm property=personIds multiple=true size=2 html:options property=ids labelProperty=names/ /html:select html:checkbox html:check 标签生成一个 checkbox。 这里的 value 值可以是 true, yes 或 on。 如果您要提交其它的值 (如某种形式的标识 )应该考虑使用 html:multibox标签。 注意 :为了正确的处理没有选中的 checkbox您必须在 reset()中设置对应的属性为 false。 下面的代码示例了 html:checkbox标签的用法,其中 CheckboxForm中声明了三个 boolean 类型的域,如下: html:checkbox name=checkboxForm property=oneOne/html:checkbox html:checkbox name=checkboxForm property=twoTwo/html:checkbox html:checkbox name=checkboxForm property=threeThree/html:checkbox 如果选中后被提交则相应的属性的值为 true。 html:radio html:radio 标签生成一个 radio。 主要的用法有两种,下面我们通过代码来示例。 下面的代码示例了 html:radio 标签的一般用法,如果被提交则选中的 radio 的 value 值将被提交到 radioForm中的 id 中。 html:radio name=radioForm property=id value=00001One/html:radio html:radio name=radioForm property=id value=00002Two/html:radio 下面的代码示例了 html:radio 标签的典型用法,其中的 persons 和 bean:define 标签中的一致,您可以参考 bean:define标签。 我只介绍这个 html:radio idName=person property=id value=id, idName 指定 html:radio 要使用的 bean(这里为 person), value=id表示person的 id属性将作为 radio元素的 value 值而 property=id表示提交时选中的 radio的值将被提交给 radioForm中的 id 属性。 logic:notEmpty name=radioForm property=persons logic:iterate id=person name=radioForm property=persons html:radio idName=person property=id value=id bean:write name=person property=name/ /html:radio /logic:iterate /logic:notEmpty html:multibox html:multibox标签生成多个 checkbox。 当您要使用大量的 checkbox时使用这个标签非常方便,可以使您避免在 ActionForm中 声明大量的 boolean 类型的变量,带之以一个数组就行了。 注意 :为了正确的处理没有选中的 checkbox您必须在 reset()中设置数组的长度为 0。 下面的代码示例了 html:multibox标签的一般用法,如果被提交则选中的所有 checkbox的 value 值将被提交到 multiboxForm中的 selectedItems 中,这是一个 String[]数组。 html:multibox name=multiboxForm property=selectedItems value=00001/ h。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。