Frameworks >> Struts >> Web Applications


Configure Struts2 Tag Library


As with Struts1, there are tag libraries shipped with Struts2 which help in placing the UI elements like input boxes, submit buttons, checkbox list and radio buttons. The configuration of struts2 tag libraries include using the taglib directive in the JSP and nothing else. There is no need to configure taglibs in web.xml which used to be done in case of Struts1 or custom tag libraries.

The use of Struts2 StrutsPrepareAndExecuteFilter helps in preparing the taglibs and provides the implementation of tags to the JSP being loaded. The example of coding a Struts2 tag libraries in JSP is shown below:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <s:form action="Test" method="POST" theme="simple">
            <b>Text Box</b> <s:textfield name="box"/>
            </br>
        </s:form>
    </body>
</html>

The use of taglib directive in the first line shows how struts2 tags can be configured and then used in a JSP. The prefix s in the taglib directive defines the prefix which can be used when using the tags in the JSP. Usually, the prefix s is used in all kind of applications but it is not mandatory to use this prefix. The taglib directive statement can also be replaced by the following statement:

<%@ taglib prefix="test" uri="/struts-tags" %>

After using the above statement, following statement makes use of this:
Text Box

Java Tutorials
File I/O in Java
How HashSet works internally in Java
Java File Writer Example
Java HashSet add Operation
Java HashSet remove Operation

Leave Comment