首先是4个自定义注解类
StaticResourceType.java
public enum StaticResourceType {
LESS,CSS,JS
}
StaticResource.java
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface StaticResource {
StaticResourceType type();
String path();
DependencyResource[] dependencies() default {};
}
StaticResources.java
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface StaticResources {
StaticResource[] value();
}
DependencyResource.java
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface DependencyResource {
StaticResourceType type();
String path();
}
所有标签的父类
BaseProperty.java
public class BaseProperty extends TagSupport{
// 按钮的id
protected String id;
// 按钮的name
protected String name;
// 按钮的class
protected String htmlClass;
// 按钮的样式
protected String style;
// 按钮上面的名字
protected String href;
protected String type;
// 按钮不可用
protected String disabled;
// 传进来的参数,是link就传url进来。是action 就传action进来。
protected String parames;
// 按钮上面的名字
protected String value;
// 打开页面的类型
protected String target;
// 类型非为三种 link submit button reset
protected String btntype;
// 事件
protected String onclick;
protected String ondblclick;
protected String onmouseover;
protected String onmouseout;
protected String onmousedown;
protected String onmouseup;
protected String onmousemove;
protected String onkeydown;
protected String onkeyup;
protected String onkeypress;
protected String onfocus;
protected String onblur;
protected String onchange;
protected String onselect;
@Override
public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext;
List> classes=(List>)pageContext.getAttribute("allTag");
if(classes==null){
classes = new ArrayList>();
}
classes.add(this.getClass());
pageContext.setAttribute("allTag", classes);
}
}
自定义的button标签
ButtonTag.java
@StaticResources(
{
@StaticResource(path = "resources/css/index3.less", type = StaticResourceType.LESS, dependencies = {
@DependencyResource(path="resources/css/7.js",type = StaticResourceType.JS),
@DependencyResource(path="resources/css/8.js",type = StaticResourceType.JS),
@DependencyResource(path="resources/css/9.js",type = StaticResourceType.CSS)
}),@StaticResource(path = "resources/css/index4.less", type = StaticResourceType.LESS, dependencies = {
@DependencyResource(path="resources/css/10.js",type = StaticResourceType.JS),
@DependencyResource(path="resources/css/11.js",type = StaticResourceType.JS),
@DependencyResource(path="resources/css/12.js",type = StaticResourceType.CSS)
})
}
)
public class ButtonTag extends BaseProperty {
@Override
public int doStartTag() throws JspException {
StringBuilder builder = new StringBuilder();
builder.append("
if ((btntype != null) && (btntype.equals("submit"))) {
builder.append("type='submit' ");
} else if ((btntype != null) && (btntype.equals("button"))) {
builder.append("type='button' ");
} else if ((btntype != null) && (btntype.equals("reset"))) {
builder.append("type='reset' ");
} else {
if (StringUtil.isEmpty(target)) {
target = "_self";
}
builder.append("type='button' οnclick=\"javascript:window.open(\'" + parames + "\',\'" + target + "\');\"");
}
builder.append("value ='" + value + "' ");
if (!StringUtil.isEmpty(id)) {
builder.append("id='" + id + "' ");
}
if (!StringUtil.isEmpty(name)) {
builder.append("name='" + name + "' ");
}
if (!StringUtil.isEmpty(disabled)) {
if (!disabled.equals("false")) {
builder.append("disabled='" + disabled + "' ");
}
}
if (!StringUtil.isEmpty(onclick)) {
builder.append("οnclick='" + onclick + "' ");
}
if (!StringUtil.isEmpty(style)) {
builder.append("style ='" + style + "' ");
}else{
if (!StringUtil.isEmpty(htmlClass)) {
builder.append("class='" + htmlClass + "' ");
} else {
builder.append("class ='selectBtn'");
}
}
builder.append(" />");
try {
pageContext.getOut().write(builder.toString());
} catch (Throwable cause) {
throw new RuntimeException("自定义按钮标签出错", cause);
}
return SKIP_BODY;
}
}
ResourceTag标签用于结束,并输入依赖的js或者css
public class ResourceTag extends TagSupport{
@Override
public int doStartTag() throws JspException {
try {
ServletRequest request=pageContext.getRequest();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
List> classes = (List>) pageContext.getAttribute("allTag");
JspWriter out = this.pageContext.getOut();
LinkedList pathListLink=new LinkedList<>();
for (Class> clazz : classes) {
StaticResources annotation = clazz.getAnnotation(StaticResources.class);
StaticResource[] staticResourceArry=annotation.value();
for(StaticResource staticResource:staticResourceArry){
String path=staticResource.path();
StaticResourceType staticResourceType=staticResource.type();
DependencyResource[] dependencyResourcesArry=staticResource.dependencies();
for(DependencyResource dependencyResource :dependencyResourcesArry){
StaticResourceType depType=dependencyResource.type();
String depPath=dependencyResource.path();
addStyle(depType,pathListLink,depPath,basePath);
}
addStyleLast(staticResourceType,pathListLink,path,basePath);
}
}
if (null != pathListLink && pathListLink.size() > 0) {
StringBuffer buffer = new StringBuffer();
/*for(Map.Entry entry : treeMap.entrySet()) {
System.out.println(entry.getKey());
buffer.append(entry.getValue());
}*/
for(String str:pathListLink){
buffer.append(str);
}
out.println(buffer.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
public void addStyle(StaticResourceType type,LinkedList pathListLink,String path,String basePath){
boolean isContains=false;
for(String str:pathListLink){
if (str.contains(path)){
isContains=true;
}
}
String currentPath2=getClass().getResource("/").getFile().toString();
if (!isContains) {
if (type == StaticResourceType.LESS) {
String lable = "";
pathListLink.add(lable);
}
if (type == StaticResourceType.JS) {
String lable = "";
pathListLink.add(lable);
}
if (type == StaticResourceType.CSS) {
String lable = "";
pathListLink.add(lable);
}
}
}
public void addStyleLast(StaticResourceType type,LinkedList pathListLink,String path,String basePath){
boolean isContains=false;
for(String str:pathListLink){
if (str.contains(path)){
isContains=true;
}
}
if (!isContains) {
if (type == StaticResourceType.LESS) {
String lable = "";
pathListLink.addLast(lable);
}
if (type == StaticResourceType.JS) {
String lable = "";
pathListLink.addLast(lable);
}
if (type == StaticResourceType.CSS) {
String lable = "";
pathListLink.addLast(lable);
}
}
}
}
声明tld文件(WEB-INF目录下)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
标签库信息描述
1.0
axx
/mytaglib
resource
com.aixuexi.tag.model.ResourceTag
empty
button
com.aixuexi.tag.model.ButtonTag
JSP
id
true
name
true
btntype
true
true
parames
true
htmlClass
true
disabled
true
style
true
value
true
true
target
true
onclick
true
web.xml中配置tld
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
/index.jsp
mytaglib
/WEB-INF/aixuexi.tld
JSP中页面引用自定义标签
1.
2.
3.