Java web application development environment construction with VS Code (struts2)

I want to prepare a development environment so that I can develop Java Web applications with VS Code. I want to compile and execute what I have developed from VS Code. I also want to debug. I also want to test. I researched various things and had a hard time because I am a beginner in everything I use, I managed to complete the executable environment, so I will summarize it. I hope it helps anyone who wants to do something similar.

Although not introduced in this article, you can also debug using Tomcat.

Operating environment

・ MacOS Catalina 10.15.2 -Visual Studio Code 1.41.1 -AdoptOpenJDK 1.8.0_232 ・ Maven 3.6.3 ・ Tomcat 9.0.30

JAVA_HOME setting

I used to use JDK 1.8.0_221 provided by Oracle. I installed OpenJDK 1.8.0_232 provided by Adopt at this timing, so first pass the path to use it.

Terminal


$ open ~/.bash_profile

Open .bash_profile that describes the environment variables.

.bash_profile


export PATH=$PATH:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Describe and save the PATH and JAVA_HOME settings.

Terminal


$ source ~/.bash_profile

Reflect the changes in .bash_profile.

Terminal


$ javac -version
$ java -version

After restarting the terminal, check if the settings are reflected. ... I think it's OK if the set jdk is displayed.

Apache tomcat download

Unzip the compressed file downloaded from the URL below and place it in any location. I have it under Applications. http://tomcat.apache.org/

Apache Maven download

Unzip the compressed file downloaded from the URL below and place it in any location. I have it under Applications. https://maven.apache.org/download.cgi

Install VS Code extension

■ Java Extension Pack (The following extensions are grouped together) ・ Language Support for Java ™ by Red Hat ・ Debugger for Java ・ Java Test Runner ・ Maven for Java -Java Dependency Viewer ・ Visual Studio IntelliCode ■Tomcat For Java ■ Generator Junit File (I included it because I wanted to create a Junit file with just one click.)

Setting JAVA_HOME with VS Code

Set up VS Code to use Java. スクリーンショット 2020-01-13 18.27.09.png Press F1 and enter it in the search box as shown in the image.

