As the title says, I had the opportunity to experience Java at work for the first time in 10 years, so I will study it myself now. It's a study of an existing system in operation, so it's not the latest tech stack, but a slightly older combination.
Don't deploy the web app, just run it locally.
Java was included before, so I'll just use it. Java 8 instead of 14 or 11 due to adult circumstances.
$ java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
Since it is mac for the time being, put Maven with brew.
$ brew install maven
(Abbreviation)
$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
(Abbreviation)
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"
Also installed IntelliJ IDEA Community. Since I entered it with GUI, the method is omitted.
Change the settings according to your preference. This was helpful. https://www.karakaram.com/intellij-idea-always-do-things/
I basically use it as it is, but it's not good for mental health, so I just played with Keymap a little (especially C-h, C-d).
For the time being, create an empty project on GitHub and clone it. This is the working directory.
$ git clone https://github.com/sekikatsu36/spring-sample.git
$ cd spring-sample
$ git config --local user.name "sekikatsu36"
$ git config --local user.email "(Abbreviation)"
Create a new Maven project in IntelliJ. Leave the default settings without changing any settings.
 
This completes the initial state.
$ ls -a
.			.DS_Store		.gitignore		pom.xml			src
..			.git			.idea			spring-sample.iml
There are many articles about spring boot on the Web, and there are few articles about spring framework. However, I need spring framework now, so I modified pom.xml to make it usable.
This is the completed form.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example.api</groupId>
    <artifactId>spring-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--  pom.Allow the version to be handled as a variable in xml-->
        <spring.version>5.2.5.RELEASE</spring.version>
        <jetty.version>9.4.11.v20180605</jetty.version>
    </properties>
    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- jetty -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.14.v20181114</version>
            </plugin>
        </plugins>
    </build>
</project>
At the beginning, I wrote only properties and dependencies. Other points will be described later.
In my environment, I got an error that ʻorg.springframework.spring-webmvc: 5.2.5.RELEASE` could not be found. It exists in Repository.
Do the following
mvn clean install
result
--The log shows Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/5.2.5.RELEASE/spring-webmvc-5.2.5.RELEASE.jar Was done
--/Users/(username)/.m2/repository/org/springframework/spring-webmvc/5.2.5.RELEASE has been added
--In IntelliJ, the error was still displayed, but when I reloaded Maven, the error disappeared.
Basically, you can do it according to https://qiita.com/kasa_le/items/6aaf17823db67c951fb0.
However, since this article is ʻIntelliJ IDEA Ultimate, in ʻIDEA Community, [Create View](https://qiita.com/kasa_le/items/6aaf17823db67c951fb0#view%E3%81%AE%E4%BD% After 9C% E6% 88% 90), it doesn't work as it is.
For the time being, the process of creating the controller and the view folder is completed in this way. The following is done.
/src/main
  /java/org/example/controllers
    IndexController.java
  /webapp/WEB-INF
    /views
    web.xml
    dispatcher-servlet.xml
In the case of Community edition, it seems that JSP / JSPX does not appear when creating a new file because there is no Spring support. https://www.jetbrains.com/ja-jp/idea/features/editions_comparison_matrix.html
So, normally select File and make everything yourself.
index.jsp
<%@ page import="org.example.controllers.IndexController" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Spring Test Page</title>
    </head>
    <body>
        <p>${someAttribute}</p>
    </body>
</html>
Use Jetty instead of Tomcat for the convenience of adults. Ultimate (paid version) of IntelliJ IDEA seems to have Tomcat built in by default (?), But since it is a community, it can not be used anyway. Jetty doesn't need to be installed in advance, so it's easy.
With reference to http://zetcode.com/spring/jetty/, I added the following to pom.xml.
   <packaging>war</packaging>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.14.v20181114</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
In this state
mvn jetty:run
When you execute and access http://0.0.0.0:8080, Hello World is displayed safely.

If you do not specify the source and target versions with maven-compiler-plugin, it seems to be 5, and an error will occur. http://orionb312.hatenablog.com/entry/2018/03/06/233734
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project spring-sample: Compilation failure: Compilation failure: 
[ERROR]Source option 5 is not currently supported. Please use 7 or later.
[ERROR]Target option 5 is not currently supported. Please use 7 or later.
The finished product is below. https://github.com/sekikatsu36/spring-sample
For the time being, I was able to confirm that Spring works. I want to do a little more.
--I want to use an API that is not a WebPage (use Jersey?) --I want to be able to debug (now it doesn't stop at breakpoints) ――I want to use PaaS or SaaS
Recommended Posts