프로그래밍/JSP Servlet

JSP/Servlet – 태그 클래스를 이용해서 변수 지원 커스텀 액션 만들기

가카리 2016. 3. 1. 19:16
반응형

 

JSP/Servlet- 태그 클래스를 이용해서 변수 지원 커스텀 액션 만들기

 

태그 클래스를 이용해서 변수를 리턴하고 싶다면 JspContext 객체의 setAttribute 메소드를 이용하면됩니다.

 

    JspContext context = getJspContext();

    context.setAttribute("변수이름", "값");

 

 

위와 같이 예제를 구성해줍니다.

 

MinimumTag.java

 

package tool;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspContext;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class MinimumTag extends SimpleTagSupport{

    private int num1, num2;

    public void setNum1(Integer num1){

        this.num1 = num1;

    }

    public void setNum2(Integer num2){

        this.num2 = num2;

    }

    

    @Override

    public void doTag() throws JspException, IOException {

        // TODO Auto-generated method stub

        JspContext context = getJspContext();

        if(num1 < num2)

            context.setAttribute("minimum", num1);

        else

            context.setAttribute("minimum", num2);

        return;

    }

}

 

 

tools.tld

여기서 변수를 지원하기 위해 <variable>이라는 서브 엘리먼트를 쓰고

변수의 이름을 정하는 <name-given> 변수의 타입을 정하는 <variable-class> 변수의 사용범위를 정하는 <scope> 엘리먼트를 써줍니다.

그리고 <rtexprvalue> 엘리먼트는 커스텀 액션의 애트리뷰트 값에 익스프레션이나 EL식을 포함시킬 수 있는지 여부를 결정합니다.

 

<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">

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

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

    <tag>

        <name>min</name>

        <tag-class>tool.MinimumTag</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>

        <variable>

            <name-given>minimum</name-given>

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

            <scope>AT_END</scope>

        </variable>

    </tag>

</taglib>

 

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>ch10_jsp_tag_variable4</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<jsp-config>

         <taglib>

         <taglib-uri>/taglibs/tools.tld</taglib-uri>

         <taglib-location>/WEB-INF/tlds/tools.tld</taglib-location>

        </taglib>

    </jsp-config>

</web-app>

 

Minimum.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

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

<!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=EUC-KR">

<title>Insert title here</title>

</head>

<body>

    <tool:min num1="${param.NUM1 }" num2="${param.NUM2 }"/>

        최소값 : ${minimum }

</body>

</html>

 

실행 화면

주소에 NUM1값과 NUM2값을 입력해준 결과 작은 값이 출력되고 있습니다.

그런데 이와 같이 하면 변수의 이름이 고정되있다는 단점이 있습니다.

 

변수가 고정된 단점을 없애기 위해서 다음과 같이 커스텀 액션의 애트리뷰트를 이용해서 변수의 이름을 지정할 수 있도록 해야합니다.

 

    <tool:min var="minimum" num1="15" num2="5" />

 

TLD파일에 <attribute> 엘리먼트를 추가해서 지정을 할 수 있습니다.

 

 

위와 같이 예제를 구성합니다.

 

MinimumTag.java

 

package tool;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspContext;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class MinimumTag extends SimpleTagSupport{

    private String var;

    private int num1, num2;

    

    //새로 받을 변수

    public void setVar(String var){

        this.var = var;

    }

    

    public void setNum1(Integer num1){

        this.num1 = num1;

    }

    

    public void setNum2(Integer 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;

    }

    

}

 

 

tools.tld

기존에 attribute 엘리먼트가 2개 였지만 1개가 추가되었습니다.

여기서 중요한 점은 requried 가 true로 됨으로써 필수 애트리뷰트임을 표시하였고 rtexprvalue 가 false라서 익스프레션이나 EL식의 애트리뷰트 값으로

사용할 수 없도록 하였습니다.

 

 

<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">

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

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

    <tag>

        <name>newMin</name>

        <tag-class>tool.MinimumTag</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>

 

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>ch10_jsp_tag_variable5</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<jsp-config>

         <taglib>

         <taglib-uri>/taglibs/tools.tld</taglib-uri>

         <taglib-location>/WEB-INF/tlds/tools.tld</taglib-location>

        </taglib>

    </jsp-config>

</web-app>

 

 

Minimum.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

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

<!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=EUC-KR">

<title>Insert title here</title>

</head>

<body>

    <tool:newMin var="MIN" num1="${param.NUM1 }" num2="${param.NUM2 }"/>

        최소값 : ${MIN }

</body>

</html>

 

 

실행 화면

실행 결과는 첫번째 예제와 같습니다.

 

반응형