您的位置:首页 > Web前端 > JavaScript

JSP简单标签的总结及案例

2015-07-04 23:04 537 查看
一、控制页面部分内容是否输出

1.控制页面部分内容输出

public void doTag() throws JspException,IOException{
JspFragment jf=this.getJspBody();
jf.invokee(null);//或jf.invoke(this.getJspContext().getOut());
}


2.控制页面部分内容不输出

public void doTag() throws JspException,IOException{
}


二、控制标签内容重复输出

public void doTag() throws JspException,IOException{
JspFragment jf=this.getJspBody();
for(int i=0;i<5;i++){
jf.invoke(null);
}
}


三、修改标签体内容

pubilc void doTag() throws JspException,IOException{
JspFragment jf=this.getJspBody();
StringWriter sw=new StringWriter();
jf.invoke(sw);
String content=sw.toString();
content=content.toUpperCase();
this.getJspContext().getOut().write(content);
}


四、控制整个页面是否输出

public void doTag() throws JspException,IOException{
throw new SkipPageException();
}


【开发带属性的标签】

<sitcast:demo count="5">
</sitcast:demo>


1.在标签处理器编写每个属性对应的setter方法

public class SimpleTagDemo extends SimpleTagSupport{
private int count;

public void setCount(int count){
this.count=count;
}

public void doTag() throws JspException,IOException{
JspFragment jf=this.getJspBody();
for(int i=0;i<count;i++){
jf.invoke(null);
}
}
}


2.在TLD文件中描述标签的属性

<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>//是否支持El和脚本表达式
</attribute>


注意:属性的值只支持八种基本类型的转换,若是其他的类型,需要用到表达式

1、开发反盗链标签

使用标签:
<fix:referer site="http://localhost" page="/index.jsp"></fix:referer>

代码:
package cn.itcast.web.tag.eaxmple;

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;

public class RefererTag extends SimpleTagSupport {
private String page;//要跳转的页面
private String site;//针对自家的哪个网站防盗链
public void setPage(String page) {
this.page = page;
}
public void setSite(String site) {
this.site = site;
}
@Override
public void doTag() throws JspException, IOException {
PageContext pageContext=(PageContext) this.getJspContext();
HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
HttpServletResponse response= (HttpServletResponse) pageContext.getResponse();
String referer=request.getHeader("referer");

if(referer==null || !referer.startsWith(site)){
String cp=request.getContextPath();
if(page.startsWith(cp))
{   response.sendRedirect(page);
}else if (page.startsWith("/")) {
response.sendRedirect(cp+page);
}else
{
response.sendRedirect(cp+"/"+page);
}
throw new SkipPageException();
}
else
super.doTag();
}

}


2、开发for标签

使用标签:
<fix:if test="${user==null }">
未登陆. <br>
</fix:if>

代码:
public class IfTag extends SimpleTagSupport{
private boolean test;
public void setTest(boolean test){
this.test=test;
}

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


3、开发if else标签

<fix:choose>
<fix:when test="${user==null }">
未登陆. <br>
</fix:when>
<fix:otherwith>
welcome用户已经登录. <br>
</fix:otherwith>
</fix:choose>

public class Choose extends SimpleTagSupport {
private boolean isDo;
public boolean isDo() {
return isDo;
}
public void setDo(boolean isDo) {
this.isDo = isDo;
}
@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}
}
public class WhenTag extends SimpleTagSupport {

private boolean test;

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

@Override
public void doTag() throws JspException, IOException {
Choose parent=(Choose) this.getParent();
if(test && !parent.isDo())
{
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}

public class OtherwithTag extends SimpleTagSupport {

@Override
public void doTag() throws JspException, IOException {
Choose parent=(Choose) this.getParent();
if(!parent.isDo())
{
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}


4、开发foreach标签

使用标签:
<fix:choose>
<fix:when test="${user==null }">
未登陆. <br>
</fix:when>
<fix:otherwith>
welcome用户已经登录. <br>
</fix:otherwith>
</fix:choose>

代码:
public class Choose extends SimpleTagSupport {
private boolean isDo;
public boolean isDo() {
return isDo;
}
public void setDo(boolean isDo) {
this.isDo = isDo;
}
@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}
}
public class WhenTag extends SimpleTagSupport {

private boolean test;

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

@Override
public void doTag() throws JspException, IOException {
Choose parent=(Choose) this.getParent();
if(test && !parent.isDo())
{
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}

public class OtherwithTag extends SimpleTagSupport {

@Override
public void doTag() throws JspException, IOException {
Choose parent=(Choose) this.getParent();
if(!parent.isDo())
{
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}


5、开发html转义标签

使用标签:
<fix:htmlFilter>
<a href="a  发 a a">超链接的写法</a>
</fix:htmlFilter>

代码:
package cn.itcast.web.tag.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 HtmlFilter extends SimpleTagSupport {

@Override
public void doTag() throws JspException, IOException {
JspFragment jf=this.getJspBody();
StringWriter sw=new StringWriter();
jf.invoke(sw);
String content=sw.getBuffer().toString();
content=filter(content);
this.getJspContext().getOut().write(content);
}
/*C:\\Tomcat 7.0\\webapps\\examples\\WEB-INF\\classes\\util\\HTMLFilter.java*/
public static 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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp