自定义标签-自定义注解
Posted 煮海焚天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义标签-自定义注解相关的知识,希望对你有一定的参考价值。
首先是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<Class<?>> classes=(List<Class<?>>)pageContext.getAttribute("allTag");
if(classes==null){
classes = new ArrayList<Class<?>>();
}
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("<input ");
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‘ onclick=\"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("onclick=‘" + 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<Class<?>> classes = (List<Class<?>>) pageContext.getAttribute("allTag");
JspWriter out = this.pageContext.getOut();
LinkedList<String> 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<String, String> 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<String> 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 = "<link rel=‘stylesheet‘ href=‘" +basePath+ path + "‘ type=‘text/css‘ />";
pathListLink.add(lable);
}
if (type == StaticResourceType.JS) {
String lable = "<script type=‘text/javascript‘ src=‘" +basePath+ path + "‘></script>";
pathListLink.add(lable);
}
if (type == StaticResourceType.CSS) {
String lable = "<link rel=‘stylesheet‘ href=‘" +basePath+ path + "‘ type=‘text/css‘ />";
pathListLink.add(lable);
}
}
}
public void addStyleLast(StaticResourceType type,LinkedList<String> 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 = "<link rel=‘stylesheet‘ href=‘"+basePath+path+ "‘ type=‘text/css‘ />";
pathListLink.addLast(lable);
}
if (type == StaticResourceType.JS) {
String lable = "<script type=‘text/javascript‘ src=‘"+basePath+ path+ "‘></script>";
pathListLink.addLast(lable);
}
if (type == StaticResourceType.CSS) {
String lable = "<link rel=‘stylesheet‘ href=‘"+basePath+path+ "‘ type=‘text/css‘ />";
pathListLink.addLast(lable);
}
}
}
}
声明tld文件(WEB-INF目录下)
<?xml version="1.0" encoding="UTF-8"?>
<taglib 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-jsptaglibrary_2_1.xsd"
version="2.1">
<description>
标签库信息描述
</description>
<tlib-version>1.0</tlib-version>
<short-name>axx</short-name><!-- 指定标签库默认的前缀名 -->
<uri>/mytaglib</uri><!-- 定标签库唯一访问表示符 -->
<!--Resource-->
<tag>
<!--指定标签名-->
<name>resource</name>
<!--指定标签类文件的全路径-->
<tag-class>com.aixuexi.tag.model.ResourceTag</tag-class>
<body-content>empty</body-content><!-- 定标签间的主体(body)内容形式 -->
</tag>
<!--button标签-->
<tag>
<name>button</name>
<tag-class>com.aixuexi.tag.model.ButtonTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>btntype</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>parames</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>htmlClass</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>disabled</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>style</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>target</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>onclick</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
web.xml中配置tld
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>mytaglib</taglib-uri>
<taglib-location>/WEB-INF/aixuexi.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
JSP中页面引用自定义标签
1.<%@ taglib uri="/mytaglib" prefix="axx"%>
2.<axx:button btntype="" value="我是自定义button" disabled="" htmlClass="asdfasdf" ></axx:button>
3.<axx:resource />
以上是关于自定义标签-自定义注解的主要内容,如果未能解决你的问题,请参考以下文章
Spring MVC 自定义标签如何使用@Autowired自动装配注解