struts标签——nested

Struts Nested标签库的分两部分:

1) 一部分用于表达JavaBean之间的嵌套关系html

2) 另外一部分可以在特定的嵌套级别提供和其余Struts标签相同的功能。jsp

a)   <nested:nest>,定义一个新的嵌套级别spa

b)   <nested:writeNesting>,输出当前嵌套级别信息hibernate

c)   <nested:nest>标签能够表达JavaBean之间的嵌套关系orm

eg.htm

以三个JavaBean为例,分别是:PersonForm BeanPerson BeanAddress Bean,在PersonForm Bean中包含一个Person Bean类型的属性person,在Person Bean中又包含一个Address Bean类型的属性address对象

则用nested标签表示以下:it

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

<html:form action="/showPerson">ast

<nested:nest property="person">

LastName:

<nested:text property="lastName"/><BR>
          .....

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

第二个<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    userUserInfo    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>  


  
 因为useruserInfo自己就是嵌套的,因此第二种方式就在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进行处理,很是方便。

相关文章
相关标签/搜索