Velocity宏递归

本文介绍了一种基于Velocity模板引擎的权限控制方案,利用含有多个用户ID的字段进行权限判断,实现了用户可见菜单项的动态加载。同时提供了两种不同的权限验证方法:一种是通过包含检查,另一种是使用SQL模糊匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


////////注意 选择人员可有多个,并以","隔开存入数据库字段中。velocity中权限登录用户id包含在那个以","隔开的字段中就可以了。简单.

<div style="border: solid 3px red;">

    当前用户:【${AccessUser.getUserName()}】
                <div class="menu_dropdown bk_2">    
                    #loadsub('')        
                    #macro(loadsub $parentId)
                    #foreach($channel in ${t_work.getChannelAll("$parentId")} )
                    #set($channels = ${t_work.getChannelAll("$channel.channelId")})
                        #set($checkChannel="")
                        #set($checkChannel=$!channel.reader)
                            ##可查看人权限
                            #if($!channel.hidden==1&&$checkChannel.contains("${AccessUser.getUserId()}"))
                                    #if($channels.size()>0)
                                        <dl>
                                            <dt>    <i id="$!{velocityCount}" class="fa fa-folder-o fa-color"></i>
                                                   $channel.channelName<i class="fa fa-plus-square-o fa-color"></i>
                                            </dt>
                                            <dd>
                                            <li>#loadsub($channel.channelId)</li>    
                                            </dd>
                                        </dl>
                                    #else
                                        <li><a id="$!channel.channelId" _href="$!base/private/information/list.jhtml?channelId=$!channel.channelId" href="javascript:void(0)">
                                            <i class="fa fa-circle-o fa-color"></i>$!channel.channelName</a>
                                        </li>
                                    #end
                            #end
                    #end
        #end
            </div>

</div>

toolbox.xml中的getChannelAll()方法,

public List<CmsInfoChannelEntity> getChannelAll(String channelId){
        return WorkFactory.getCmsInfoChannelRemote().findByParent(channelId);
    }

java类中的实现

	public List<CmsInfoChannelEntity> findChildByParent(String parentId) {
		String query = null;
		if (StringUtils.isEmpty(parentId)) {
			query = "select s from CmsInfoChannelEntity s where s.channelParentId is null order by s.channelLevel,s.channelSort ";
			return this.findList(query);
		}
		query = "select s from CmsInfoChannelEntity s where s.channelParentId=?1 order by s.channelLevel,s.channelSort ";
		return this.findList(query, parentId);
	}


///////另外一种方法是不使用velocity的包含contains;

/////而是使用like模糊匹配

public List<CmsInfoChannelEntity> channellistByCurrentUserId(String channelId,String currentUserId) {

String sql = new String("select s from CmsInfoChannelEntity s where 1=1 ");

if(!StringUtils.isEmpty(currentUserId)){

sql+=" and s.reader like '%"+currentUserId+"%'";

}

if(!StringUtils.isEmpty(channelId)){

sql+=" and s.channelId ='"+channelId+"'";

}

return this.cmsInfoChannelDao.findList(sql);

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值