@Controller, @RequestMapping > 스프링 부트

본문 바로가기

[개발] @Controller, @RequestMapping

필기자
2022-09-07 11:31 8,240 0

본문

프로젝트 구조

 

20220907115222_ddfa29dc4cb6a70c618839d7aa582e62_dh9u.png

 

dto/UserDto.java



public class UserDto {
    private String name;
    private String phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "UserDto{" +
                "name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

 

controller/DefaultReqController.java



@Controller
@RequestMapping("/req")
public class DefaultReqController {
    //http://localhost:8080/req/html
    @RequestMapping(value = "/html", method = RequestMethod.GET)
    public String html() {
        return "get/index";
    }

    //http://localhost:8080/req/html/안녕하세요
    @RequestMapping(value = "/html/{msg}", method = RequestMethod.GET)
    public String html(@PathVariable String msg, Model m) {
        m.addAttribute("msg", msg);
        return "get/index";
    }

    //http://localhost:8080/req/txt?msg=안녕하세요
    @RequestMapping(value = "/txt", method = RequestMethod.GET)
    @ResponseBody
    public String html(@RequestParam(value="msg", required=false) String msg) {
        return msg;
    }

    //http://localhost:8080/req/json
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Object> html(Model m) {
        m.addAttribute("model","모델값 json");
        return new ResponseEntity<>(m, HttpStatus.OK);
    }

    //http://localhost:8080/post.html -> http://localhost:8080/req/dto
    // POST : name=홍길동&phone=01011111111 -> userDto
    @RequestMapping(value = "/dto",  method = RequestMethod.POST)
    @ResponseBody
    public UserDto html(UserDto userDto) {
        return userDto;
    }

    //http://localhost:8080/post.html -> http://localhost:8080/req/dto-model
    // POST : name=홍길동&phone=01011111111 -> userDto
    @RequestMapping(value = "/dto-model", method = RequestMethod.POST)
    public String html(UserDto userDto, Model m) {
        m.addAttribute("dto", userDto);
        return "post/index";
    }
}

 

src/main/resources/static/post.html



<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>@RequestMapping 테스트</title>
</head>
<body>
    <!--  @PostMapping -->
    <div>
        <h4>http://localhost:8080/post/html</h4>
        <form action="/post/html" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div>
        <h4>http://localhost:8080/post/html/{msg}</h4>
        <form action="/post/html/안녕하세요/" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div>
        <h4>http://localhost:8080/post/txt</h4>
        <form action="/post/txt?msg=홍길동" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div>
        <h4>http://localhost:8080/post/json</h4>
        <form action="/post/json" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div>
        <h4>http://localhost:8080/post/json</h4>
        <form action="/post/json" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div>
        <h4>http://localhost:8080/post/dto</h4>
        <form action="/post/dto" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td>이름</td>
                    <td><input type="text" name="name" value="홍길동" /></td>
                </tr>
                <tr>
                    <td>핸드폰</td>
                    <td><input type="text" name="phone" value="010-8888-88888" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div>
        <h4>http://localhost:8080/post/dto-model</h4>
        <form action="/post/dto-model" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td>이름</td>
                    <td><input type="text" name="name" value="홍길동" /></td>
                </tr>
                <tr>
                    <td>핸드폰</td>
                    <td><input type="text" name="phone" value="010-8888-88888" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    
    <!--  @RequestMapping RequestMethod.POST -->
    <div>
        <h4>http://localhost:8080/req/dto</h4>
        <form action="/req/dto" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td>이름</td>
                    <td><input type="text" name="name" value="홍길동" /></td>
                </tr>
                <tr>
                    <td>핸드폰</td>
                    <td><input type="text" name="phone" value="010-8888-88888" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div>
        <h4>http://localhost:8080/req/dto-model</h4>
        <form action="/req/dto-model" method="post">
            <table style="border: 1px solid #383434;">
                <tr>
                    <td>이름</td>
                    <td><input type="text" name="name" value="홍길동" /></td>
                </tr>
                <tr>
                    <td>핸드폰</td>
                    <td><input type="text" name="phone" value="010-8888-88888" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center">
                        <button type="submit">전송</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>

 

src/main/resources/templates/get/index.html



<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>@GetMapping 테스트</title>
</head>
<body>
    <h1 th:if="${msg != null}" th:text="'model 값 : '+${msg}">안녕하세요.</h1>
    <h1 th:unless="${msg != null}" th:text="'model 값 없음'">안녕하세요.</h1>
    <div th:if="${dto}">
        <h1 th:if="${dto.name != null}" th:text="'dto 값 : '+${dto.toString()}">안녕하세요.</h1>
        <h1 th:unless="${dto.name != null}" th:text="'dto 값 없음'">안녕하세요.</h1>
    </div>
</body>
</html>

 

src/main/resources/templates/post/index.html



<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>@PostMapping 테스트</title>
</head>
<body>
    <h1 th:if="${msg != null}" th:text="'model 값 : '+${msg}">안녕하세요.</h1>
    <h1 th:unless="${msg != null}" th:text="'model 값 없음'">안녕하세요.</h1>
    
    <div th:if="${dto}">
        <h1 th:if="${dto.name != null}" th:text="'dto 값 : '+${dto.toString()}">안녕하세요.</h1>
        <h1 th:unless="${dto.name != null}" th:text="'dto 값 없음'">안녕하세요.</h1>
    </div>
</body>
</html>

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색