1
0

More than 3 years have passed since last update.

[mac][Eclipse]jdk10でJava Springを動かす

Last updated at Posted at 2020-04-09

Spring MVCとSpring bootをmacで動かす。
かなり飛ばして書いてる。

前提

eclipseとMaven使った。
https://mergedoc.osdn.jp/
java EEをインストール。SEの方はサーバーサイドがなくて、swingとかしか使えない。
https://codeaid.jp/eclipse-java-mac/

ヘルプタブからマーケットプレイスでプラグインをインスストール。
Eclipse でマーケットプレイスが無い場合に追加する方法

eclipseでMavenを動かす
https://qiita.com/tarosa0001/items/e5667cfa857529900216

Spring boot

Spring boot環境設定

今回はSpring Tool Suiteを直接は使わないことにした。

eclipseにマーケットプレースからアドインでspring boot用のtoolをインストールする。

Spring bootハローワールド

イニシャライザーでも生成できると思う。
https://start.spring.io/

Mavenでも生成できると思う。
https://spring-boot-camp.readthedocs.io/ja/latest/01-HelloWorld.html

サンプル実装1

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    @RequestMapping("/")
    String hello() {
        return "Hello Spring Boot!";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

でアップ
http://localhost:8080/

サンプル実装2

Spring MVC

あれ。port埋まってる?って思ったら確認して消すか検討。

lsof -i :8080
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── spring
│   │   │           └── mvc
│   │   │               ├── GreetController.java
│   │   │               └── HomeController.java
│   │   ├── resources
│   │   │   ├── META-INF
│   │   │   └── log4j.xml
│   │   └── webapp
│   │       ├── WEB-INF
│   │       │   ├── classes
│   │       │   ├── spring
│   │       │   │   ├── appServlet
│   │       │   │   │   └── servlet-context.xml
│   │       │   │   └── root-context.xml
│   │       │   ├── views
│   │       │   │   ├── greet.jsp
│   │       │   │   └── home.jsp
│   │       │   └── web.xml
│   │       └── resources
│   └── test
│       ├── java
│       │   └── com
│       │       └── spring
│       │           └── mvc
│       └── resources
│           └── log4j.xml
└── target
    ├── classes
    │   ├── com
    │   │   └── spring
    │   │       └── mvc
    │   │           ├── GreetController.class
    │   │           └── HomeController.class
    │   └── log4j.xml
    ├── m2e-wtp
    │   └── web-resources
    │       └── META-INF
    │           ├── MANIFEST.MF
    │           └── maven
    │               └── com.spring
    │                   └── mvc
    │                       ├── pom.properties
    │                       └── pom.xml
    └── test-classes
        ├── com
        │   └── spring
        │       └── mvc
        └── log4j.xml
GreetController.java
package com.spring.mvc;

import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * Handles requests for the application home page.
 */
@Controller
public class GreetController {


    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/greet", method = RequestMethod.GET)
    public String greet(Locale locale, Model model) {
        model.addAttribute("str1", "iiiii");
        return "greet";
    }

}

greet.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Greet</title>
</head>
<body>
    <P>  The time on the server is ${str1}. </P>
</body>
</html>

実行するにはeclipseとtomcatを繋げないといけない。
tomcatをダウンロードして、mysqlをJDBCで繋いで、tomcatのフォルダをeclipseに設定する。
ここ参考

2.3. はじめてのSpring MVCアプリケーション
https://terasolunaorg.github.io/guideline/public_review/Overview/FirstApplication.html

Serving Web Content with Spring MVC
https://spring.io/guides/gs/serving-web-content/

Spring MVCでJava Config
https://qiita.com/HiroyaEnd/items/17175e947911d84c1b3b

http://ponsuke-tarou.hatenablog.com/entry/2018/03/01/223627
http://ponsuke-tarou.hatenablog.com/entry/2018/03/04/140035

サンプル
https://qiita.com/neriai/items/6b27fba2aa1af7b60bb8
https://sites.google.com/site/soracane/home/springnitsuite/spring-mvc/05-shi-jinosanpuru
http://terasolunaorg.github.io/guideline/5.5.1.RELEASE/ja/Overview/FirstApplication.html
https://qiita.com/t-shin0hara/items/c7d08aaef24fcdd64a28

IntelliJの時

[Ubuntu18/mac][IntelliJ]jdk10でtomcat9を立てる
https://qiita.com/miyamotok0105/items/3f5e429d80cfe2ca3f32

トラブルシューティング

ポートが埋まってる時

lsof -i -P | grep 1080

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0