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
 
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
 
import org.springframework.stereotype.Controller;
 
@Controller
@RequestMapping("/")
public class CustomerController {
    
    @Autowired
    AmazonProperties amazonProperties;
    
    @Autowired
    CustomerService customerService;
    
    @RequestMapping(value = "/react", method=RequestMethod.GET)
    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
 
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>
    <link rel="stylesheet" href="/resources/css/style.css"></link>
</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>
    <link rel="stylesheet" href="/resources/css/style.css"></link>
</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
 
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();
        
        repository.save(new Customer("Jack""Bauer"));
        repository.save(new Customer("Chloe""O'Brian"));
        repository.save(new Customer("Kim""Bauer"));
        repository.save(new Customer("David""Palmer"));
        repository.save(new Customer("Michelle""Dessler"));
    }
    
    public static void main(String[] args) {
        SpringApplication.run(Example2Application.class, args);
    }
}
 

 

Spring Boot App으로 실행하고 나면

 

다음과 같은 결과를 확인 할 수 있습니다.

2탄은 여기까지 하겠습니다. 

이어서 3탄에서는 CURD가 가능한 React View를 구성해보도록 하겠습니다 

+ Recent posts