スクリーンショット 2020-01-13 18.27.36.png Select ʻUser Setting` in java.home スクリーンショット 2020-01-13 18.31.13.png Select edit in settings.json スクリーンショット 2020-01-13 18.32.04.png Write the location of JAVA_HOME after `" java.home ":` and restart VS Code

Creating a struts2 project with Maven

Terminal


$ mkdir MavenProject
$ cd MavenProject
$ mvn archetype:generate

Creating a folder for creating an application Move to the created folder Command to create maven project

Terminal


...........abridgement
2545: remote -> us.fatehi:schemacrawler-archetype-maven-project (-)
2546: remote -> us.fatehi:schemacrawler-archetype-plugin-command (-)
2547: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
2548: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
2549: remote -> ws.osiris:osiris-archetype (Maven Archetype for Osiris)
2550: remote -> xyz.luan.generator:xyz-gae-generator (-)
2551: remote -> xyz.luan.generator:xyz-generator (-)
2552: remote -> za.co.absa.hyperdrive:component-archetype (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1467:

Enter struts2

Terminal


...........abridgement
: Choose archetype:
1: remote -> br.com.address.archetypes:struts2-archetype (an archetype web 3.0 + struts2 (bootstrap + jquery) + JPA 2.1 with struts2 login system)
2: remote -> br.com.address.archetypes:struts2-base-archetype (An Archetype with JPA 2.1; Struts2 core 2.3.28.1; Jquery struts plugin; Struts BootStrap plugin; json Struts plugin; Login System using Session and Interceptor)
3: remote -> com.amazonaws.serverless.archetypes:aws-serverless-struts2-archetype (-)
4: remote -> com.jgeppert.struts2.jquery:struts2-jquery-archetype-base (This Archetype provides a Webapp Configuration ready for the Struts2 jQuery Plugin.)
5: remote -> com.jgeppert.struts2.jquery:struts2-jquery-archetype-mobile (This Archetype provides a Webapp Configuration ready for the Struts2 jQuery Mobile Plugin.)
6: remote -> com.jgeppert.struts2.jquery:struts2-jquery-bootstrap-archetype-grid (This Archetype provides a Webapp Configuration ready for the Struts2 jQuery Grid Plugin and the Struts2
        Bootstrap Plugin.)
7: remote -> org.apache.struts:struts2-archetype-angularjs (-)
8: remote -> org.apache.struts:struts2-archetype-blank (-)
9: remote -> org.apache.struts:struts2-archetype-convention (-)
10: remote -> org.apache.struts:struts2-archetype-dbportlet (-)
11: remote -> org.apache.struts:struts2-archetype-plugin (-)
12: remote -> org.apache.struts:struts2-archetype-portlet (-)
13: remote -> org.apache.struts:struts2-archetype-starter (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :8

Enter 8 to ENTER

Terminal


Choose org.apache.struts:struts2-archetype-blank version:
1: 2.2.1
2: 2.2.1.1
3: 2.2.3
4: 2.2.3.1
5: 2.3.3
6: 2.3.4
7: 2.3.4.1
8: 2.3.7
9: 2.3.8
10: 2.3.12
11: 2.3.14
12: 2.3.14.1
13: 2.3.14.2
14: 2.3.14.3
15: 2.3.15
16: 2.3.15.1
17: 2.3.15.2
18: 2.3.15.3
19: 2.3.16
20: 2.3.16.1
21: 2.3.16.2
22: 2.3.16.3
23: 2.3.20
24: 2.3.20.1
25: 2.3.20.3
26: 2.3.24
27: 2.3.24.1
28: 2.3.24.3
29: 2.3.28
30: 2.3.28.1
31: 2.3.29
32: 2.3.30
33: 2.3.31
34: 2.3.32
35: 2.3.33
36: 2.3.34
37: 2.3.35
38: 2.3.36
39: 2.3.37
40: 2.5-BETA1
41: 2.5-BETA2
42: 2.5-BETA3
43: 2.5
44: 2.5.1
45: 2.5.2
46: 2.5.5
47: 2.5.14
Choose a number: 47:39

Enter the version number of struts2 and ENTER

Terminal


Define value for property 'groupId': Hello
Define value for property 'artifactId': exsample
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' Hello: :
Confirm properties configuration:
groupId: Hello
artifactId: exsample
version: 1.0-SNAPSHOT
package: Hello
 Y: :

groupId ... Specify the name of a Java package. artifactId ... Specify the project name arbitrarily. version ... as it is ENTER package ... as it is ENTER Y:: ... ENTER as it is

Terminal


[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: struts2-archetype-blank:2.3.37
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: Hello
[INFO] Parameter: artifactId, Value: exsample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: Hello
[INFO] Parameter: packageInPathFormat, Value: Hello
[INFO] Parameter: package, Value: Hello
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: Hello
[INFO] Parameter: artifactId, Value: exsample
[INFO] Project created from Archetype in dir: /MavenProject/exsample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12:24 min
[INFO] Finished at: 2020-01-13T18:54:18+09:00
[INFO] ------------------------------------------------------------------------

Maven project creation completed

Directory structure of the created project

MavenProject/
└── exsample
    ├── pom.xml
    └── src
        ├── main
        │   ├── java
        │   │   └── Hello
        │   │       └── example
        │   │           ├── ExampleSupport.java
        │   │           ├── HelloWorld.java
        │   │           └── Login.java
        │   ├── resources
        │   │   ├── Hello
        │   │   │   └── example
        │   │   │       ├── Login-validation.xml
        │   │   │       ├── package.properties
        │   │   │       └── package_es.properties
        │   │   ├── example.xml
        │   │   ├── log4j2.xml
        │   │   └── struts.xml
        │   └── webapp
        │       ├── WEB-INF
        │       │   ├── example
        │       │   │   ├── HelloWorld.jsp
        │       │   │   ├── Login.jsp
        │       │   │   ├── Menu.jsp
        │       │   │   ├── Missing.jsp
        │       │   │   ├── Register.jsp
        │       │   │   └── Welcome.jsp
        │       │   └── web.xml
        │       └── index.html
        └── test
            └── java
                └── Hello
                    └── example
                        ├── ConfigTest.java
                        ├── HelloWorldTest.java
                        └── LoginTest.java

Even if you build and run it as it is, the page prepared by default is displayed, but this time we will create a simple Hello World application using struts2.

Open the created project with VS Code

スクリーンショット 2020-01-13 19.30.12.png Add the created exsample project folder to your workspace. スクリーンショット 2020-01-13 20.32.34.png

Added.

Create a simple Hello World application

Change the contents of the project to a simple Hello World display application.

Project structure


MavenProject
└── exsample
    ├── pom.xml
    └── src
        ├── main
        │   ├── java
        │   │   └── Hello
        │   │       └── example
        │   │           └── HelloStrutsAction.java
        │   ├── resources
        │   │   └── struts.xml
        │   └── webapp
        │       ├── WEB-INF
        │       │   └── web.xml
        │       ├── hello.jsp
        │       └── index.jsp
        └── test
            └── java
                └── Hello
                    └── example

HelloStrutsAction.java


package Hello.example;
import com.opensymphony.xwork2.ActionSupport;

public class HelloStrutsAction extends ActionSupport{
  /**
   *
   */
  private static final long serialVersionUID = 1L;

  public String execute() {
	  return SUCCESS;
  }
}

struts.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">

  <struts>
    <constant name = "struts.devMode" value = "true"/>
    <package name = "Hello.example" extends = "struts-default">
      <action name = "HelloStrutsAction" class = "Hello.example.HelloStrutsAction" method = "execute">
        <result name = "success">hello.jsp</result>
      </action>

    </package>
  </struts>

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>WebProj</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern> 
  </filter-mapping>
</web-app>

hello.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>HelloStruts</title>
</head>
<body>
  <h2>HelloStruts2!</h2>
</body>
</html>

index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>INDEX</title>
</head>
<body>
  <s:form action = "HelloStrutsAction">
    <s:submit value = "HelloStruts"/>
  </s:form>

</body>
</html>

TOMCAT SERVERS settings

スクリーンショット 2020-01-13 19.31.31.png If you select the save destination of Tomcat from the + button, you can set the Tomcat server as shown below. スクリーンショット 2020-01-13 19.32.18.png

pom.xml settings

In Maven, the definition information of the project is written in the configuration file called pom.xlm. Change the settings a little.

pom.xlm


···············abridgement
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
      <version>3.3</version>
      <configuration>
        <encoding>UTF-8</encoding>
          <source>1.5</source>
          <target>1.5</target>
      </configuration>
  </plugin>
···············abridgement

By default, the JDK you use is set to jdk1.5.

pom.xlm


···············abridgement
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
      <version>3.3</version>
      <configuration>
        <encoding>UTF-8</encoding>
          <source>1.8</source>
          <target>1.8</target>
      </configuration>
  </plugin>
···············abridgement

This time I will change it to use jdk1.8.

pom.xlm


···············abridgement
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
      <version>5.1.48</version>
  </dependency>
···············abridgement

You don't need it this time, but to include other plugins such as MySQL, write it like this.

MAVEN PROJECTS settings

スクリーンショット 2020-01-13 19.49.21.png Do maven install. The plugins defined in pom.xml will be installed.

compile

スクリーンショット 2020-01-13 19.51.01.png Compile.

Creating a package

スクリーンショット 2020-01-13 19.51.18.png A war file is created.
MavenProjct
└── exsample
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── Hello
    │   │   │       └── example
    │   │   │           └── HelloStrutsAction.java
    │   │   ├── resources
    │   │   │   ├── log4j2.xml
    │   │   │   └── struts.xml
    │   │   └── webapp
    │   │       ├── WEB-INF
    │   │       │   └── web.xml
    │   │       ├── hello.jsp
    │   │       └── index.jsp
    │   └── test
    │       └── java
    │           └── Hello
    │               └── example
    └── target
        ├── classes
        │   ├── Hello
        │   │   └── example
        │   │       └── HelloStrutsAction.class
        │   ├── log4j2.xml
        │   └── struts.xml
        ├── exsample-1.0-SNAPSHOT
        │   ├── META-INF
        │   ├── WEB-INF
        │   │   ├── classes
        │   │   │   ├── Hello
        │   │   │   │   └── example
        │   │   │   │       └── HelloStrutsAction.class
        │   │   │   └── struts.xml
        │   │   ├── lib
        │   │   │   ├── asm-3.3.jar
        │   │   │   ├── asm-commons-3.3.jar
        │   │   │   ├── asm-tree-3.3.jar
        │   │   │   ├── commons-fileupload-1.4.jar
        │   │   │   ├── commons-io-2.2.jar
        │   │   │   ├── commons-lang3-3.2.jar
        │   │   │   ├── freemarker-2.3.28.jar
        │   │   │   ├── javassist-3.11.0.GA.jar
        │   │   │   ├── log4j-api-2.3.jar
        │   │   │   ├── log4j-core-2.3.jar
        │   │   │   ├── ognl-3.0.21.jar
        │   │   │   ├── struts2-config-browser-plugin-2.3.37.jar
        │   │   │   ├── struts2-core-2.3.37.jar
        │   │   │   └── xwork-core-2.3.37.jar
        │   │   └── web.xml
        │   ├── hello.jsp
        │   └── index.jsp
        ├── exsample-1.0-SNAPSHOT.war
        ├── generated-sources
        │   └── annotations
        ├── maven-archiver
        │   └── pom.properties
        └── maven-status
            └── maven-compiler-plugin
                ├── compile
                │   └── default-compile
                │       ├── createdFiles.lst
                │       └── inputFiles.lst
                └── testCompile
                    └── default-testCompile
                        └── inputFiles.lst

When all is done, it will change to the above project structure.

Project execution

スクリーンショット 2020-01-13 20.51.53.png Right-click on the war file and select `Run on Tomcat Server`. スクリーンショット 2020-01-13 20.53.56.png

Right-click on Tomcat and select ʻOpen in Browser` to open the screen below in your browser.

