简体   繁体   中英

Servlet - web.xml vs Java config

I'm migrating an old project from a web.xml approach to a complete Java-style Servlet 3.0 configuration.

But I can't understand how to translate part of the XML configuration in Java code. In particular the next snippet:

<jsp-config>
    <taglib>
        <taglib-uri>....</taglib-uri>
        <taglib-location>....</taglib-location>
    </taglib>
</jsp-config>

Any hint would be welcome!

As a secondary, more academic, question: do Servlet 3.0 API offer a full coverage of what you could do with XML, or not?

Stefano,

Since JSP 2.0, there is no need in put <taglib> tag in web.xml. From Head First Servlets and JSP book:

The Container automatically builds a map between TLD files and names, so that when a JSP invokes a tag, the Container knows exactly where to find the TLD that describes the tag. How? By looking through a specific set of locations where TLDs are allowed to live. When you deploy a web app, as long as you put the TLD in a place the Container will search, the Container will find the TLD and build a map for that tag library.

So, all you have to do is to have a TLD file with the correct URI.

Places to put your TLD file:

  • Directly inside WEB-INF
  • Directly inside a sub-directory of WEB-INF
  • Inside the META-INF directory inside a JAR fi lethat's inside WEB-INF/lib
  • Inside a sub-directory of META-INF inside a JAR fi lethat's inside WEB-NF/lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM