Mahmoud Saleh
Mahmoud Saleh

Reputation: 33605

How to create a custom taglib

i want to make a util method to convert from map to list to be used with EL here's what i tried:

1- utils.taglib.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "https://facelets.dev.java.net/source/browse/*checkout*/facelets/src/etc/facelet-taglib_1_0.dtd">
<facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
    <namespace>http://mycomp.com/utils</namespace>

    <function>
      <function-name>mapToList</function-name>
      <function-class>com.mycomp.MyClass</function-class>
      <function-signature>java.util.List mapToList(java.util.Map)</function-signature>
    </function>

</facelet-taglib>

2- web.xml : i added it as i added the spring security tag lib, as follows:

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>
    /WEB-INF/springsecurity.taglib.xml
    /WEB-INF/utils.taglib.xml
    </param-value>
</context-param>

3- xhtml page :

xmlns:utils="http://mycomp.com/utils"

  <ui:repeat value="#{utils:mapToList(myBean.map)}" var="entry" >
    Key = #{entry.key} Value = #{entry.value} <br/>
  </ui:repeat>

4- Util Class :

public class MyClass{

    public static <T, S> List<Map.Entry<T, S>> mapToList(Map<T, S> map) {

    if (map == null) {
        return null;
    }

    List<Map.Entry<T, S>> list = new ArrayList<Map.Entry<T, S>>();
    list.addAll(map.entrySet());

    return list;
  }

 }

PROBLEM :

i am getting two errors:

1- when trying to open a page that uses spring security tags, the tags is not recognized anymore and i am getting the error:

Warning: This page calls for XML namespace http://www.springframework.org/security/tags declared with prefix sec but no taglibrary exists for that namespace.

2- when accessing the page that uses the new custom taglib, i am getting the error:

Function 'utils:mapToList' not found 

please advise, thanks.

Upvotes: 3

Views: 6365

Answers (1)

Ernesto Campohermoso
Ernesto Campohermoso

Reputation: 7371

In the pount 2 note you must define multiple tag libraries to be used by passing facelets.LIBRARIES as a semicolon-delimited list.

So try this:

<context-param>
     <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
     <param-value>/WEB-INF/springsecurity.taglib.xml;/WEB-INF/utils.taglib.xml</param-value>
</context-param>

Upvotes: 8

Related Questions