这两天闲来无事,为方便以后项目需要改变按钮功能,制作了一个自定义标签的解决方案。
使用方法:
1、将jar包导入,当然也可发按其它方式导入。
2、并将rar包解压,放入WEB目录下的某个位置(路径A)!至此导入完毕。
3、调用方式:
在待使用的页面增加下述代码(注意此处路径为A):
<%@ taglib prefix="g" uri="/rmt_button /rmt-tag.tld" %>
<link rel="stylesheet" type="text/css" href="/rmt_button/btn-style.css" />
<script language=JavaScript src="/rmt_button /Jbutton.js"></script>
自定义按钮使用方式:
<g:button key="SAVE" chkAccess="${false}" name="保存" id="saveBtn">
insertBtn.onclick = function(){
//你的方法体
}
</g:button>
4、上述按键key值和id值不可以重复。其中chkAccess属性暂时未后,其实它可以用来控制此按钮的权限,等后面有时候再上传。
5、如果对按钮样式不满意,可以调整css(不推荐),简单的做法是直接更改图片。
最终效果看图片附件。
类源码如下 :
package com.rmt.tags;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
/**
* @author duanxw
* @Date Oct 12, 2010
* @package com.flagsky.tags
* @class Jbutton
* @since JDK1.5
* 描述: 自定义按钮标签
*/
public class Jbutton extends BodyTagSupport{
private static final Log log = LogFactory.getLog(Jbutton.class);
private HttpServletRequest request;
protected JspWriter out;
private String path;
private String id;
private String key;
private String name;
private boolean chkAccess;
@Override
public int doStartTag() throws JspException {
request = (HttpServletRequest) this.pageContext.getRequest();
path = request.getContextPath();
out = this.pageContext.getOut();
return this.EVAL_BODY_BUFFERED;
}
@Override
public int doAfterBody() throws JspException {
try {
String sid = "_"+id+"_id";//_insertBtn_id
out.print("<table id='" + sid + "' cellpadding='0' cellspacing='0'>"
+ " <tr>"
+ " <td>"
+ " </td>"
+ " <td>" + name + "</td>"
+ " <td></td>"
+ " </tr>"
+ "</table>");
out.print("<script language='javascript' >");
out.print("$(document).ready(function(){"
+ "var "+ id +" = new Jbutton(document.getElementById('" + sid + "'));"
+ id + ".conf = {key: '" + key + "'};" + this.bodyContent.getString()
+ id + ".init();" + "});");
out.print("</script>");
} catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChkAccess() {
return chkAccess;
}
public void setChkAccess(boolean chkAccess) {
this.chkAccess = chkAccess;
}
}