Struts 2 XML response Annotation example

Struts 2 XML response Annotation example

This code will show you how to return XML response in struts 2 application. Here I have used annotation based struts 2 application and if you are using struts.xml for your configuration then you will have to add action class mapping in your XML file.

  • Project structure:

Struts 2 XML response Annotation example

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javahonk</groupId>
  <artifactId>Struts2XMLResponse</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Struts2XMLResponse Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.16.3</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>Struts2XMLResponse</finalName>
  </build>
</project>

  • XMLResponseAction.java:
package com.javahonk.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ActionSupport;

public class XMLResponseAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    @Override
    @Action(value = "/xMLResponseAction")
    public String execute() throws Exception {
        
        HttpServletResponse response = ServletActionContext.getResponse();

        PrintWriter printWriter = null;
        response.setContentType("text/xml;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");

        StringBuilder stringBuilder = new StringBuilder("<person>");
        
        try {
            
            printWriter = response.getWriter();
            stringBuilder.append("<firstName>" + "Java" + "</firstName>");
            stringBuilder.append("<lastName>" + "Honk" + "</lastName>");
            stringBuilder.append("</person>");
            printWriter.print(stringBuilder.toString());
            
        } catch (IOException ioException) {
            ioException.printStackTrace();          
        } finally {
            printWriter.close();
            printWriter = null;
        }
        return NONE;
    }   

}

  • index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Struts 2 XML response Annotation example</title>
        <s:head/>
    </head>
 
    <body>
        <h2 style="color: green"><s:text name="Struts 2 XML response Annotation example" /></h2>
        <a href="xMLResponseAction">Click here to pull XML data from server</a>        
    </body> 
</html>  
  • web.xml:
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>struts.devMode</param-name>
            <param-value>true</param-value>
        </init-param>                       
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>

  • Run application in tomcat server:

Struts 2 XML response Annotation example

and on above page Click here to pull XML data from server it will show you XML data output as below:

Struts 2 XML response Annotation example

For more information please read this struts tutorial

download Download Project:  Struts2XMLResponse

Leave a Reply

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