2012-04-25 10:23 velocity 自定义标签实现页面权限控制

部分页面:

复制代码
 <table width="100%" border="0" align="center" cellspacing="3" bgcolor="#FFFFFF">
    <tr class="text5">
     <td align="center">
      <input type="hidden" name="selectNodeId" id="selectNodeId" value="" />
      #btnOperate("/pas/business/chenYearAction!insert.do")
       <input type="button" class="input1" name="insertBtn"  value=" 新增 " id="insertBtn"/>
      &nbsp;&nbsp;
      #end
      #if ($chenYears.size() > 0)
       #btnOperate("/pas/business/chenYearAction!update.do")
       <input type="button" class="input1" name="updateBtn"  value=" 修改 " id="updateBtn"/>
       &nbsp;&nbsp;
       #end
       #btnOperate("/pas/business/chenYearAction!delete.do")
       <input type="button" class="input1" name="deleteBtn"  value=" 删除 " id="deleteBtn"/>
       &nbsp;&nbsp;
       #end
      #end
     </td>
    </tr>
   </table>
复制代码

配置:

在/WEB-INF/velocity.properties(没有就创建一个)中添加:

userdirective=com.shop.pas.system.util.VelocityTagDirective

处理JAVA类:VelocityTagDirective.java

复制代码
package com.shop.pas.system.util;

import java.io.IOException;
import java.io.Writer;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;

/**
 * velocity 控制页面展示问题 #btnOperate("权限连接") 被控制部分 #end
 * 
 * @author chenweixian
 * 
 */
public class VelocityTagDirective extends Directive
{
 // 指定指令的名称
 @Override
 public String getName()
 {
  return "btnOperate";
 } 

 // 指定指令类型为块指令
 @Override
 public int getType()
 {
  return BLOCK;
 } 

 // 指令内容操作
 @Override
 public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException
 {
  // 获得缓存信息
  SimpleNode sn_region = (SimpleNode) node.jjtGetChild(0);
  // 获得请求连接
  String requestUrl = (String) sn_region.value(context);

  // 判断当前用户是否有此连接权限
  try
  {
   if (UserAccessFlag.flagIsAllowAccess(requestUrl))
   {
    // 显示被自定义标签内的内容
    SimpleNode sn_key = (SimpleNode) node.jjtGetChild(1);
    sn_key.render(context, writer);
   }
  }
  catch (Exception e)
  {
  }

  return true;
 }
}
复制代码

 

 

posted on   陈惟鲜的博客  阅读(654)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2013年3月 >
24 25 26 27 28 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6
点击右上角即可分享
微信分享提示