JavaWeb同步学习笔记之五十九、JavaWeb_带属性的自定义标签

JavaWeb_带属性的自定义标签

    • 带属性的自定义标签

带属性的自定义标签

  • 1.setJspContext:一定会被JSP引擎所调用,先于doTag,把代表 JSP 引擎的 pageContext 传给标签处理器类。
    HelloSimpleTag.java
	private PageContext pageContext;
	@Override
	public void setJspContext(JspContext arg0) {
		System.out.println(arg0 instanceof PageContext);
		this.pageContext = (PageContext)arg0;
	}
  • 2.带属性的自定义标签:
     1)先在标签处理器类中定义 setter 方法。建议把所有的属性类型都设置为 String 类型。
    HelloSimpleTag.java
	private String value;
	private String count;
	
	/**  
	 * @param value: the value to set
	 */
	public void setValue(String value) {
		this.value = value;
	}
	
	/**  
	 * @param count: the count to set
	 */
	public void setCount(String count) {
		this.count = count;
	}

.
   2)在 tld 描述文件中来描述属性:

		
		<attribute>
			
			<name>valuename>
			
			<required>truerequired>
			
			<rtexprvalue>truertexprvalue>
		attribute>
		<attribute>
			<name>countname>
			<required>falserequired>
			<rtexprvalue>falsertexprvalue>
		attribute>

.
   3)在页面中使用属性,属性名同 tld 文件中定义的名字。

<xs:hello value="${param.name }" count="10"/>
  • 3.通常情况下开发简单标签直接继承 SimpleSupport 就可以了。可以直接调用其对应的 getter 方法得到对应的 API 。
    SimpleSupport.java
public class SimpleTagSupport implements SimpleTag{
    
    public void doTag() 
        throws JspException, IOException{}
    
    private JspTag parentTag;
    
    public void setParent( JspTag parent ) {
        this.parentTag = parent;
    }
    
    public JspTag getParent() {
        return this.parentTag;
    }
    
    private JspContext jspContext;
    
    public void setJspContext( JspContext pc ) {
        this.jspContext = pc;
    }
    
    protected JspContext getJspContext() {
        return this.jspContext;
    }
    
    private JspFragment jspBody;
                
    public void setJspBody( JspFragment jspBody ) {
        this.jspBody = jspBody;
    }
    
    protected JspFragment getJspBody() {
        return this.jspBody;
    }   
}
  • 4.练习一:定制一个带有两个属性的标签< max>,用于计算并输出两个数的最大值。
    练习二:定制一个带有一个属性的标签< xs: readFile src=“”>,用于输出指定文件的内容。
    mytag.tld


<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>JSTL 1.1 core librarydescription>
	<display-name>JSTL coredisplay-name>
	<tlib-version>1.1tlib-version>

	
	<short-name>xsshort-name>
	
	<uri>http://www.xs.com/mytag/coreuri>

	<tag>
		<name>readerFilename>
		<tag-class>com.xs.javaweb.tag.ReadFileTagtag-class>
		<body-content>emptybody-content>
		<attribute>
			<name>srcname>
			<required>truerequired>
			<rtexprvalue>truertexprvalue>
		attribute>
	tag>

	
	<tag>
		<name>maxname>
		<tag-class>com.xs.javaweb.tag.MaxTagtag-class>
		<body-content>emptybody-content>
		<attribute>
			<name>num1name>
			<required>truerequired>
			<rtexprvalue>truertexprvalue>
		attribute>
		<attribute>
			<name>num2name>
			<required>truerequired>
			<rtexprvalue>truertexprvalue>
		attribute>
	tag>


	<tag>
		
		<name>helloname>

		
		<tag-class>com.xs.javaweb.tag.HelloSimpleTagtag-class>
		
		<body-content>emptybody-content>
		
		<attribute>
			
			<name>valuename>
			
			<required>truerequired>
			
			<rtexprvalue>truertexprvalue>
		attribute>
		<attribute>
			<name>countname>
			<required>falserequired>
			<rtexprvalue>falsertexprvalue>
		attribute>
	tag>
taglib>

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@taglib prefix="xs" uri="http://www.xs.com/mytag/core" %>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
	
	<xs:readerFile src="/WEB-INF/question.txt"/>
	
	<br>

	<xs:max num2="${param.a }" num1="${param.b }"/>
	
	<br>
	
	<xs:hello value="${param.name }" count="10"/>
	
body>
html>

MyTag.java

package com.xs.javaweb.tag;
import java.io.IOException;

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

/**  
 * All rights Reserved,Designed By XS
 * @Title: MaxTag.java
 * @Package 
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月14日 上午9:07:34
 * @version V1.0
 */

/**   
 * @ClassName: MaxTag
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月14日 上午9:07:34
 * @version V1.0
 */
public class MaxTag extends SimpleTagSupport {
	
	private String num1;
	private String num2;
	
	/**  
	 * @param num1: the num1 to set
	 */
	public void setNum1(String num1) {
		this.num1 = num1;
	}
	
	/**  
	 * @param num2: the num2 to set
	 */
	public void setNum2(String num2) {
		this.num2 = num2;
	}

	/**   
	 * 

Title: doTag

*

Description:

* @see javax.servlet.jsp.tagext.SimpleTag#doTag() * @throws JspException * @throws IOException */
@Override public void doTag() throws JspException, IOException { int a = 0; int b = 0; PageContext pageContext = (PageContext)getJspContext(); JspWriter out = pageContext.getOut(); try { a = Integer.parseInt(num1); b = Integer.parseInt(num2); out.print(a > b ? a :b); } catch (Exception e) { out.print("输入的属性格式不正确!"); } } }

ReadFileTag.java

/**  
 * All rights Reserved,Designed By XS
 * @Title: ReadFileTag.java
 * @Package com.xs.javaweb.tag
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月14日 上午10:04:48
 * @version V1.0
 */
package com.xs.javaweb.tag;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

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

/**   
 * @ClassName: ReadFileTag
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月14日 上午10:04:48
 * @version V1.0
 */
public class ReadFileTag extends SimpleTagSupport {
	
	//相对于当前WEB应用的根路径的文件名。
	private String src;
	
	/**  
	 * @param src: the src to set
	 */
	public void setSrc(String src) {
		this.src = src;
	}

	/**   
	 * 

Title: doTag

*

Description:

* @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag() * @throws JspException * @throws IOException */
@Override public void doTag() throws JspException, IOException { PageContext pageContext = (PageContext)getJspContext(); InputStream in = pageContext.getServletContext().getResourceAsStream(src); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String str = null; while ((str = reader.readLine()) != null) { str = Pattern.compile("<").matcher(str).replaceAll("<"); str = Pattern.compile(">").matcher(str).replaceAll(">"); pageContext.getOut().println(str); pageContext.getOut().println("
"
); } } }

你可能感兴趣的:(JavaWeb)