Configure the Struts Tag Libraries

In Struts framework, you always need to configure the Struts tag libraries in order to access it in view page (JSP). There are two ways to configure it.

1. Strut Tag Libraries Manual Configuration

The manual configuration is the old and classic way, used in Struts version <= 1.1 and Servlet < 2.3 container. Download all the Struts dependencies, make sure the following “tld” files are copy to WEB-INF folder, you can find these files in the downloaded Struts library.

  • struts-bean.tld
  • struts-html.tld
  • struts-logic.tld
  • struts-tiles.tld

Declare the taglib uri in web.xml

web.xml

...
<taglib>
     <taglib-uri>
	  http://struts.apache.org/tags-bean
     </taglib-uri>
     <taglib-location>
	  /WEB-INF/struts-bean.tld
     </taglib-location>
</taglib>
<taglib>
     <taglib-uri>
	  http://struts.apache.org/tags-html
     </taglib-uri>
     <taglib-location>
	  /WEB-INF/struts-html.tld
     </taglib-location>
</taglib>
<taglib>
     <taglib-uri>
	  http://struts.apache.org/tags-logic
     </taglib-uri>
     <taglib-location>
	  /WEB-INF/struts-logic.tld
     </taglib-location>
</taglib>
<taglib>
     <taglib-uri>
	  http://struts.apache.org/tags-tiles
     </taglib-uri>
     <taglib-location>
	  /WEB-INF/struts-tiles.tld
     </taglib-location>
</taglib>
<taglib>
     <taglib-uri>
	  http://struts.apache.org/tags-nested
     </taglib-uri>
     <taglib-location>
	  /WEB-INF/struts-nested.tld
     </taglib-location>
</taglib>
...

Now you can access it in JSP page. The JSP’s @taglib uri have to match with web.xml <taglib-uri>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

Actually, you can define your own taglib uri name, for example

web.xml

...
<taglib>
     <taglib-uri>
	  customer-anything/tags-bean
     </taglib-uri>
     <taglib-location>
	  /WEB-INF/struts-bean.tld
     </taglib-location>
</taglib>
...

Then access it via your custom taglib uri name.

<%@ taglib uri="customer-anything/tags-bean" prefix="bean" %>

2. Strut Tag Libraries Automatic Configuration

This is the easy way, and used in Struts version 1.2, 1.3 and Servlet 2.3/2.4 container only. You do not need to define the “tlds” details in web.xml anymore, just include the struts-taglib.jar in your project classpath or copy it to WEB-INF/lib folder.

All the “tld” details are define inside the “struts-taglib.jar\META-INF\tld” folder. During deployment, the struts-bean.tld, struts-html.tld, struts-logic.tld and struts-tiles.tld will deploy automatically. However, you can access it via the following “pre-fixed uri” name only. In this method, you are not allow to change the “taglib uri” name.

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
``
##FAQ

Q : Look like the “taglib uri” is pointing to Apache website, how about the client has NO internet access?
A : The taglib uri is define in “`struts-taglib.jar\META-INF\tld`” folder, it’s just a project uri name, not pointing to Apache website, you still can access it even in no internet access environment.

Q : Can the manual configuration work in latest Struts 1.2 or 1.3?
A : Yes, Struts is backward compatible, the old way is still support in Struts 1.2 and 1.3.

Q : Which method is the best?
A : It’s depend, the automatic configuration is only worked in Servlet 2.3/2.4 container. If you are allow to choose, please go to the automatic method, why you want to copy the tld files manually?
posted @ 2015-08-28 13:18  wuhn  阅读(178)  评论(0编辑  收藏  举报