• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Struts 2 Resource Bundle Example

December 15, 2013 //  by Krishna Srinivasan

It is best practice to have all the labels used in your application in the centralized location. Tradionally these message resources are stored in the property files using the key=value pairs and loaded on demand by the forms. Every framework supports this basic feature to configure the message resources. This example shows how to load property files in your Struts 2 Application and display it in the screen. Lets look at the code.

1. Action Class

package javabeat.net.struts2;

public class Struts2HelloWorldAction{
	private String firstName;
	private String lastName;

	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String execute(){
		return "success";
	}
}

2. Property File

Create a property file in the class path.

form_labels.properties

firstName=First Name
lastName=Last Name

3. Struts 2 Configuration File

Add the entry for loading the property files from the class path.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="form_labels" />
<constant name="struts.devMode" value="true" />
<package name="uitagsdemo" extends="struts-default">
	<action name="PropertyDemo" class="javabeat.net.struts2.Struts2HelloWorldAction" >
		<result name="success">Input.jsp</result>
	</action>
</package>
</struts>

4. JSP File

Input.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
	<h2>JavaBeat - Struts 2 Property Labels Demo</h2>
	<s:form action="PropertyDemo">
		<s:textfield name="firstName" key="firstName" />
		<s:textfield name="lastName" key="lastName" />
		<s:submit label="Submit" />
	</s:form>

</body>
</html>

5. Resource Bundle Demo

If you access the application using URL http://localhost:8080/Struts2App/PropertyDemo, you would see the following output.

Struts 2 Resource Bundle Example

Category: StrutsTag: Struts 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Struts 2 + Spring Integration Example
Next Post: Struts 2 Annotation Example »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Implement getActiveCount() Method of ThreadPoolExeceutor in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact