Mark Juliano wrote:
>
> To all...
>
> With regards to Tag Libraries.
>
> Is it possible to specify a default value for an attribute via the Tag Lib
> (.tld) file???  This way if a user does not use an optional property, it gets
> its value from this file???
>
> If this is not possible, is there anyway to add information in the .tld that the
> Tag Lib Component can access???

No, you can't set defaults via the TLD. If you can live with a hard coded
default, just make the attribute optional and use a default value defined
in the tag handler class if it's not set by the page author.

If you need to be able to configure the default value, I suggest you
use a servlet context init parameter, defined in the web.xml file, and
read it in the tag handler.

  web.xml:

  <web-app>
     ...
     <context-param>
        <param-name>com.mycompany.taglib.foo</param-name>
        <param-value>bar</param-value>
     </context-param>
     ...
  </web-app>

  Tag handler class:

  ...
  String optAttribute = getOptionalAttribute();
  if (optAttribute == null) {
    ServletContext sc = pageContext.getServletContext();
    optAttribute = sc.getInitParameter("com.mycompany.taglib.foo");
  }
  ...

If you have many default values you need to define, you may consider
using a special servlet (marked as "load-at-startup") to initialize all
defaults and use servlet init parameters instead of context init parameters.
The servlet can then set context attributes for all default values, that
can be retrieved by the tag handlers. One advantage with this approach
is that the servlet can verify that all default values have been set to
okay values when the application starts, so you don't have to do that
in all tag handlers. It also allows you to use short init param value
names without risking name clashes with other parts of the application
(you should still use the package name like names for the context
attributes though, especially if this is a tag library that can
potentially be used with any application and you need to be careful
about name clashes).

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to