1탄에 구현한 서비스를 컨트롤러-View에 연동하는 시간을 갖겠습니다
View에서 mustache를 사용할것이므로
우선 build.gradle에 관련 의존성을 추가하겠습니다
그럼 다음과 같이 구성해보겠습니다
application.properties
CustomerService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.example.demo;
import org.springframework.stereotype.Service;
@Service
public class CustomerService {
@Autowired
CustomerRepository customerRepository;
public Iterable<Customer> findAll() {
return customerRepository.findAll();
}
}
|
CustomerController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.example.demo;
import org.springframework.stereotype.Controller;
@Controller
@RequestMapping("/")
public class CustomerController {
@Autowired
AmazonProperties amazonProperties;
@Autowired
CustomerService customerService;
public ModelAndView main(/* Map<String, Object> model, */
ModelAndView mav) {
String id=amazonProperties.getAssociateId();
Iterable<Customer> list=customerService.findAll();
mav.addObject("dataList", list);
mav.setViewName("main/index");
return mav;
}
}
|
AmazonProperties.java
(사실 이 소스는 필수는 아닙니다. 사용 안하신다면 컨트롤러에서 Autowired를 제거해주세요)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.example.demo;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("amazon")
public class AmazonProperties {
private String associateId;
public void setAssociateId(String associateId) {
this.associateId=associateId;
}
public String getAssociateId() {
return associateId;
}
}
|
error.html
(Spring Boot에서는 에러페이지가 error.html로 자동 매핑되어있습니다)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<span class="oops">Oops!</span><br />
<p>There seems to be a problem with the page you requested</p>
</body>
</html>
|
index.html (우리가 컨트롤러에서 매핑한 View입니다. {{ }} 가 mustache입니다)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
{{dataList}}
</body>
</html>
|
style.css (아시다 싶이. 외부 스타일 시트입니다)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
body {
background-color: #cccccc;
font-family: arial, helvetica, sans-serif;
}
.bookHeadline {
font-size: 12pt;
font-weight: bold;
}
.bookDescription {
font-size: 10pt;
}
label {
font-weight: bold;
}
.error {
color: red;
}
.errorPage {
text-align: center;
}
.oops {
font-size: 76pt;
}
|
여기까지 완성하셨다면 서버를 실행해보겠습니다
그 전에 ~Application.java 소스를 한번 체크하고 가겠습니다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package com.example.demo;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@SpringBootApplication
public class Example2Application implements WebMvcConfigurer, CommandLineRunner {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//welcomePage
//registry.addViewController("/login").setViewName("login");
}
@PersistenceContext
EntityManager entityManager;
@Autowired
CustomerRepository repository;
@Override
public void run(String... args) throws Exception {
//entityManager.createNativeQuery("create table customer (id bigint not null, first_name varchar(255), last_name varchar(255), primary key (id)) engine=InnoDB").executeUpdate();
}
public static void main(String[] args) {
SpringApplication.run(Example2Application.class, args);
}
}
|
Spring Boot App으로 실행하고 나면
다음과 같은 결과를 확인 할 수 있습니다.
2탄은 여기까지 하겠습니다.
이어서 3탄에서는 CURD가 가능한 React View를 구성해보도록 하겠습니다
'웹개발 > server-side' 카테고리의 다른 글
1탄) ASP .NET 웹 개발 환경 세팅 (2) | 2020.03.31 |
---|---|
3탄) spring boot gradle project + JPA Hibernate + MariaDB + React.js 프로젝트 (0) | 2020.03.06 |
1탄) spring boot gradle project + JPA Hibernate + MariaDB + React.js 프로젝트 (0) | 2020.02.28 |
5탄) 전자정부프레임워크 + spring legacy MVC + Oracle 프로젝트 (0) | 2020.02.28 |
4탄) 전자정부프레임워크 + spring legacy MVC + Oracle 프로젝트 (0) | 2020.02.24 |