struts2自定义标签控制权限按钮是否显示

1、

package com.asppro.JspTag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.opensymphony.xwork2.util.ValueStack;

//继承ComponentTagSupport类是为了获得JSP页面中用户自定义的标签中设置的属性值,并包装成Component对象
public class PermissionControlTag extends ComponentTagSupport {
    private static final long serialVersionUID = -4539195579251655410L;

    // 目前为标签中传递过来的菜单ID值,再根据此id获取按钮信息
    private String reqUrl;

    public String getReqUrl() {
    return reqUrl;
    }

    public void setReqUrl(String reqUrl) {
    this.reqUrl = reqUrl;
    }

    @Override
    public Component getBean(ValueStack valueStack, HttpServletRequest request, HttpServletResponse response) {
    return new PermissionControlComponent(valueStack, request);
    }

    // 此方法是用来将JSP页面传来的属性值赋给基本类
    protected void populateParams() {
    super.populateParams();
    PermissionControlComponent psc = (PermissionControlComponent) component;
    // 将jsp标签中的值传递此属性当中
    psc.setReqUrl(reqUrl);
    ;
    }

}

2、

package com.asppro.JspTag;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.asppro.model.MenuDto;
import com.asppro.model.RoleDto;
import com.asppro.service.IMenuService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

//继承Component类是为了从Struts2中的ValueStack中获得相对应的值。
public class PermissionControlComponent extends Component {

    private String reqUrl;

    // // @Autowired
    // private IMenuService menuService;
    //
    // public IMenuService getMenuService() {
    // return menuService;
    // }
    //
    // public void setMenuService(IMenuService menuService) {
    // this.menuService = menuService;
    // }

    public PermissionControlComponent(ValueStack stack, HttpServletRequest request) {
    super(stack);
    // this.setMenuService(request);
    }

    // public void setMenuService(HttpServletRequest request) {
    // WebApplicationContext wac =
    // WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
    //
    // this.menuService = (IMenuService) wac.getBean("menuService");
    //
    // }

    public String getReqUrl() {
    return reqUrl;
    }

    public void setReqUrl(String reqUrl) {
    this.reqUrl = reqUrl;
    }

    // 自己需要输出的逻辑通过writer输出字符串就可以了
    public boolean start(Writer writer) {
    try {
//        System.out.println("---------------id的值:" + this.id);
        String resultContent = contentWriter();
        writer.write(resultContent);
    } catch (IOException e) {
        e.printStackTrace();
    }
    boolean flag = super.start(writer);

    return flag;
    }

    // 将内容根据ID获取出来
    protected String contentWriter() {
    Map session = ActionContext.getContext().getSession();
    List<String> permissionList = (List<String>) session.get("permission");
    // Map<String, Object> roleMapSession =
    // ServletActionContext.getContext().getSession();

    // System.out.println(ServletActionContext.getContext().getValueStack());
    // System.out.println(ServletActionContext.getRequest().getAttribute("MENU_ID"));

    // String strMenuid = (String)
    // ServletActionContext.getRequest().getAttribute("MENU_ID");

    // 从session获取当前登陆用户的角色信息
    // List<RoleDto> roleList = (List<RoleDto>) roleMapSession.get("role");

    // String menuCode[] = { "userManage" };

    // Map<String, Object> map = new HashMap<String, Object>();
    // map.put("strMenuid", strMenuid);
    // map.put("roleID", roleList.get(0).getId());
    //
    // List<MenuDto> menuList = menuService.getMenuButtonByShow(map);
    String buttonShowHtml = "";
    if (!permissionList.isEmpty() && permissionList.size() > 0) {
        // 循环将数据输出
        if(permissionList.contains(reqUrl)){
        buttonShowHtml = "";
        }else{
        buttonShowHtml = "οnclick='javascript:return false;' style='color:gray'";
        }
//        for (int i = 0; i < menuList.size(); i++) {
//        MenuDto menuDto = menuList.get(i);
//        // <a
//        // href="getMenuAction!getMenu.action?id=${id}&forward=viewMenuJsp"
//        // class="u-tb"><span class="see"></span>查看</a>
//        buttonShowHtml += "<a href='" + menuDto.getButtonUrl() + "' class='u-tb'><span class='see'></span>" + menuDto.getButtonName() + "</a>";
//        }

    }
    //System.out.println(menuList.get(0).getButtonName());

    return buttonShowHtml;
    }

}

 

3、在WEB-INFO下定义tld文件

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <display-name>auto defined tags</display-name>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>m</short-name>
    <uri>/auto-defined-tags</uri>

    <tag>
        <description><![CDATA[permission tag]]></description>
        <name>permission</name>
        <tag-class>com.asppro.JspTag.PermissionControlTag</tag-class>
        <body-content>JSP</body-content>
        <!-- 定义标签中的属性 -->
        <attribute>
            <description><![CDATA[Expression to determine if body of tag is to be displayed]]></description>
            <name>reqUrl</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <dynamic-attributes>false</dynamic-attributes>
    </tag>


</taglib>

 

4、在页面上用如下方式引用

<%@taglib prefix="m" uri="/auto-defined-tags"%>

<a <m:permission reqUrl='getMenuAction!getMenu.action' /> href="getMenuAction!getMenu.action?id=${id}&forward=viewMenuJsp" class="u-tb"><span class="see"></span>查看</a>

注:自定义标签中的reqUrl属性不能使用特殊的符号,具体可以通过自测。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值