• 第八部分_客户化JSP标签


     EL语言(减少JSP页面中的Java代码)

    <body>
        <%String username = request.getParameter("username");
          String password = request.getParameter("password");
         %>
         username: <% out.print(username); %><br>
         password: <% out.print(password); %><br>
         
         username: ${param.username }<br> <%用EL达到和上面四行代码同样的功能 %>
         password: ${param.password }<br>
      
      </body>

    属性范围->在EL中的名称

    Page->pageScope;Request->requestScope;Session->sessionScope;Application->applicationScope.

    此外,启动tomcat服务器,在浏览器中访问localhost:8080/examples,选择JSP Examples,其中给出了若干实例,也可以进行相关的学习。

    首先创建标签处理类HelloTag.java:

    package com.jsp.tag;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspTagException;
    import javax.servlet.jsp.tagext.TagSupport;
    
    public class HelloTag extends TagSupport
    {
    	public HelloTag()
    	{
    		
    	}
    
    
    	public int doStartTag() throws JspException
    	{
    		try
    		{
    			this.pageContext.getOut().print("nihao");
    		}
    		catch (Exception ex)
    		{
    			throw new JspTagException(ex.getMessage());
    		}
    		return EVAL_BODY_INCLUDE;
    	}
    	
    	// Method called when the closing hello tag is encountered
    	public int doEndTag() throws JspException
    	{
    		try
    		{
    			// We use the pageContext to get a Writer
    			// We then print the text string Hello
    			this.pageContext.getOut().print("Hello");
    		}
    		catch (Exception e)
    		{
    
    			throw new JspTagException(e.getMessage());
    		}
    		return EVAL_PAGE;
    	}
    
    	public void release()
    	{
    
    		// Call the parent's release to release any resources
    		// used by the parent tag.
    		// This is just good practice for when you start creating
    		// hierarchies of tags.
    		super.release();
    	}
    }
    

    然后,创建标签库描述文件(在WEB-INF下新建一个tld(taglib description )文件,这里命名为mytaglib.tld):

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE taglib
            PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
    	"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
    
    <!-- a tag library descriptor -->
    
    <taglib>
    	<tlibversion>1.0</tlibversion>
    	<jspversion>1.1</jspversion>
    	<shortname>mytaglib</shortname>
    	<uri>/mytaglib</uri>
    
    	<tag>
    		<name>hello</name>
    		<tagclass>com.jsp.tag.HelloTag</tagclass>
    		<bodycontent>empty</bodycontent>
    	</tag>
    	
    	
    	
    </taglib>
    

    然后,在hellowithtag1.jsp中引入标签库,然后插入标签:

    <%@ taglib uri="/mytaglib" prefix="mm" %>
    
    <html> 
    <head>
      <title>tag library example</title>
    </head>
    <body>
      <b><mm:hello/></b>
    </body>  
    </html>        
    

    访问http://localhost:8080/test/hellowithtag1.jsp,输出nihaoHello

    范例2:

    创建一个能替换test应用中JSP网页的静态文本的标签,这个标签名为message,它放在mytaglib标签库中。

    首先在WEB-INF下面放置一个静态文本messageresource.properties

    hello.title = Tile of hello.jsp
    hello.hello = Hello
    

    然后通过一个DispatcherServlet装载:

    package com.test.servlet;
    
    import java.io.InputStream;
    import java.util.Properties;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    
    public class DispatcherServlet extends HttpServlet
    {
    
    	public void init(ServletConfig config) throws ServletException
    	{
    		super.init(config);
    
    		Properties ps = new Properties();
    		
    		try
    		{
    			ServletContext context = config.getServletContext();
    			InputStream in = context
    					.getResourceAsStream("/WEB-INF/messageresource.properties");
    			ps.load(in);
    
    			in.close();
    
    			context.setAttribute("ps", ps);
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    
    	public void destroy()
    	{
    	}
    }
    

    接下来,是标签库的处理类MessageTag.java:

    package com.jsp.tag;
    
    import java.util.Properties;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspTagException;
    import javax.servlet.jsp.PageContext;
    import javax.servlet.jsp.tagext.TagSupport;
    
    public class MessageTag extends TagSupport
    {
        private String key = null;
    
        public MessageTag()
        {
        }
    
        public String getKey()
        {
            return this.key;
        }
    
        public void setKey(String key)
        {
            this.key = key;
        }
    
        // Method called when the closing hello tag is encountered
        public int doEndTag() throws JspException
        {
            try
            {
                Properties ps = (Properties) pageContext.getAttribute("ps",
                        PageContext.APPLICATION_SCOPE);
                String message = (String) ps.get(key);
                pageContext.getOut().print(message);
            }
            catch (Exception e)
            {
                throw new JspTagException(e.getMessage());
            }
    
            return EVAL_PAGE;
        }
    
        public void release()
        {
            super.release();
        }
    
    }
    

    添加相关信息到标签库描述文件mytaglib.tld中:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE taglib
            PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
    	"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
    
    <!-- a tag library descriptor -->
    
    <taglib>
    	<tlibversion>1.0</tlibversion>
    	<jspversion>1.1</jspversion>
    	<shortname>mytaglib</shortname>
    	<uri>/mytaglib</uri>
    
    	<tag>
    		<name>hello</name>
    		<tagclass>com.jsp.tag.HelloTag</tagclass>
    		<bodycontent>empty</bodycontent>
    	</tag>
    	
    	<tag>
    		<name>message</name>
    		<tagclass>com.jsp.tag.MessageTag</tagclass>
    		<bodycontent>empty</bodycontent>
    		<attribute>
    			<name>key</name>
    			<required>true</required>
    		</attribute>
    	</tag>
    	
    </taglib>
    

    最后,在hellowithtag2.jsp文件中引入标签库,然后插入标签:

    <%@ page contentType="text/html; charset=GB2312" %>
    <%@ taglib uri="/mytaglib" prefix="mm" %>
    <html>
    <head>
      <title><mm:message key="hello.title"/></title>
    </head>
    <body>
    
    <b><mm:message key="hello.hello"/></b>
    </body>
    </html>
      
    

    为了在web应用启动时通过DispatcherServlet装载静态文本,应该在web.xml中配置这个Servlet时设置load-on-startup属性:

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>com.test.servlet.DispatcherServlet</servlet-class>
      	<load-on-startup>5</load-on-startup>
      </servlet>
    

    在MessageTag的doEndTag方法中,首先从pageContext中读取包含静态文本的Properties对象:

     public int doEndTag() throws JspException
        {
            try
            {
                Properties ps = (Properties) pageContext.getAttribute("ps",
                        PageContext.APPLICATION_SCOPE);
                String message = (String) ps.get(key);
                pageContext.getOut().print(message);
            }
            catch (Exception e)
            {
                throw new JspTagException(e.getMessage());
            }
    
            return EVAL_PAGE;
        }
    

    然后从Properties对象中读取key对应的静态文本,最后输出该文本。

    最后,访问http://localhost:8080/test/hellowithtag2.jsp,输出hello。

  • 相关阅读:
    IndexedDB demo showcase
    javascript for
    IndexedDB
    web sql Database
    webSql
    哈哈 代表月亮
    网易
    Android 百度地图 SDK v3.0.0 (一)
    iOS使用Instrument的Leaks查找代码内存泄露
    换主页轮播的主题图片(4、删除)---轻开电子商务系统(企业入门级B2C站点)
  • 原文地址:https://www.cnblogs.com/Code-Rush/p/4639192.html
Copyright © 2020-2023  润新知