본문 바로가기

JSP

변수를 지원하는 커스텀 액션을 만드는 태그 클래스

NewMinimumTag.java 


package customaction;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspContext;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class NewMinimumTag extends SimpleTagSupport {

   

    private String var;

    private int num1, num2;

   

    public void setVar(String var) {

         this.var = var;

    }

    public void setNum1(int num1) {

         this.num1 = num1;

    }

    public void setNum2(int num2) {

         this.num2 = num2;

    }

    @Override

    public void doTag() throws JspException, IOException {

         // TODO Auto-generated method stub

         JspContext context = getJspContext();

         if (num1<num2)

             context.setAttribute(var, num1);

         else

             context.setAttribute(var, num2);

         return;

    }

   

}

 

 customaction.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">

   

    <tlib-version>1.0</tlib-version>

    <short-name>tool</short-name>

    

    

     <tag>

         <name>newMin</name>

         <tag-class>customaction.NewMinimumTag</tag-class>

         <body-content>empty</body-content>

        

         <attribute>

             <name>num1</name>

             <type>java.lang.Integer</type>

             <rtexprvalue>true</rtexprvalue>

         </attribute>

        

         <attribute>

             <name>num2</name>

             <type>java.lang.Integer</type>

             <rtexprvalue>true</rtexprvalue>

         </attribute>

        

         <attribute>

             <name>var</name>

             <type>java.lang.String</type>

             <required>true</required>

             <rtexprvalue>false</rtexprvalue>

         </attribute>

        

         <variable>

             <name-from-attribute>var</name-from-attribute>

             <variable-class>java.lang.Integer</variable-class>

             <scope>AT_END</scope>

         </variable>

     </tag>

    

 

</taglib>

   

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

   

<%@taglib prefix="tool" uri="/taglibs/customaction.tld" %>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

 

<tool:newMin var="min" num1="10" num2="20" />

 

최소값 : ${min}

 

</body>

</html>