您的位置:首页 > 其它

标签开案例

2014-02-19 10:53 197 查看
•开发防盗链标签: (控制整个jsp页面是否执行)

package cn.gbx.web.example;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.xml.ws.RespectBinding;

public class RefererTag extends SimpleTagSupport {
private String site;
private String page;

public void setSite(String site) {
this.site = site;
}

public void setPage(String page) {
this.page = page;
}

@Override
public void doTag() throws JspException, IOException {

//1. 得到用户请求的referer
PageContext pc = (PageContext) this.getJspContext();
HttpServletRequest request = (HttpServletRequest)pc.getRequest();
String referer = request.getHeader("referer");
HttpServletResponse response = (HttpServletResponse)pc.getResponse();

//2. 检查盗链情况
// 是盗链者
if (referer == null || !referer.startsWith(site)) {
if (page.startsWith(request.getContextPath())) {
response.sendRedirect(page);
} else if (page.startsWith("/")) {
response.sendRedirect(request.getContextPath() + page);
} else {
response.sendRedirect(request.getContextPath() + "/" + page);
}
throw new SkipPageException();  // 后的页面不执行
} else {  // 非盗链者

}
//System.out.println(request.getContextPath());
}
}

<tag>
<name>referer</name>
<tag-class>cn.gbx.web.example.RefererTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>


  

•开发<c:if>标签 (控制标签体内容是都执行)

package cn.gbx.web.example;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class IfTag extends SimpleTagSupport {
private boolean test;

public void setTest(boolean test) {
this.test = test;
}

@Override
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();
if (test) {
jf.invoke(null);
} else {

}
}

}

<tag>
<name>if</name>
<tag-class>cn.gbx.web.example.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>


  

•开发<c:if><c:else>标签 (涉及父标签的使用)

package cn.gbx.web.example;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ChooseTag extends SimpleTagSupport {
//子标签共享的变量
private boolean isOver;

public boolean isOver() {
return isOver;
}

public void setOver(boolean isOver) {
this.isOver = isOver;
}

@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}

}


  

package cn.gbx.web.example;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class WhenTag extends SimpleTagSupport {
private boolean test;

public void setTest(boolean test) {
this.test = test;
}

@Override
public void doTag() throws JspException, IOException {
ChooseTag ch = (ChooseTag)this.getParent();
if (test && !ch.isOver()) { //子标签是真, 共享的父标签的变量还未使用
JspFragment jf = this.getJspBody();
jf.invoke(null);
ch.setOver(true);
} else {

}
}

}


  

package cn.gbx.web.example;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class OtherWiseTag extends SimpleTagSupport {

@Override
public void doTag() throws JspException, IOException {
ChooseTag ch = (ChooseTag) this.getParent();
if (!ch.isOver()) {
JspFragment jf = this.getJspBody();
jf.invoke(null);
ch.setOver(true);
} else {

}
}

}


  

<tag>
<name>choose</name>
<tag-class>cn.gbx.web.example.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>cn.gbx.web.example.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>cn.gbx.web.example.OtherWiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>


  

•开发迭代标签 (控制标签内的内容重复执行)

package cn.gbx.web.example;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ForeachTag extends SimpleTagSupport {
private Object items;
private String var;
private Collection collection;

public void setVar(String var) {
this.var = var;
}
public void setItems(Object items) {
this.items = items;
//集合
if (items instanceof Collection) {
collection = (Collection)items;
}
//Map
if (items instanceof Map) {
collection = ((Map)items).entrySet();
}
//数组的判断
if (items.getClass().isArray()) {
this.collection = new ArrayList();
int length = Array.getLength(items);
for (int i = 0; i < length; ++i) {
this.collection.add(Array.get(items, i));
}
}
}

//遍历
@Override
public void doTag() throws JspException, IOException {
Iterator it = this.collection.iterator();
while (it.hasNext()) {
Object value = it.next();
this.getJspContext().setAttribute(var, value);
this.getJspBody().invoke(null);
}
}
}


  

<tag>
<name>foreach</name>
<tag-class>cn.gbx.web.example.ForeachTag</tag-class>
<body-content>scriptless</body-content>

<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>fase</rtexprvalue>
</attribute>

<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

</tag>


  

•开发html转义标签

package cn.gbx.web.example;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class HtmlFilterTag extends SimpleTagSupport {

//转义标签内的内容
@Override
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();
StringWriter sw = new StringWriter();
jf.invoke(sw);
String content = sw.toString();
content = filter(content);
this.getJspContext().getOut().write(content);

}
//对标签体内的内容进行转义
public  String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuilder result = new StringBuilder(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}


  

<tag>
<name>filter</name>
<tag-class>cn.gbx.web.example.HtmlFilterTag</tag-class>
<body-content>scriptless</body-content>
</tag>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: