Native Dependency Injection in Struts2

Struts2 Native Dependency Injection

Struts2 provides support for Dependency injection. You may use DI containers like “Spring” in your Struts2 application, or may use Native Dependency Injection available in Struts2 (XWork2) framework. It’s based on Google’s “guice” (both being made by same person).

In this sample application, I have made Two Action classes:

  1. AddProductAction           To Add new product
  2. ListProductAction            To List all products

Both actions are dependent on a “Business” class  : ProductManager. Instead of creating instance of ProductManager in each action class, I have created its instance through “bean” element in “struts.xml”.

<bean class="com.mahendra.ProductManager" name="man" />

The ObjectFactory available in Struts2 will create instance of ProductManager using default constructor and is made available to all other components using name : “man”.

Every Action class needs to have property of type “ProductManager” along with annotation:

private ProductManager manager;
// This method is used for Injecting Object
public void setManager(ProductManager manager) {
this.manager = manager;

Now, lets create an application.


  1. Java SDK 1.6 (or newer)
  2. Eclipse helios (or newer)
  3. Apache Tomcat 6.0 (or 7.0)
  4. Struts 2.1.x from apache

Steps to create application:

  1. Create new Dynamic Web Application.
  2. Copy all required JAR’s inside “web-inf/lib” folderJARS required:
    jstl (For JSTL Core Tags)
  3. Modify your “web.xml” to have Struts2 dispatcher filter.
  4. Create new “struts.xml” in your source folder:
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "">
    <struts><bean class="com.mahendra.ProductManager"
    name="man" />
    <package name="p1" extends="struts-default"
    <action name="add"
    <result type="redirectAction">list</result>
    <action name="list"
  5. Create Model class : Product
    package com.mahendra;
    public class Product {
    private String name;
    private String description;
    private double price;
    public String getName() {
    return name;
    public void setName(String name) { = name;
    public String getDescription() {
    return description;
    public void setDescription(String description) {
    this.description = description;
    public double getPrice() {
    return price;
    public void setPrice(double price) {
    this.price = price;
    public Product(String name, String description, double price) {
    super(); = name;
    this.description = description;
    this.price = price;
    public Product() {
  6. Shared “Business” class :

    package com.mahendra;
    import java.util.*;
    public class ProductManager {
    private List<Product> products;
    public ProductManager() {
    System.out.println("Creating new manager...");
    products = new LinkedList<Product>();
    System.out.println("Empty Product list generated");
    public void add(Product p) {
    public Product search(String name) {
    Product p = null;
    for (int i = 0; i < products.size(); i++) {
    if (products.get(i).getName().equals(name)) {
    p = products.get(i);
    }return p;
    public void delete(String name) {
    Product p = search(name);
    if (p != null) {
    public List<Product> getProducts() {
    return products;
    public void setProducts(List<Product> products) {
    this.products = products;
  7. Create First Action class to add new product: AddProductAction
    package com.mahendra;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.inject.Inject;
    public class AddProductAction extendsActionSupport {
    private ProductManager manager;
    private Product product;
    public String execute() {
    return SUCCESS;
    public ProductManager getManager() {
    return manager;
    // This method is used for Injecting Object
    public void setManager(ProductManager manager) {
    this.manager = manager;
    public Product getProduct() {
    return product;
    public void setProduct(Product product) {
    this.product = product;
  8. Create another action class to List all products: ListProductAction
    import java.util.List;
    import com.opensymphony.xwork2.inject.Inject;
    public   class ListProductAction {
    @Inject("man")// requires setter 
    private ProductManager manager; 
    private List<Product> productList;
    public  String execute() {
    return "success";
    public  ProductManager getManager() {
     return  manager;
    public void  setManager(ProductManager manager) {
    this.manager = manager;
    public  List<Product> getProductList() {
     return  productList;
    public void setProductList(List<Product> productList) {
     this.productList = productList;
  9. Create new JSP page “index.jsp” and add following line to load “list” action.
    <% response.sendRedirect("list.action"); %>
  10. Create new JSP page “list.jsp” and add following lines to render form and list.
    <%@ taglib uri= "/struts-tags"  prefix= "s"%>
    <meta http-equiv= "Content-Type"  content= "text/html;charset=ISO-8859-1" >
    <title>Struts2 Dependency Demo</title>
    <s:form action="add"  method="post" >
    <s:textfield name= "" label= "Name of product" />
    <s:textfield name= "product.description" label= "Description"/>
    <s:textfield name= "product.price" label= "Product Price"  />
    <s:submit value= "Save"  />
    <h2>Available Products</h2>
    <td>Product Name</td>
    <s:iterator value= "productList"  var= "p" >
    <td>${ }</td>
    <td>${p.description }</td>
    <td>${p.price }</td>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s