All contents, unless mentioned, are written by me.

Abstract:
Struts 2 Framework uses a specialized tags on its JSP file to generate its View page. The Framework processed these tags (UI tags) using template mechanism and these templates are written in FreeMarker language and every copy of Struts 2 Framework are shipped with a default template.

This article shows that the Framework’s default template renders form, textfield, password, and submit Struts 2 UI tags into table and its children HTML tags. Further, this article also shows how to create a custom template with simple alterations so that the forementioned Struts 2 UI tags are not going to be rendered as table in the resulted HTML view.

Create a Base for your Custom Template

Struts 2 has a default set of user interface tags (UI Tags) that are backed with templates which in return, did the actual rendering of a resulted HTML page. In turn, these templates are written in the FreeMarker template language.

With these UI tags, a Struts 2 typical login form could be written like this:

<s:form action="some.action" method="post">
	<s:textfield label="Username" name="USERNAME"/>
	<s:password label="Password" name="PASSWORD"/>
	<s:submit value="Login"/>
</s:form>

But of course, defaults are rarely, if ever, satisfying and I daresay that before long you’d probably itching to deconstruct these templates with one that screamed ‘consistency’ for your overall web’s design.

Well, at least I did.

It is perhaps a common belief that extending an existing template is always more favorable against building it from scratch. I’d personally say that this should be the case even without a need for asking in the first place.

Now, in order to use your own custom template, you need to have a folder inside your WEB-INF/classes/template named with your intended custom template’s name and If you’re like me, building a web-app with NetBeans, you could create a package under the Source Packages on your Projects window, give it a name template.xxx where xxx is the name of your intended custom template. When you build your project, this package would automatically translates into a folder and put under WEB-INF/classes.

Next thing you want to do is copy all of Struts 2 standard template files into this folder, and start modifying these templates as you see fit.

You could find this standard template inside a Struts 2 core source code under main/resources/template folder. Typically you’d find several folder here, each represents a valid template. You might want to open xhtml folder here, and copy the whole contents in here (all files with .ftl extensions) into a new template folder you just created for your project.