Thursday 17 June 2021

Spring Boot Struts 2 Integration

InOrder to Integrate Spring boot with the struts 2 follow the below Sample provided. This is a very basic project and gives you an understanding of the spring boot and Struts 2 Integration.

Create a Spring boot starter project with war packaging.


Maven Dependencies.


Add the Following dependencies to your maven project.

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>

<version>2.0</version>

<scope>provided</scope>

</dependency>


<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-core</artifactId>

<version>2.5.26</version>

</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-spring-plugin</artifactId>

<version>2.5.26</version>

</dependency>


<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-java8-support-plugin</artifactId>

<version>2.5.2</version>

</dependency>

</dependencies>


Create the Action Class: GreetUserAction.java


package com.searchendeca.demo.action;

import com.opensymphony.xwork2.ActionSupport;

public class GreetUserAction extends ActionSupport {

private String message;

    public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

@Override

    public String execute() throws Exception {

        return SUCCESS;

    }

}

This class should extend the ActionSupport class and has the default method as the execute.


Create Struts Configuration File: Struts2Configuration.java


package com.searchendeca.demo.config;


import org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;


import javax.servlet.DispatcherType;


@Configuration

public class Struts2Configuration {

    @Bean

    public FilterRegistrationBean someFilterRegistration() {

        FilterRegistrationBean registration = new FilterRegistrationBean();

        registration.setFilter(new StrutsPrepareAndExecuteFilter());

        registration.addUrlPatterns("*.action");

        registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD);

        registration.setName("StrutsPrepareAndExecuteFilter");

        return registration;

    }

}


This is the place we filter the struts URL. In the above sample, we are allowing only URLs with the *.action.


Create Struts File: struts.xml


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"

        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.devMode" value="true"/>

    <package name="basicStruts2" extends="struts-default">

        <action name="message" class="com.searchendeca.demo.action.GreetUserAction" method="execute">

        <param name="message">Welcome to SearchEndeca</param>

            <result name="success">/greetUser.jsp</result>       

             </action>

    </package>

</struts>

In this File we register the action and map to the result. In the above example we are mapping the success result to the /greetUser. also sending the parameters message to display in the jsp.


Create Jsp:  greetUser.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

    "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Greet User</title>

</head>

<body>

    <center>

        <h3>${message}</h3>

    </center>

</body>

</html>


ServletInitializer Class: ServletInitializer.java


package com.searchendeca.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

return application.sources(SpringStrutsDemoApplication.class);

}

}

SpringBoot Application Class :SpringStrutsDemoApplication.java

package com.searchendeca.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringStrutsDemoApplication {


public static void main(String[] args) {

SpringApplication.run(SpringStrutsDemoApplication.class, args);

}

}


After this execute the command mvn : clean, package create the war file deploy it to either tomcat or the default server.


In-Browser Navigate to http://localhost:8080/SpringStrutsDemo/message.action





It Produces the above output.

This Project is available in Git here.

Happy Learning!!!!

1 comment:
Write comments
  1. Hi, Im trying to test your code. I generated the war file and deployed in to tomcat. But I get the 404 Error ( The requested resource [/SpringStrutsDemo/message.action] is not available) When i try to navigate to the url http://localhost:8080/SpringStrutsDemo/message.action

    ReplyDelete