自定义标签:移除jsp页面的Java代码
1步骤:--1.编写一个实现了Tag接口的java类(标签处理器类)
-2.编写标签库描述符(tld)文件,在tld文件中对标签处理器列进行描述
2.快速入门:使用标签输出客户机的IP地址
步骤:2.1建一个实现了Tag接口的类,在类中实现Java代码
2.2在WEB-INF目录下建一个以tld为后缀的文件,文件中描述
2.3在jsp页面导入标签--tag,然后使用即消除掉了Java代码
<%@ taglib uri="/luo" prefix="luo"%>
当前时间是:<luo:TagDemo1/>
3.控制jsp页面的逻辑(TagSupport)--控制是否执行,是否执行一部分,是否重复执行,修改内容执行
3.1控制标签体执行-doStartTag()方法中返回Tag.EVAL_BODY_INCLUDE/不执行Tag.SKIP_BODY
3.2控制余下的页面执行-doEndTag()方法,同上
3.3控制页面内容重复执行(IterationTag-TagSupport)
- doStartTag()方法返回Tag.EVAL_BODY_INCLUDE;
-doAfterBody()方法返回IterationTag.EVAL_BODY_AGAIN;
3.4修改后输出(BodyTag--BodyTagSupport)--doEndTag()方法中控制
4.简单标签(SimpleTag-SimpleTagSupport==3个传统标签接口):
4.0只提供了一个简单的doTag()方法 ;标签体被jspFragment封装了
4.1用简单标签控制标签体是否执行jf.invoke(null)默认写给浏览器;标签体类型scriptless
JspFragment jsf=this.getJspBody();
PageContext context=(PageContext) this.getJspContext();
jsf.invoke(context.getOut());
4.2控制重复执行
4.3修改后输出StringWriter sw--jf.invoke(sw)
4.4控制余下页面不执行--throw new SkipPageException()抛异常就不执行
throw new SkipPageException();
5.开发带属性的标签:通过属性控制标签
5.1在tld中需要描述属性
5.2属性值支持八大基本类型可以自动转换;复杂类型-使用el表达式
6.案例:1-防盗链<itcast:referer site="http://localhost:8080/" page="/index.jsp">
2-开发I<c:if test="">标签/开发</c:if></c:else>--使用父标签
3-开发迭代标签<c:forEach > 判断类型instanceof--map;list;对象数组;基本数据数组
4-开发HTML转义标签<c:htmlfilter>使取出的代码原样输出
5-打包标签库:将开发好的标签打包,便宜别人使用
将Java打包成exe文件:1-把工程做成可运行的jar;
2-在Tomcat中可运行的jar;找到可运行的主类
3-为jar生成exe--exe4j软件
1步骤:--1.编写一个实现了Tag接口的java类(标签处理器类)
-2.编写标签库描述符(tld)文件,在tld文件中对标签处理器列进行描述
2.快速入门:使用标签输出客户机的IP地址
步骤:2.1建一个实现了Tag接口的类,在类中实现Java代码
public int doStartTag() throws JspException {
Date date=new Date();
JspWriter out=pagecontext.getOut();
try {
out.write(date.toLocaleString());
} catch (IOException e) {
throw new RuntimeException(e);
}
return 0;
}
2.2在WEB-INF目录下建一个以tld为后缀的文件,文件中描述
<tag>
<name>TagDemo1</name>
<tag-class>com.luolailiang.tag.TagDemo1</tag-class>
<body-content>empty</body-content>
</tag>
2.3在jsp页面导入标签--tag,然后使用即消除掉了Java代码
<%@ taglib uri="/luo" prefix="luo"%>
当前时间是:<luo:TagDemo1/>
3.控制jsp页面的逻辑(TagSupport)--控制是否执行,是否执行一部分,是否重复执行,修改内容执行
3.1控制标签体执行-doStartTag()方法中返回Tag.EVAL_BODY_INCLUDE/不执行Tag.SKIP_BODY
3.2控制余下的页面执行-doEndTag()方法,同上
3.3控制页面内容重复执行(IterationTag-TagSupport)
- doStartTag()方法返回Tag.EVAL_BODY_INCLUDE;
-doAfterBody()方法返回IterationTag.EVAL_BODY_AGAIN;
3.4修改后输出(BodyTag--BodyTagSupport)--doEndTag()方法中控制
String content=this.getBodyContent().getString();
String result=content.toUpperCase();
try {
this.pageContext.getOut().write(result);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Tag.EVAL_PAGE;
4.简单标签(SimpleTag-SimpleTagSupport==3个传统标签接口):
4.0只提供了一个简单的doTag()方法 ;标签体被jspFragment封装了
4.1用简单标签控制标签体是否执行jf.invoke(null)默认写给浏览器;标签体类型scriptless
JspFragment jsf=this.getJspBody();
PageContext context=(PageContext) this.getJspContext();
jsf.invoke(context.getOut());
4.2控制重复执行
JspFragment jf= this.getJspBody();
for(int i=0;i<4;i++){
jf.invoke(null);
}
4.3修改后输出StringWriter sw--jf.invoke(sw)
JspFragment jf=this.getJspBody();
StringWriter sw=new StringWriter();
jf.invoke(sw);
String content=sw.getBuffer().toString().toUpperCase();
PageContext context=(PageContext) this.getJspContext();
context.getOut().write(content);
4.4控制余下页面不执行--throw new SkipPageException()抛异常就不执行
throw new SkipPageException();
5.开发带属性的标签:通过属性控制标签
5.1在tld中需要描述属性
private int count;
public void setCount(int count) {
this.count = count;
}
public void doTag() throws JspException, IOException {
or(int i=0;i<count;i++){
this.getJspBody().invoke(null);
}
}
//<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
5.2属性值支持八大基本类型可以自动转换;复杂类型-使用el表达式
6.案例:1-防盗链<itcast:referer site="http://localhost:8080/" page="/index.jsp">
//查看访问者从那个页面来的
PageContext context=(PageContext) this.getJspContext();
HttpServletRequest request=(HttpServletRequest) context.getRequest();
String referer=request.getHeader("referer");
//判断
if(referer==null||!referer.startsWith(site)){
HttpServletResponse response=(HttpServletResponse) context.getResponse();
String webroot=request.getContextPath();
if(page.startsWith(webroot)){
response.sendRedirect(page);
}else{
response.sendRedirect(webroot+page);
}
throw new SkipPageException();
}
2-开发I<c:if test="">标签/开发</c:if></c:else>--使用父标签
3-开发迭代标签<c:forEach > 判断类型instanceof--map;list;对象数组;基本数据数组
4-开发HTML转义标签<c:htmlfilter>使取出的代码原样输出
5-打包标签库:将开发好的标签打包,便宜别人使用
将Java打包成exe文件:1-把工程做成可运行的jar;
2-在Tomcat中可运行的jar;找到可运行的主类
3-为jar生成exe--exe4j软件