Struts2 标签实现 多对多查询显示

Struts2 标签实现 多对多查询显示

嵌套遍历

有这样一个关系

博客的日志 Entry表、日志分类表Category表 、

还有一个中间表Entry_Category表分别是日志表主键和分类表的主键,又同时作为外键关联。

中间表在SSH框架逆向生成的时候并没有生成实体类。

在页面显示时 要 遍历日志的同时还要遍历日志的分类,

 

可以用到Struts2的<s:iterator>标签进行嵌套输出。

最外层的Value属性内是 request、session、application内的集合对象,id属性为 具体对象

内层的value属性为 具体对象内属性名(要和实体类内属性名一致),

最后通过<s:propertyvalue=”set 集合对象内的属性名” /> 输出

首先导入标签库

<%@ taglib uri="/struts-tags" prefix="s" %>

利用

 <s:iterator value="list" id="entry">

<s:iterator value="blogCategories" >

<s:property value="name"/>

</s:iterator>

</s:iterator>

 

实体日志bean

publicclass BlogEntry implements java.io.Serializable {

 

    // Fields

 

    private Long id;

    private BlogUser blogUser;

    private String title;

    private String summary;

    private String content;

    private Date modifytime;

    private Date posttime;

    private String postip;

    private Integer hits;

    private Integer commensize;

    private String entrystatus;

    private Set blogComments = new HashSet(0);

                    private Set blogCategories = new HashSet(0);

实体类 日志类型

publicclass BlogCategory implements java.io.Serializable {

 

    // Fields

 

    private Long id;

    private String name;

    private String description;

    private Integer defaultcategory;

    private Date createtime;

                    private Set blogEntries = new HashSet(0);

 

 

要将延迟加载设置为 取消 lazy="false"

BlogEntry.hbm.xml 配置文件

<set name="blogComments"inverse="true" lazy="false">

            <key>

                <column name="ENTRYID" precision="10"scale="0" not-null="true"/>

            </key>

            <one-to-many class="com.tc.entity.BlogComment" />

        </set>

        <set name="blogCategories" table="BLOG_ENTRY_CATEGORY" schema="WANG" lazy="false">

            <key>

                <column name="ENTRYID" precision="10"scale="0" not-null="true"/>

            </key>

            <many-to-many entity-name="com.tc.entity.BlogCategory">

                <column name="CATEGORYID" precision="10" scale="0"not-null="true" />

            </many-to-many>

        </set>

BlogCategory.hbm.xml配置文件

<set name="blogEntries"inverse="true" table="BLOG_ENTRY_CATEGORY"schema="WANG" lazy="false">

            <key>

                <column name="CATEGORYID" precision="10" scale="0"not-null="true" />

            </key>

            <many-to-many entity-name="com.tc.entity.BlogEntry">

                <column name="ENTRYID" precision="10"scale="0" not-null="true"/>

            </many-to-many>

        </set>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值