Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Database
- 코딩도장
- 크롤링 개발
- Machine Learning
- GoogleDrive
- 국가과제
- C언어
- LINUX MASTER
- 인터넷의이해
- Spring
- 고등학생 대상
- Resnet
- colab
- ICT멘토링
- Spring Boot
- rnn
- suricata
- Github
- API
- KAKAO
- OSS
- cloud
- Powershell
- VSCode
- Python
- Docker
- Rocky Linux
- Kubernetes
- git
- ChatGPT
Archives
- Today
- Total
코딩두의 포트폴리오
컨트롤러 수정 본문
컨트롤러에서 HTML 페이지 반환하게끔 수정
src/main/java/com/example/demo/controller
[추가할 메소드 부분]
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
import com.example.demo.entity.TouristSpot;
import com.example.demo.service.TouristSpotService;
@Controller
public class TouristSpotController {
@Autowired
private TouristSpotService touristSpotService;
@GetMapping("/touristspots")
public String getAllTouristSpots(Model model) {
List<TouristSpot> touristSpots = touristSpotService.findAll();
model.addAttribute("touristSpots", touristSpots);
return "index"; // index.html 템플릿을 반환
}
}
새로운 컨트롤러 (TouristSpotHtmlController):
- 새로운 컨트롤러를 추가하여 HTML 템플릿을 제공
- @Controller와 /touristspots 경로를 사용하여 HTML 페이지를 제공
- getAllTouristSpots 메서드는 Model 객체를 사용하여 touristSpots 데이터를 HTML 템플릿에 전달
[기존 메소드]
@Autowired
private TouristSpotService touristSpotService;
@GetMapping
public List<TouristSpot> getAllTouristSpots() {
return touristSpotService.findAll();
}
@GetMapping("/{id}")
public TouristSpot getTouristSpotById(@PathVariable Long id) {
return touristSpotService.findById(id);
}
@PostMapping
public TouristSpot createTouristSpot(@RequestBody TouristSpot touristSpot) {
return touristSpotService.save(touristSpot);
}
@PutMapping("/{id}")
public TouristSpot updateTouristSpot(@PathVariable Long id, @RequestBody TouristSpot touristSpot) {
TouristSpot existingSpot = touristSpotService.findById(id);
if (existingSpot != null) {
existingSpot.setName(touristSpot.getName());
existingSpot.setDescription(touristSpot.getDescription());
return touristSpotService.save(existingSpot);
} else {
return null;
}
}
@DeleteMapping("/{id}")
public void deleteTouristSpot(@PathVariable Long id) {
touristSpotService.deleteById(id);
}
기존 컨트롤러 (TouristSpotController):
- 기존의 RESTful API 엔드포인트를 유지
- @RestController와 /api/touristspots 경로를 사용하여 JSON 데이터를 제공
[기존 코드 유지 및 새로운 메서드 추가]
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.example.demo.entity.TouristSpot;
import com.example.demo.service.TouristSpotService;
@RestController
@RequestMapping("/api/touristspots")
public class TouristSpotController {
@Autowired
private TouristSpotService touristSpotService;
// 기존 RESTful API 메서드들 유지
@GetMapping
public List<TouristSpot> getAllTouristSpots() {
return touristSpotService.findAll();
}
@GetMapping("/{id}")
public TouristSpot getTouristSpotById(@PathVariable Long id) {
return touristSpotService.findById(id);
}
@PostMapping
public TouristSpot createTouristSpot(@RequestBody TouristSpot touristSpot) {
return touristSpotService.save(touristSpot);
}
@PutMapping("/{id}")
public TouristSpot updateTouristSpot(@PathVariable Long id, @RequestBody TouristSpot touristSpot) {
TouristSpot existingSpot = touristSpotService.findById(id);
if (existingSpot != null) {
existingSpot.setName(touristSpot.getName());
existingSpot.setDescription(touristSpot.getDescription());
return touristSpotService.save(existingSpot);
} else {
return null;
}
}
@DeleteMapping("/{id}")
public void deleteTouristSpot(@PathVariable Long id) {
touristSpotService.deleteById(id);
}
}
// HTML 템플릿을 제공하는 새로운 컨트롤러 추가
@Controller
@RequestMapping("/touristspots")
public class TouristSpotHtmlController {
@Autowired
private TouristSpotService touristSpotService;
@GetMapping
public String getAllTouristSpots(Model model) {
List<TouristSpot> touristSpots = touristSpotService.findAll();
model.addAttribute("touristSpots", touristSpots);
return "index"; // index.html 템플릿을 반환
}
}
'2024 Tourism Data Utilization Contest > Spring Boot' 카테고리의 다른 글
HTML 파일 생성 (0) | 2024.06.20 |
---|---|
Thymeleaf dependency 추가 (0) | 2024.06.20 |
데이터 로더 (0) | 2024.06.20 |
설정 파일 (0) | 2024.06.20 |
컨트롤러 (0) | 2024.06.20 |