スクリーンショット 2020-01-13 20.54.04.png

Click the project name.

スクリーンショット 2020-01-13 20.54.35.png

index.jsp is displayed! Clicking the "Hello Struts" button ...

スクリーンショット 2020-01-13 20.55.46.png

You have transitioned to hello.jsp!

Recommended Posts

Java web application development environment construction with VS Code (struts2)
[Environment construction] Build a Java development environment with VS Code!
Prepare Java development environment with VS Code
Build a Java development environment with VS Code
Introduction to Java development environment & Spring Boot application created with VS Code
Build Java development environment with VS Code on Mac
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
Java development environment (Mac, VS Code)
Web application development environment construction in Java (for inexperienced people)
java development environment construction
[Jakarta EE 8 application development with Gradle] 1. Environment construction
Creating a java web application development environment with docker for mac part1
Create a java web application development environment with docker for mac part2
Spring5 MVC Web application development with Visual Studio Code Environment construction (Installation of JDK11, Maven, Tomcat, Visual Studio Code)
[Java] Environment construction procedure for developing struts 1.3 with Eclipse
Build Java program development environment with Visual Studio Code
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
Environment construction for Servlet application development
Java build with mac vs code
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~
Build WebAPP development environment with Java + Spring with Visual Studio Code
[Java development environment construction] Install OpenJDK 11 (Java 11) on macOS with Homebrew.
Comparison of WEB application development with Rails and Java Servlet + JSP
Let me do VS Code Remote Development + Java development in Proxy environment
[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
[Processing x Java] Construction of development environment
Laravel development environment construction with Docker (Mac)
Java development environment construction memo on Mac
Start web application development with Spring Boot
Spring Boot + Docker Java development environment construction
Java application development environment created in VM environment
[Java] Environment construction
Java environment construction
Java development environment
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 1/3 [Preparation]
Spring Boot2 Web application development with Visual Studio Code SQL Server connection
Spring5 MVC web application development with Visual Studio Code SQL Server connection
Spring5 MVC Web application development with Visual Studio Code Maven template creation
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
Using Gradle with VS Code, build Java → run
Create Spring Boot environment with Windows + VS Code
Try debugging a Java program with VS Code
Wordpress local environment construction & development procedure with Docker
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 2/3 [Page creation 1/2]
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 3/3 [Page creation 2/2]
Lombok with VS Code
Java development environment memo
Build VS Code + WSL + Java + Gradle environment from scratch
Java development environment construction on Mac-JDK Install (2020 preservation version)
BEAR application Docker development environment construction example (docker-sync, Mutagen)
Experienced Java users get started with Android application development
Stable development environment construction manual for "Rails6" with "Docker-compose"
[Tutorial] Download Eclipse → Run Web application with Java (Pleiades)
Introduce JavaFX 15 and do GUI development with VS Code
Roughly the flow of web application development with Rails.
Beginners create Spring Tools Suite environment with VS Code
The procedure I did when I prepared the environment of gradle + Java with VS Code (Windows 10)
[For beginners] Until building a Web application development environment using Java on Mac OS