📄 buttontag.java
字号:
// =========================================================
// 项目:嘉兴市民健康信息系统
// 路径:$Archive: $
// =========================================================
package cn.gov.jiaxing.health.common.taglib;
import java.sql.SQLException;
import javax.servlet.jsp.JspException;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.taglib.TagUtils;
import com.shsafe.common.util.JavaScriptUtil;
import cn.gov.jiaxing.health.common.dto.UserDTO;
import cn.gov.jiaxing.health.common.securitymanager.SecurityManager;
/**
* 实现权限控制的按钮标签类。<br>
* 如果没有权限实现某种操作,则不显示此按钮
*
* @author $Author: Jisj$
* @version $Revision: $ $Date: $
*/
public class ButtonTag extends org.apache.struts.taglib.html.ButtonTag {
/** serialVersionUID */
private static final long serialVersionUID = 165198465L;
/** 进入权限常量值 */
private static final int ENTRY = 0;
/** 检索权限常量值 */
private static final int SEARCH = 1;
/** 插入权限常量值 */
private static final int INSERT = 2;
/** 更新权限常量值 */
private static final int UPDATE = 3;
/** 删除权限常量值 */
private static final int DELETE = 4;
/** 上传权限常量值 */
private static final int UPLOAD = 5;
/** 下载权限常量值 */
private static final int DOWNLOAD = 6;
/** 审核权限常量值 */
private static final int AUDIT = 7;
/** 保存权限常量值 */
private static final int SAVE = 8;
private boolean isDisplay = false;
/**
* 设置是否显示Button的字符串
*/
private String dispNone = "";
/**
* The type of button, such as "Search", "update", "delete", "insert",
* "upload", "download"
*/
private String type = null;
/**
* 是否是submit按钮
*/
private String submit = null;
/**
* 是否是reset按钮
*/
private String reset = null;
/**
* 是否是file按钮
*/
private String isFileButton = null;
/**
* 判断标签库生成代码是否是在JavaScript的字符串中。
*/
private boolean inJavaScript = false;
/**
* 可操作的类型
*/
protected static final String[] TYPES = { "Entry", "Search", "Insert",
"Update", "Delete", "Upload", "Download", "Audit", "Save", };
/**
* @see javax.servlet.jsp.tagext.Tag#doStartTag()
*/
@Override
public int doStartTag() throws JspException {
dispNone = "";
return super.doStartTag();
}
/**
* Process the end of this tag.
* <p>
* Support for Indexed property since Struts 1.1
*
* @return <code>EVAL_PAGE</code>
* @exception JspException
* if a JSP exception has occurred
*/
public int doEndTag() throws JspException {
// Generate an HTML element
StringBuffer results = new StringBuffer();
results.append(getElementOpen());
prepareAttribute(results, "name", prepareName());
prepareButtonAttributes(results);
results.append(prepareEventHandlers());
results.append(prepareStyles());
prepareOtherAttributes(results);
results.append(getElementClose());
String html = results.toString();
if (isInJavaScript()) {
html = JavaScriptUtil.escape(html);
}
TagUtils.getInstance().write(pageContext, html);
if (isDisplay && text != null && !StringUtils.isEmpty(value)) {
TagUtils.getInstance().write(pageContext, text);
}
return (EVAL_PAGE);
}
/**
* Release
*
* @see org.apache.struts.taglib.html.SubmitTag#release()
*/
@Override
public void release() {
this.isDisplay = false;
this.inJavaScript = false;
this.dispNone = null;
this.submit = null;
this.reset = null;
super.release();
}
/**
* Returns the style attribute.
*
* @see org.apache.struts.taglib.html.BaseHandlerTag#getStyle()
*/
@Override
public String getStyle() {
// if authority is limited, then set style with "display:none"
UserDTO userDto = (UserDTO) pageContext.getSession().getAttribute(
UserDTO.SESSION_KEY);
SecurityManager securityManager = null;
try {
securityManager = new SecurityManager(userDto);
} catch (SQLException e) {
throw new NullPointerException("Connection Error" + e);
}
// 判断选择的菜单ID
int selectType = 0;
String menuId = userDto.getCurrMenuId();
for (int i = 0; i < TYPES.length; i++) {
if (type.equalsIgnoreCase(TYPES[i])) {
selectType = i;
break;
}
}
// 根据Button类型判断是否显示
isDisplay = canDisp(securityManager, selectType, menuId);
if (!isDisplay) {
dispNone = "display:none";
}
if (StringUtils.isEmpty(super.getStyle())) {
dispNone = ";" + dispNone;
}
return super.getStyle() + dispNone;
}
private boolean canDisp(SecurityManager securityManager,
int authorityConst, String menuId) {
switch (authorityConst) {
case ENTRY:
// 测试是否有检索权限
return securityManager.canEntry(menuId);
case SEARCH:
// 测试是否有检索权限
return securityManager.canSearch(menuId);
case INSERT:
// 测试是否有检索权限
return securityManager.canInsert(menuId);
case UPDATE:
// 测试是否有更新权限
return securityManager.canUpdate(menuId);
case DELETE:
// 测试是否有删除权限
return securityManager.canDelete(menuId);
case UPLOAD:
// 测试是否有插入权限
return securityManager.canUpdate(menuId);
case DOWNLOAD:
// 测试是否有上传权限
return securityManager.canDownload(menuId);
case AUDIT:
// 测试是否有下载权限
return securityManager.canAudit(menuId);
case SAVE:
// 测试是否有保存权限
return securityManager.canInsert(menuId)
|| securityManager.canUpdate(menuId);
default:
return false;
}
}
/**
* Render the opening element.
*
* @return The opening part of the element.
*/
protected String getElementOpen() {
if (StringUtils.equalsIgnoreCase(getSubmit(), "true")) {
return "<input type=\"submit\"";
}
if (StringUtils.equalsIgnoreCase(getReset(), "true")) {
return "<input type=\"reset\"";
}
if (StringUtils.equalsIgnoreCase(getIsFileButton(), "true")) {
return "<input type=\"file\"";
}
return "<input type=\"button\"";
}
/**
* 返回<code>type</code>的值。
*
* @return <code>type</code>的值
*/
public String getType() {
return type;
}
/**
* 设置字段<codSe>type</code>的值。
*
* @param type
* 要设置的<code>type<code>字段的新值
*/
public void setType(String type) {
this.type = type;
}
/**
* 返回<code>inJavaScript</code>的值。
*
* @return <code>inJavaScript</code>的值
*/
public boolean isInJavaScript() {
return inJavaScript;
}
/**
* 设置字段<code>inJavaScript</code>的值。
*
* @param inJavaScript
* 要设置的<code>inJavaScript<code>字段的新值
*/
public void setInJavaScript(boolean inJavaScript) {
this.inJavaScript = inJavaScript;
}
/**
* 返回<code>submit</code>的值。
*
* @return <code>submit</code>的值
*/
public String getSubmit() {
return submit;
}
/**
* 设置字段<code>submit</code>的值。
*
* @param submit
* 要设置的<code>submit<code>字段的新值
*/
public void setSubmit(String submit) {
this.submit = submit;
}
/**
* 返回<code>reset</code>的值。
*
* @return <code>reset</code>的值
*/
public String getReset() {
return reset;
}
/**
* 设置字段<code>reset</code>的值。
*
* @param reset
* 要设置的<code>reset<code>字段的新值
*/
public void setReset(String reset) {
this.reset = reset;
}
/**
* 返回<code>isFileButton</code>的值。
*
* @return <code>isFileButton</code>的值
*/
public String getIsFileButton() {
return isFileButton;
}
/**
* 设置字段<code>isFileButton</code>的值。
*
* @param isFileButton
* 要设置的<code>isFileButton<code>字段的新值
*/
public void setIsFileButton(String isFileButton) {
this.isFileButton = isFileButton;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -