Develop Faster With Tag Libraries (Continued)
Using a custom tag library in general is easy: Simply do the steps discussed in the previous section. However, some tag libraries include rules that make using it a more involved process. The Struts HTML tag library is one of them. Some of the tags are simple and easy to use; others, however, depend on other tags or other elements in the Struts application.
I have loosely grouped the tags in the HTML library into two categories: the simple and easy-to-use tags, which I call "independent tags," and those that must be used with a form tag. Tags in the second category are simply called form-related tags. I'll explain independent tags here, and form-related tags in Part 2 of this article series.
The HTML library contains several independent tags that are easy to use. Here are the more important ones.
The <html> Tag
The <html> tag is the easiest tag in the HTML tag library. It can have two attributes: locale and xhtml, neither of which is required. This code is from a JSP page that uses the <html> tag:
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html locale="true">
<head>
<title>Welcome</title>
</head>
<body>
Hello World!
</body>
</html:html>
Note that instead of the normal <html> element, you use the <html:html> tag. The first html in <html:html> refers to the prefix, and the second refers to the <html> tag itself. Note also that you use the locale attribute. The JSP page will be rendered as:
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
Hello World!
</body>
</html>
Note that the locale attribute in <html:html locale="true"> has been translated into lang="en" in the resulting HTML page. The result depends on the locale of the server where the Struts application resides. If you deploy the application to a server that has a different locale, you don't need to change your code. The locale will be adjusted automatically.
The <base> Tag
The <base> tag renders an HTML element with an href attribute pointing to the absolute location of the enclosing JSP page. This tag is valid only when nested inside a head tag body. For instance, this JSP page:
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html locale="true">
<head>
<title>Welcome</title>
<html:base/>
</head>
<body>
Hello World!
</body>
</html:html>
is translated like this:
<html lang="en">
<head>
<title>Welcome</title>
<base href="http://www.domain.com/myStrutsApp/testing.jsp">
</head>
<body>
Hello World!
</body>
</html>
Back to top
|