Showing posts with label struts. Show all posts
Showing posts with label struts. Show all posts

Thursday 17 June 2021

Deploying SpringBoot Struts 2 Integration Project in Docker

 Follow my previous post and create the sample spring struts integration project from here.


Create a Docker File: Dockerfile


FROM tomcat:latest

ADD target/SpringStrutsDemo-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/

EXPOSE 8080

CMD ["catalina.sh","run"]


Build and tag the Docker File:spring-strutsdemo


C:\Users\Syed\Spring-workspace\SpringStrutsDemo>docker build -t spring-strutsdemo .

Sending build context to Docker daemon  58.75MB

Step 1/4 : FROM tomcat:latest

 ---> 5505f7218e4d

Step 2/4 : ADD target/SpringStrutsDemo-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/

 ---> a30a842ce761

Step 3/4 : EXPOSE 8080

 ---> Running in 35d616d2803f

Removing intermediate container 35d616d2803f

 ---> 2c848691227a

Step 4/4 : CMD ["catalina.sh","run"]

 ---> Running in 270c9c8d4b5d

Removing intermediate container 270c9c8d4b5d

 ---> f7b915b47c1f

Successfully built f7b915b47c1f

Successfully tagged spring-strutsdemo:latest


Run the docker image from the tag 


docker run -p 8080:8080 spring-strutsdemo

This will start in 8080 port. In case if it is not started well there could be an issue with the java version used in the docker or the project is not properly generated and war is invalid.

Access it using 

http://localhost:8080/SpringStrutsDemo-0.0.1-SNAPSHOT/message.action

where SpringStrutsDemo-0.0.1-SNAPSHOT is the context. 





Happy Learning!!!!



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!!!!