• 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 Merge Tag Example

December 13, 2013 //  by Krishna Srinivasan//  Leave a Comment

Struts 2 merge tag is useful for merging the two lists into one object. This is very helpful when you have two different list of objects and then you need to merge them and use it in your JSP page. Merge tag has nested param element to take the multiple list objects in each line. You can add more than one param element and as many as you want. All the lists will be merged and assigned to one variables which is given in the var attribute of merge tag. I have used the iterate tag example to demonstrate this example.

1. Create Data Bean

Create a UserDetails.java class for storing the user details.

[code lang=”java”] package javabeat.net.struts2;

public class UserDetails {
private String name;
private String city;
public UserDetails(String… args){
this.name = args[0];
this.city = args[1];
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}

}
[/code]

2. Create Struts 2 Action

Create a Struts 2 action class with two different list objects and pass it to the JSP page.

[code lang=”java”] package javabeat.net.struts2;

import java.util.ArrayList;

public class Struts2Iteration {
private ArrayList<UserDetails> users;
private ArrayList<UserDetails> admins;

public ArrayList<UserDetails> getAdmins() {
return admins;
}

public void setAdmins(ArrayList<UserDetails> admins) {
this.admins = admins;
}

public ArrayList<UserDetails> getUsers() {
return users;
}

public void setUsers(ArrayList<UserDetails> users) {
this.users = users;
}
public String execute(){
users = new ArrayList<UserDetails>();
users.add(new UserDetails("Name 1","City 1"));
users.add(new UserDetails("Name 2","City 2"));

admins = new ArrayList<UserDetails>();
admins.add(new UserDetails("Admin 1","City 1"));
admins.add(new UserDetails("Admin 2","City 2"));

return "success";
}
}

[/code]

3. Merge Tag Example

See the below code, we are using s:merge tage to merge two lists. Later it is displayed using the s:iterator tag.

[code lang=”xml”] <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts 2 Merge Example</title>
</head>
<body>
<B>Struts 2 Merge Example</B>
<form action="iterationexample">
<s:merge var="allusers">
<s:param value="users"/> ,
<s:param value="admins"/><br/>
</s:merge>

<s:iterator value="allusers">
<s:property value="name"/> ,
<s:property value="city"/><br/>
</s:iterator>

</form>
</body>
</html>
[/code]

4. Struts.xml configurations

Create struts.xml configuration file.

[code lang=”xml”] <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="tags" extends="struts-default">
<action name="iterationexample" class="javabeat.net.struts2.Struts2Iteration"
method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
[/code]

5. Run the application

If you access the application http://localhost:8080/Struts2App/iterationexample.action. You would see the following output in your screen.

Struts 2 Merge Tag Example Screen

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 Iterator Tag Example
Next Post: Struts 2 Generator Tag Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

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