"It's okay to make it an app already"
With that said, we will take on the challenge of upgrading the Othello program, which could only be executed on the console, to a Web application. This time, we will create a sample level project using Spring Boot, which is said to be easy to build a web application in the Spring Framework.
In addition, there is almost no technical explanation, and the procedure of the actual work is described as a memorandum. I referred to various books and websites, but ~~ Black magic ~~ Since there is no black box feeling, I will proceed with the policy of "just move" as with Git related.
There seem to be various ways to create a Spring Boot project, but since I have been developing in Eclipse so far, I will take the method of additionally installing STS (Spring Tool Suite) in Eclipse.
src / main / java folder → MyOthelloAppApplication.java is created in the com.example.demo folder.
package com.myothello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyOthelloAppApplication {
public static void main(String[] args) {
SpringApplication.run(MyOthelloAppApplication.class, args);
}
}
The line starting with @ above the MyOthelloAppApplication class is called annotation, which specifies various roles in the app. The Spring Boot Application above means that it is an application that starts with Spring Boot.
I will try it. When I right-clicked on the name of MyOthelloApp and ran it as a Spring Boot application, the following message (excerpt) appeared on the console.
***************************
APPLICATION FAILED TO START
***************************
Description:
The Tomcat connector configured to listen on port 8080 failed to start.
The port may already be in use or the connector may be misconfigured.
Since Tomcat has already used port 8080, it's unpleasant, so I'll ask Google University teacher. A page that says that you should kill the process using this 8080 (here I found tomcat-connector-configured-to-listen-on)), so I will refer to it here. From the command prompt
netstat -ao | find "8080"
Enter. Then, the PID using 8080 was searched, and 10572 came out this time, so next
Taskill /PID 10572 /F
Enter. Then, it is said that it was forcibly terminated, so when I tried it again, it was no longer said that APPLICATION FAILED TO START. If you access the browser by typing "localhost: 8080" in this state,
Was displayed. Since we haven't decided on the content of the page (Controller) yet, the 404 error is natural. This is OK now.
Let's display "Hallo world" and "Hello othello" in the web application.
Web apps in Spring Boot are designed with the MVC architecture. M stands for Model (data used in the application), V stands for View (screen display), and C stands for Controller (process control). First, create a Controller.
Create MyOthelloAppController.java in the same directory as MyOthelloAppApplication.java. At that time, check "Inherited abstract method". Next, write the code that displays "Hello othello" in the MyOthelloAppController class.
package com.myothello;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyOthelloAppController {
@RequestMapping("/")
public String hello() {
return "Hello othello";
}
}
If you run it again and access 8080 ...
I did it.
With that said, we are ready to develop an Othello program that runs on the Web, the original Othello app (probably). We will use various classes created for the console so far, and first implement Controller mainly.
Thank you for reading!
Recommended Posts