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?
InStruts框架配置指南:手动与自动方式
本文详细介绍了InStruts框架中Strutstag库的两种配置方式:手动配置和自动配置。手动配置适用于旧版Struts,而自动配置则用于Servlet2.3/2.4容器。此外,文章还提供了常见问题解答,包括关于本地互联网访问的疑惑及不同Struts版本的兼容性。
3万+

被折叠的 条评论
为什么被折叠?



