Tuesday, July 2, 2013

Struts2: How to display data contained in a list/map/List of Object from Java to screen

Today we will learn how to pass data stored in List or Map from Java classes to screens (JSP in this case) using Struts2
Display list/map data from java
using Struts2

Pre-requisite: You need to include the struts tag libaries in your web project and put the below line in your JSP to use the tags.
 <%@ taglib prefix="s" uri="/struts-tags"%>

Let's start with a list of string objects.

1. Suppose in your action class (Java) you have a list of String objects which you want to display on the screen.
For eg. List of Movies:
  • Batman Begins
  • The Dark Knight
  • The Dark Knight Rises
  • Inception
  • Memento
  • The Prestige
Yeah you get it! Like millions I am also a Nolan fan.
Now to display this list on the screen you need to create a List object in your action.

 private List movieList;

Now create the required getter/setter methods for this object as below.
Tip: For eclipse users, short-cut to creating getters/setters is ALT+SHIFT+S + R

public List getMovieList() {
  return movieList;
public void setMovieList(List movieList) {
  movieList.add("Batman Begins");
  movieList.add("The Dark Knight");
  movieList.add("The Dark Knight Rises");
  movieList.add("The Prestige");  
  this.movieList = movieList;

As you can see I have added the movies to the movieList object.
Now coming to the JSP, you will have to use the "iterator" tag of the struts library to iterate through the object.

 <s:iterator value="movieList"> //value field contains the name of the object in Java.  
 <div movie="<s:property>"><s:property /></div> //<s:property /> will get the movie names as there is only single value fields in the list  

That's pretty much it. You will get the movie names on your screen.

2. Now let's consider you have a Map and you want to use data from your map object on the screen.

First we will create a Map object just like we did for the list object.

 private Map<String, String> movieMap;  

And the getters/setters

 public Map<String, String> getMovieMap() {  
      return movieMap;  
 // I have created a movie:year of release(key:value) map  
 public void setMovieMap(Map<String, String> movieMap) {  
      movieMap.put("Batman Begins", "2005");  
      movieMap.put("The Dark Knight", "2008");  
      movieMap.put("The Dark Knight Rises", "2012");  
      movieMap.put("Inception", "2010");  
      movieMap.put("Memento", "2000");  
      movieMap.put("The Prestige", "2006");  
      this.movieMap = movieMap;  

Now in your JSP, let's suppose you want to create a node/div which will display the movie name and an attribute field storing the year of release. You need to do something like this.

 <s:iterator value="movieMap">  
 <div year="<s:property value="value">"><s:property value="key" /></div>  

And lastly what if you have a List of Java objects and you need to extract data from those Java objects?
Let's suppose you have a Movie class as below:

 public class Movie {  
      private String name;  
      private int year;  
      private String director;  
      private String genre;  
      public String getName() {  
           return name;  
      public void setName(String name) {  
           this.name = name;  
      public int getYear() {  
           return year;  
      public void setYear(int year) {  
           this.year = year;  
      public String getDirector() {  
           return director;  
      public void setDirector(String director) {  
           this.director = director;  
      public String getGenre() {  
           return genre;  
      public void setGenre(String genre) {  
           this.genre = genre;  

And you create a list of this Movie objects in your action class

 Movie movie = new Movie();  
 movie.setName("The Dark Knight");  
 // similarly you create objects for other movies  
 List<Movie> movieList = new ArrayList<Movie>();  
 movieList.add(/*all the above movie objects*/);  

In the JSP, you need to do two things.
  1. Iterate over the list
  2. Use a local variable to extract the individual properties from the java object
 <s:iterator value="movieList" var="eachMovie"> //here eachMovie is a local variable
 <div year="<s:property value="#eachMovie.year">" //attribute year created with the correspoding value  
       director="<s:property value="#eachMovie.director">" //similarly director and genre are created  
       genre="<s:property value="#eachMovie.genre">">  
 <s:property value="#eachMovie.name"></div> // name of the movie display on screen.  

That's it! That's how you bind your java objects with your JSP using struts and access the data.
Please post your comments and views on this tutorial.