jsp custom tag issues

gudii9
gudii9 used Ask the Experts™
on
I tried jsp custom tag example.

custom tag color is not changing even though i changed to blue or some other color it still shows default color as attached.

Index.jsp is

<%--
    Document   : index
    Created on : Nov 6, 2018, 1:41:41 PM
    Author     : saiganesh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="mytags" uri="/WEB-INF/tlds/my_tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <mytags:formattag colour="blue" font_size="50" is_uppercase="false">
            this is ext hello custom tag
            </mytags:formattag>
    </body>
</html>



my_tags.tld is

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>my_tags</short-name>
  <uri>/WEB-INF/tlds/my_tags</uri>
 
  <tag>
      <name>formattag</name>
      <tag-class>tags.FormatTag</tag-class>
      <body-content>scriptless</body-content>
      <attribute>
          <name>colour </name>
          <required>true</required>
      </attribute>
        <attribute>
          <name>font_size </name>
          <required>true</required>
      </attribute>
        <attribute>
          <name>is_uppercase </name>
          <required>false</required>
      </attribute>
     
  </tag>
 
  <!-- A validator verifies that the tags are used correctly at JSP
          translation time. Validator entries look like this:
       <validator>
           <validator-class>com.mycompany.TagLibValidator</validator-class>
           <init-param>
              <param-name>parameter</param-name>
              <param-value>value</param-value>
           </init-param>
       </validator>
    -->
  <!-- A tag library can register Servlet Context event listeners in
         case it needs to react to such events. Listener entries look
         like this:
      <listener>
          <listener-class>com.mycompany.TagLibListener</listener-class>
      </listener>
    -->
</taglib>



FormatTag.java is

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tags;

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;

/**
 *
 * @author saiganesh
 */
public class FormatTag extends SimpleTagSupport{
    private String colour;
    public int font_size;
    public boolean is_uppercase;

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public int getFont_size() {
        return font_size;
    }

    public void setFont_size(int font_size) {
        this.font_size = font_size;
    }

    public boolean isIs_uppercase() {
        return is_uppercase;
    }

    public void setIs_uppercase(boolean is_uppercase) {
        this.is_uppercase = is_uppercase;
    }

    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext=(PageContext)getJspContext();
        JspWriter out=pageContext.getOut();
        String style="";
        if(is_uppercase){
            style="style=\"text-transform: uppercase: font-size:"+font_size+"px: colour: "+colour+";\"";
        }
        else{
            style="style=\"font-size:" + font_size+"px: colour: "+colour+";\"";
        }
       
      try{  
        out.print("<p"+style+">");
        getJspBody().invoke(out);
        out.print("</p>");
      }catch(IOException | JspException e){
         
      }
   
   
}
}
CustomTag1.png
CustomTag2.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Director of Technologyy
Commented:
You have color spelled as "colour", but html/css uses "color"

Author

Commented:
i changed colour to color in FormatTag.java and my_tags.tld
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tags;

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;

/**
 *
 * @author saiganesh
 */
public class FormatTag extends SimpleTagSupport{
    private String color;
    public int font_size;
    public boolean is_uppercase;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getFont_size() {
        return font_size;
    }

    public void setFont_size(int font_size) {
        this.font_size = font_size;
    }

    public boolean isIs_uppercase() {
        return is_uppercase;
    }

    public void setIs_uppercase(boolean is_uppercase) {
        this.is_uppercase = is_uppercase;
    }

    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext=(PageContext)getJspContext();
        JspWriter out=pageContext.getOut();
        String style="";
        if(is_uppercase){
            style="style=\"text-transform: uppercase: font-size:"+font_size+"px: colour: "+color+";\"";
        }
        else{
            style="style=\"font-size:" + font_size+"px: color: "+color+";\"";
        }
        
      try{  
        out.print("<p"+style+">");
        getJspBody().invoke(out);
        out.print("</p>");
      }catch(IOException | JspException e){
          
      }
    
    
}
}

Open in new window



and
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>my_tags</short-name>
  <uri>/WEB-INF/tlds/my_tags</uri>
  
  <tag>
      <name>formattag</name>
      <tag-class>tags.FormatTag</tag-class>
      <body-content>scriptless</body-content>
      <attribute>
          <name>color</name>
          <required>true</required>
      </attribute>
        <attribute>
          <name>font_size </name>
          <required>true</required>
      </attribute>
        <attribute>
          <name>is_uppercase </name>
          <required>false</required>
      </attribute>
      
  </tag>
  
  <!-- A validator verifies that the tags are used correctly at JSP
          translation time. Validator entries look like this: 
       <validator>
           <validator-class>com.mycompany.TagLibValidator</validator-class>
           <init-param>
              <param-name>parameter</param-name>
              <param-value>value</param-value>
           </init-param>
       </validator>
    -->
  <!-- A tag library can register Servlet Context event listeners in
         case it needs to react to such events. Listener entries look
         like this: 
      <listener>
          <listener-class>com.mycompany.TagLibListener</listener-class> 
      </listener>
    -->
</taglib>

Open in new window


i clean built getting below error

HTTP Status 500 - Internal Server Error


type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: /index.jsp(16,8) PWC6130: According to the TLD or the tag file, attribute color is mandatory for tag formattag


note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs.


GlassFish Server Open Source Edition 4.0

Author

Commented:
i changed index.jsp line 16 also to color still not working

<%-- 
    Document   : index
    Created on : Nov 6, 2018, 1:41:41 PM
    Author     : saiganesh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="mytags" uri="/WEB-INF/tlds/my_tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <mytags:formattag color="blue" font_size="500" is_uppercase="false">
            this is ext hello custom tag
            </mytags:formattag>
    </body>
</html>

Open in new window

even font size i tried changing from 50 to 500 which did not work. I tried in chrome instead of Internet explorer browser still not working

please advise
Jeffrey Dake Senior Director of Technologyy

Commented:
It still looks like your css is not quite right

if(is_uppercase){
            style="style=\"text-transform: uppercase: font-size:"+font_size+"px: colour: "+color+";\"";
        }
        else{
            style="style=\"font-size:" + font_size+"px: color: "+color+";\"";
        }

Open in new window


You have a colon after your font-size.

You have something like style="font-size:12px:"
when it should be style="font-size:12px;"

Author

Commented:
let me try and get back to you. thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial