Struts Nested标签

来源:互联网 发布:mysql怎样修改root密码 编辑:程序博客网 时间:2024/04/28 10:09
Struts Nested标签库的一部分标签用于表达JavaBean之间的嵌套关系,还有一部分标签能够在特定的嵌套级别提供和其他Struts标签相同的功能。
  • <nested:nest>,定义一个新的嵌套级别
  • <nested:writeNesting>,输出当前嵌套级别信息

<nested:nest>标签可以表达JavaBean之间的嵌套关系,以三个JavaBean为例,分别是:PersonForm Bean,Person Bean和Address Bean,在PersonForm Bean中包含一个Person Bean类型的属性person,在Person Bean中又包含一个Address Bean类型的属性address。

定义两个<nested:nest>标签,第一个<nested:nest>标签嵌套在<html:form>标签中,如下:

      <html:form action="/showPerson">

           <nested:nest property="person">

                  Last Name:<nested:text property="lastName"/><BR>

          .....

         </nested:nest>

     </html:form>

以上<nested:nest>标签的上层JavaBean为於<html:form>表单标签对应的PersonForm Bean,<nested:nest>标签的property属性为“person",代表PersonForm Bean的person属性,这个person属性代表Person Bean,因此嵌套在<nested:nest>标签内部的Nested标签都相对于这个Person Bean,例如第一个<nested:text>标签的property属性”lastName“,代表Person Bean的lastName属性。

第二个<nested:nest>标签嵌套在第一个<nested:nest>标签内部:如下

<html:form action="/showPerson">

           <nested:nest property="person">

            .............

                     <nested:nest property="address">

                       Current nesting is :<nested:writeNesting/><br>

                       Street 1:<nested:text property="street1"/><BR>

          .....

         </nested:nest>

     </nested:nest>

     </html:form>

在以上代码中,第二个<nested:nest>标签的property属性为“address",代表PersonBean 的address属性,这个address属性代表Address Bean,因此嵌套在第二个<nested:nest>标签内部的Nested标签都相对於这个Address Bean。

   第二个<nested:nest>标签内还嵌套了一个<nested:writeNesting>标签,它显示当前的嵌套级别,输出结果为”person.address".

在默认情况下,<nested:nest>标签的property属性为当前ActionForm Bean的某个属性,或者为於上层<nested:nest>标签对应的JavaBean的某个属性,可以使用<nested:root>标签来显式指定顶层级别的JavaBean,<nested:root>标签的name属性指定JavaBean的名字,嵌套在<nested:root>标签中的<nested:nest>标签的property属性为这个JavaBean的某个属性。

和其他标签库中的标签功能相同的Nested标签

许多Nestd标签库中的标签具有和其他标签库中的标签相同的功能,区别在于Nested标签库中的标签属性相对于当前的嵌套级别,例如

   <nested:nest property = "person">

        Last name :<nested:text property="lastName"/>

   </nested:nest>

上面的<nested:text>标签和<html:text>标签具有相同的功能,都可以生成文本框,两者的区别在于<nested:text>标签的property属性为於当前嵌套级别对应的JavaBean的某个属性,而<html:text>标签的property属性为於当前表单对应的ActionForm Bean的某个属性。

 

====================================

比如我有一个User类和一个UserInfo类,前者记录用户的帐号密码,后者记录用户的详细信息。前者也有一个UserInfo属性,这样它们两者是嵌套了。   
   现在我要把这个用户的帐号和详细信息都显示到界面上。   
   一种方式是在actionForm中用两个属性User    user和UserInfo    userInfo来存储,在jsp中就可以用如下方式显示出来:   
   <nested:nest    property="user">   
           帐号:<nested:write    property="account"/>   
   </nested:nest>   
   <nested:nest    property="userInfo">   
           姓名:<nested:write    property="name"/>   
           性别:<nested:write    property="sex"/>   
   </nested:nest>   
   由于user和userInfo本身就是嵌套的,所以第二种方式就在actionForm中使用一个User    user属性即可:   
   <nested:nest    property="user">   
           帐号:<nested:write    property="account"/>   
           <nested:nest    property="userInfo">   
                   姓名:<nested:write    property="name"/>   
                   性别:<nested:write    property="sex"/>   
           </nested:nest>   
   </nested:nest>   
    
   这样处理是不是很方便了,actionForm可以直接放上数据存储对象,如果使用了hibernate做数据持久层,我们就可以直接把持久画对象放入actionForm来显示到界面上,不用在actionForm里写很多属性来分别存储数据,也免去了给这些属性分别赋值的过程。   
    
   如果我们把上边例子中的<nested:write/>标记换成<nested:text/>,这就类似于<html:text/>标记,是一个输入框,这样我们就可以把街面上输入一次提交到actionForm中的这个数据存储对象,比如user。我们在action中就可以直接获得这个user进行处理,非常方便。

====================================

 

原创粉丝点击