> # SpringBoot 3/24 上課內容
###### tags: `SpringBoot`
- [ ]
# 2 Spring Boot 動態網站開發實務

[spring-boot](https://spring.io/projects/spring-boot)

* Tomcat不能用jar專案
# 3 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

[Spring tools](https://spring.io/tools)
* 好用[intellij](https://www.jetbrains.com/idea/)
# 4 Spring Boot 動態網站開發實務

[java sdk](http://java.sun.com/)
## Spring Boot 動態網站開發實務

# 5 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 6 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 7 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 8 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 9 Spring Boot 動態網站開發實務

# 10 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

[Spring](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-build-systems)
# 11 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 12 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 13 Spring Boot 動態網站開發實務

# 14 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

* 這個寫法 jsp不能用
# 15 Spring Boot 動態網站開發實務


# 16 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 17 Spring Boot 動態網站開發實務

* provided = 由系統提供
## Spring Boot 動態網站開發實務

* 測試程式用的
# 18 Spring Boot 動態網站開發實務

* 可改成jar黨或war

* 現在使用的

## Spring Boot 動態網站開發實務

# 19 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務


# 20 Spring Boot 動態網站開發實務

* 不能放的位子

* domain name = 網址反過來寫
## Spring Boot 動態網站開發實務

# 21 Spring Boot 動態網站開發實務

* 從外部帶入參數
## Spring Boot 動態網站開發實務

# 22 Spring Boot 動態網站開發實務


* ports 0~65535除2=正跟負
* 系統ports 0~1023
* 網址通訊埠 預設80 就不會顯示

## Spring Boot 動態網站開發實務

# 23 Spring Boot 動態網站開發實務
* 2008

## Spring Boot 動態網站開發實務

# 24 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 25 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 26 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 27 Spring Boot 動態網站開發實務

# 28 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 29 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 30 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# <font color="green">------------實作部份------------</font>
#### springbootWorkspace

















#### 檢查環境設定


#### cmd
mvn
mvn -help

#### 建立專案 DemoSpringBootWebProject

* 可透過網路建立



* (jre)是預設






* 白色是在用的 灰色是目前沒用到的


### package
* **tw.leonchen**
* controller
* model
* config
* product
* model
* controller
* order
* model
* controller
#### HelloController.java

```clike=
package tw.leonchen.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(path="/hello.controller", method = RequestMethod.GET)
public String processAction() {
return "hello, Controller";
}
}
```
### 如果不能執行

### 實作組態檔

#### application.properties
```clike=
#Server port
#server.port=8081
#data setup資料設定
name=mary
age=18
#profiles information
profiles.username=alice
profiles.address=USA
profiles.phone=N/A
```
### Person.java

```clike=
package tw.leonchen.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Person {
@Value("${name}")
private String name;
@Value("${age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
#### PersonController.java
```clike=
package tw.leonchen.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tw.leonchen.model.Person;
@RestController
public class PersonController {
@Autowired
private Person person;
@GetMapping(path = "/person.controller")
public Person processAction() {
return person;
}
}
```
#### application.yml

```clike=
#Server port
server:
port: 8082
#data setup資料設定
name: mary
age: 20
#profiles information
profiles:
username: louis
address: Taiwan
phone: "0987654321"
```
#### PersonalProfiles.java
```clike=
package tw.leonchen.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "profiles")
public class PersonalProfiles {
private String username;
private String address;
private String phone;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```

#### PersonalProfilesController.java
```clike=
package tw.leonchen.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tw.leonchen.model.PersonalProfiles;
@RestController
public class PersonalProfilesController {
@Autowired
private PersonalProfiles pp1;
@GetMapping("/personalProfiles.controller")
public PersonalProfiles processAction() {
return pp1;
}
}
```
## 看優先權

#### PathVaribleController.java
```clike=
package tw.leonchen.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PathVaribleController {
@GetMapping("/member/{mid}")
@ResponseBody //true 一定要給值
public String processPathVariableAction(@PathVariable(name = "mid", required = true )String memberId) {
return "memberId:" + memberId;
}
}
```
#### DemoSpringBootWebProjectApplication
* 加入Servlet
```clike=
package tw.leonchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class DemoSpringBootWebProjectApplication {
public static void main(String[] args) {
SpringApplication.run(DemoSpringBootWebProjectApplication.class, args);
}
}
```
#### MyServletfilter.java
```clike=
package tw.leonchen.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter("/")
public class MyServletfilter implements Filter{
@Override
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{
response.setContentType("text/html;charset=UTF=8");
PrintWriter out = response.getWriter();
System.out.println("execute filter ready");
chain.doFilter(request, response);
System.out.println("execute filter finished");
out.close();
}
}
```