0

While calling the static method of a Java class from tld , I am facing some issues like while running the jsp file it always display this ${test:concat("java")} as an output it's not even calling the java class.

index.jsp File

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="test" uri="/WEB-INF/SubstrDescriptor.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>JSP Custom Taglib example: Substr function</title>
</head>
${test:concat("java")}
</html>

SubstrDescription.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/web-jsptaglibrary_2_0.xsd"
    version="2.0">

    <tlib-version>2.0</tlib-version>

    <function>
        <name>concat</name>
        <function-class>java4s.Demo</function-class>
        <function-signature>java.lang.String doMyStuff( java.lang.String )
        </function-signature>
    </function>
</taglib>

Demo.java

package java4s;

public class Demo {

     public static String doMyStuff( String myparam )
        {
         System.out.println(myparam);
           return myparam;
        }

}
Braj
  • 46,415
  • 5
  • 60
  • 76
CoreThought
  • 113
  • 6
  • 16
  • Don't edit your original code otherwise there is no meaning of any answer that address the problem in exiting code. Add edited code just below the actual one and mention EDIT. – Braj Jun 22 '14 at 11:02

2 Answers2

2

Make few change as mentioned below and check it again.

  • Define <uri>SubstrDescriptor</uri> in tld.
  • Use <%@ taglib prefix="test" uri="SubstrDescriptor"%> in JSP.

Please have a look at similar post How to call a static method in JSP/EL? that might help you for better understanding.

JSP:

<%@ taglib prefix="test" uri="SubstrDescriptor"%>
<body>
     ${test:concat("java")}
</body>

SubstrDescription.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/web-jsptaglibrary_2_0.xsd"
    version="2.0">

    <tlib-version>2.0</tlib-version>    
    <uri>SubstrDescriptor</uri> 
    <function>
        <name>concat</name>
        <function-class>java4s.Demo</function-class>
        <function-signature>java.lang.String doMyStuff( java.lang.String )
        </function-signature>
    </function>
</taglib>

Project structure:

WebContent
          |
          |__WEB-INF
          |         |
          |         |__SubstrDescription.tld
          |         |__web.xml
          |
          |__index.jsp
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
1

EDIT : damned wrong. Thanks to Braj for putting me in the right direction - will be soon deleted

You should try to allways give it the same name. You wrote

<function-signature>java.lang.String doMyStuff( java.lang.String )
        </function-signature>

when it should be

<function-signature>java.lang.String concat( java.lang.String )
        </function-signature>
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252