• Servlet------>jsp自定义标签1(简单入门)


    自定义标签能做什么:

    1.移除java代码

    2.控制jsp页面某一部分是否执行

    3.控制整个jsp是否执行

    3.jsp内容重复输出

    4.修改jsp内容输出

    效果:

    首先先写好实现这个标签的java类,这个类要继承tagsupput方法

    来看下api:所以我们重写doStartTag就行

    下面是我写好的java类:

     TagDemo1.java

    public class TagDemo1 extends TagSupport {
    
        @Override
        public int doStartTag() throws JspException {
            //通过pagecontext对象获取HttpServletRequest对象
            HttpServletRequest request=(HttpServletRequest) this.pageContext.getRequest();
            //获取JspWriter对象
            JspWriter out=this.pageContext.getOut();
            //获取当前ip,因为我是mac所以ip有点怪
            String ip=request.getRemoteAddr();
            try {
                out.print(ip);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return super.doStartTag();
        }
    }
    

    TagDemo2.java 

    public class TagDemo2 extends TagSupport {
    
    	@Override
    	public int doStartTag() throws JspException {
    		
    		HttpServletRequest request=(HttpServletRequest) this.pageContext.getRequest();
    		JspWriter out=this.pageContext.getOut();
    		Date date=new Date();
    		String time=date.toLocaleString();
    		try {
    			out.print(time);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return super.doStartTag();
    	}
    }
    

    写好java类之后,我们来把他绑定到一个uri上:在tag.tld中

    这个的格式tomcat中有例子,位置如下:

    tag.tld:

     tag.tld

    <?xml version="1.0" encoding="UTF-8" ?>
    <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
        <description>A tag library exercising SimpleTag handlers.</description>
        <tlib-version>1.0</tlib-version>
        <short-name>SHOW</short-name>
        <uri>snowing.com</uri>
        <tag>
            <name>SHOW</name>
            <tag-class>tag.TagDemo1</tag-class>
            <body-content>empty</body-content>
        </tag>
            <tag>
            <name>SH</name>
            <tag-class>tag.TagDemo2</tag-class>
            <body-content>empty</body-content>
        </tag>
     </taglib>
    

    绑定后:在jsp页面引用

    tag.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@ taglib uri="/WEB-INF/tag.tld" prefix="snowing"%>
        <%@ page import="java.util.Date"%> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>使用标签控制内容石否输出</title>
    </head>
    <body>
    您的ip是:<snowing:SHOW/></br>
    您的现在的时间是:<snowing:SH/>
    </body>
    </html>
    

    编译后的源码:

     切记:!引入的/day06/WebContent/WEB-INF/lib/jsp-api.jar要和tomcat中的版本一致

  • 相关阅读:
    [ActionScript3.0] 运用JPEGEncoderOptions或者PNGEncoderOptions保存图片到本地
    [AIR] 在 Adobe AIR 中为不同屏幕尺寸的多种设备提供支持
    [ActionScript 3.0] flash如何访问父级或者舞台上的变量、函数等的方法
    [ActionScript 3.0] 自定义顶级类
    [ActionScript&Flex] FlashBuilder编译条件之如何屏蔽调试代码
    [ActionScript 3.0] LocalConnection示例
    [ActionScritp 3.0] 使用LocalConnection建立通信
    [AIR] Screen 的应用
    [ActionScript 3.0] AS3 对XML的操作,创建、删除、增加节点方法
    [ActionScript 3.0] AS3.0 水面波纹效果
  • 原文地址:https://www.cnblogs.com/SnowingYXY/p/6713694.html
Copyright © 2020-2023  润新知