When you use a Struts 2 tag such as s:select in your web page, the Struts 2 framework generates HTML that styles the appearance and controls the layout of the select control. The style and layout is determined by which Struts 2 theme is set for the tag. Struts 2 comes with three built-in themes: simple, xhtml, and css_xhtml. If you don’t specify a theme, then Struts 2 will use the xhtml theme by default.

Thus in our case, Struts 2 automatically rendered the controls using default xhtml theme.

Specifying The Theme in Struts 2

The Struts 2 tags have a theme attribute you can use to specify which Struts 2 theme should be used when creating the HTML for that tag. The values for the theme attribute are simple, xhtml, css_xhtml, and ajax.

You can specify the theme on a per Struts 2 tag basis or you can use one of the following methods to specify what theme Struts 2 should use:

  • The theme attribute on the specific tag
  • The theme attribute on a tag’s surrounding form tag
  • The page-scoped attribute named “theme”
  • The request-scoped attribute named “theme”
  • The session-scoped attribute named “theme”
  • The application-scoped attribute named “theme”
  • The struts.ui.theme property in struts.properties (defaults to xhtml)

Thus, you can specify what theme you want to use at any level. Tag level, enclosed tag lever, form level, page level or at application level.

I preferred specifying theme at the application level. To override the default scheme you can create (or update existing) struts.xml file in your project.

Create struts.xml file and add following line into it:

<constant name="struts.ui.theme" value="css_xhtml" />
<constant name="struts.ui.templateDir" value="template" />

So if you want to take full control for your user interface and want to align all controls yourself, I would suggest you to use css_xhtml theme instead of default xhtml.

The theme for above JSP code when changed into css_xhtml would generate following HTML code.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:property value="getText('global.form')" /> - Struts2 Demo | dineshonjava.com</title>
<s:head />
</head>
 
<body>
<h2><s:property value="getText('global.form')" /></h2>
 
<s:form action="employee" method="post" validate="true" theme="css_xhtml">
    <s:textfield name="name" key="global.name" size="20" theme="css_xhtml"/>
    <s:textfield name="age" key="global.age" size="20" theme="css_xhtml"/>
    <s:textfield name="email" key="global.email" size="20" theme="css_xhtml"/>
    <s:textfield name="telephone" key="global.telephone" size="20" theme="css_xhtml"/>
    <s:submit name="submit" key="global.submit" align="center" theme="css_xhtml"/>
</s:form>

<s:url id="localeEN" namespace="/" action="locale" >
   <s:param name="request_locale" >en</s:param>
</s:url>
<s:url id="localeHN" namespace="/" action="locale" >
   <s:param name="request_locale" >hn</s:param>
</s:url>
<s:url id="localeES" namespace="/" action="locale" >
   <s:param name="request_locale" >es</s:param>
</s:url>
<s:url id="localezhCN" namespace="/" action="locale" >
   <s:param name="request_locale" >zh_CN</s:param>
</s:url>
<s:url id="localeDE" namespace="/" action="locale" >
   <s:param name="request_locale" >de</s:param>
</s:url>
<s:url id="localeFR" namespace="/" action="locale" >
   <s:param name="request_locale" >fr</s:param>
</s:url>
 
<s:a href="%{localeEN}" >English</s:a>
<s:a href="%{localeHN}" >Hindi</s:a>
<s:a href="%{localeES}" >Spanish</s:a>
<s:a href="%{localezhCN}" >Chinese</s:a>
<s:a href="%{localeDE}" >German</s:a>
<s:a href="%{localeFR}" >France</s:a>
</body>
</html>
<<Previous <<   || Index ||   >>Next >>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago