HomeController.java
package com.ll.sb1114prac;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
public class HomeController {
@GetMapping("/") // @GetMapping -> 액션 메서드
@ResponseBody // return 값을 그대로 브라우저에 전송
String showMain() {
return "안녕하세요";
}
@GetMapping("/about")
@ResponseBody
String showAbout() {
return "개발자 커뮤니티";
}
@GetMapping("/calc")
@ResponseBody
String showCalc(Integer a, Integer b) {
return "계산 결과 : %d".formatted(a + b);
}
@GetMapping("/calc2")
@ResponseBody
String showCalc2(Integer a, Integer b) {
return "a:"+a+" b:"+b;
}
@GetMapping("/calc3")
@ResponseBody
String showCalc3(
@RequestParam(defaultValue = "0") int a,
@RequestParam(defaultValue = "0") int b
) {
return "계산 결과 : %d".formatted(a + b);
}
@GetMapping("/calc4")
@ResponseBody
String showCalc4(
@RequestParam(defaultValue = "0") double a,
@RequestParam(defaultValue = "0") double b
) {
return "계산 결과 : %f".formatted(a + b);
}
@GetMapping("/calc5")
@ResponseBody
String showCalc5(
@RequestParam(defaultValue = "0") String a,
@RequestParam(defaultValue = "0") String b
) {
return "계산 결과 : %s".formatted(a + b);
}
@GetMapping("/calc6")
@ResponseBody
int showCalc6(
int a, int b
) {
return a + b;
}
@GetMapping("/calc7")
@ResponseBody
boolean showCalc7(
int a, int b
) {
return a > b;
}
@GetMapping("/calc8")
@ResponseBody
Person showCalc8(
String name, int age
) {
return new Person(name, age);
}
@GetMapping("/calc9")
@ResponseBody
Person2 showCalc9(
String name, int age
) {
return new Person2(name+"2", age);
}
@GetMapping("/calc10")
@ResponseBody
Map<String, Object> showCalc10(
String name, int age
) {
Map<String, Object> personMap = Map.of(
"name", name,
"age", age // 뭐지??
// Map.of은 불변(immutable)한 Map을 생성하는 메서드
// 거기서 key-value 쌍을 만드는 방법임
);
return personMap;
}
@GetMapping("/calc11")
@ResponseBody
List<Integer> showCalc11() {
List<Integer> nums = new ArrayList<>() {{
add(10);
add(-2344);
add(3403);
}}; // 왜 중괄호 2개?? -> 더블 블레이스 초기화 (익명 클래스 + 생성자)
// ArrayList 초기화: 외부 중괄호 {}는 ArrayList 상속받은 익명 클래스 생성.
// => 익명 클래스에서 인스턴스를 생성 동시에 객체 초기화 수행 가능
//초기화 블록: 내부 중괄호 {}는 익명 클래스의 초기화 블록.
// => 익명 클래스의 인스턴스 생성과 동시에 추가적인 작업 수행 가능
return nums;
}
@GetMapping("/calc12")
@ResponseBody
int[] showCalc12() {
int[] nums = new int[]{10, -23423, 234};
return nums;
}
@GetMapping("/calc13")
@ResponseBody
List<Person2> showCalc13(
String name, int age
) {
List<Person2> persons = new ArrayList<>() {{
add(new Person2(name, age));
add(new Person2(name+"!", age+1));
add(new Person2(name+"A", age+2));
}};
return persons;
}
@GetMapping("/calc14")
@ResponseBody
String showCalc14() {
String html = "";
html += "<div>";
html += "<input type=\"text\" placeholder=\"내용\">";
html += "</div>";
return html;
}
@GetMapping("/calc15")
@ResponseBody
StringBuilder showCalc15() {
StringBuilder html = new StringBuilder(); // 문자열 불변성을 위해
html.append("<div>");
html.append("<input type=\"text\" placeholder=\"내용\">");
html.append("</div>");
return html;
}
@GetMapping("/calc16")
@ResponseBody
String showCalc16() {
String html = "<div><input type=\"text\" placeholder=\"내용\"></div>";
return html;
}
@GetMapping("/calc17")
@ResponseBody
String showCalc17() {
String html = """
<div>
<input type="text" placeholder="내용">
</div>
""";
return html;
}
@GetMapping("/calc18")
@ResponseBody
String showCalc18() {
String html = """
<div>
<input type="text" placeholder="내용" value="반가워요">
</div>
""";
return html;
}
@GetMapping("/calc19")
@ResponseBody
String showCalc19(
@RequestParam(defaultValue = "") String subject,
@RequestParam(defaultValue = "") String content
) {
String html = """
<div>
<input type="text" placeholder="주제" value="%s">
</div>
<div>
<input type="text" placeholder="내용" value="%s">
</div>
""".formatted(subject, content);
return html;
}
@GetMapping("/calc20")
String showCalc20() {
return "calc20";
}
@GetMapping("/calc21")
String showCalc21(Model model) {
model.addAttribute("v1", "안녕");
model.addAttribute("v2", "반가워");
return "calc21";
}
int num = 0; // HomeController 인스턴스 변수
@GetMapping("/calc22")
@ResponseBody
int showCalc22() {
num++;
return num;
}
}
@AllArgsConstructor
class Person {
public String name;
public int age;
}
@AllArgsConstructor
class Person2 {
@Getter
private String name;
@Getter
private int age;
}
ArticleController.java
▼ String으로 doWrite 메서드 만들기
package com.ll.sb1114prac;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class ArticleController {
@GetMapping("/article/write") // @GetMapping -> 액션 메서드
String showWrite() {
return "article/write.html";
}
@GetMapping("/article/dowrite")
@ResponseBody
String doWrite(
String title,
String body
) {
return "게시물이 작성되었습니다.";
}
}
▼ Article 객체 사용 doWrite 메서드 만들기
package com.ll.sb1114prac;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class ArticleController {
@GetMapping("/article/write")
String showWrite() {
return "article/write.html";
}
@GetMapping("/article/dowrite")
@ResponseBody
Article doWrite(
String title,
String body
) {
Article article = new Article(1, title, body);
return article;
}
}
@AllArgsConstructor
@Getter
class Article {
private long id;
private String title;
private String body;
}
▼ Map 객체 사용 doWrite 메서드 만들기
package com.ll.sb1114prac;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class ArticleController {
private Article lastArticle;
@GetMapping("/article/write")
String showWrite() {
return "article/write.html";
}
@GetMapping("/article/doWrite")
@ResponseBody
Map<String, Object> doWrite(
String title,
String body
) {
lastArticle = new Article(1, title, body);
Map<String, Object> rs = new HashMap<>();
rs.put("msg", "1번 게시물이 작성되었습니다.");
rs.put("data", lastArticle);
return rs;
}
@GetMapping("/article/getLastArticle")
@ResponseBody
Article getLastArticle() {
return lastArticle;
}
}
@AllArgsConstructor
@Getter
class Article {
private long id;
private String title;
private String body;
}
'Study with me > TECH!T back-end shcool 7' 카테고리의 다른 글
week_05 SpringBoot 3/? (0) | 2023.11.16 |
---|---|
week_05 SpringBoot 2/? (0) | 2023.11.15 |
week_05 JAVA 11/11 (0) | 2023.11.13 |
week_04 Java 10/11 (0) | 2023.11.09 |
week_04 Optional +@ (0) | 2023.11.08 |