[스프링 예제] 2. 스프링 웹 개발 기초

16968 단어 SpringSpring

정적 컨텐츠

  • 서버에서 하는 것 없이 파일을 그대로 웹브라우저에 다운.

  • 레퍼런스 참고

  • 간단 예)

    • \resources\static\hello-spring.html

      <!DOCTYPE HTML>
      <html>
      <head>
       <title>static content</title>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      </head>
      <body>
      정적 컨텐츠 입니다.
      </body>
      </html>
    • 결과

    • 동작


MVC와 템플릿 엔진

  • MVC : Model, View, Controller

    • Model-1을 사용하지 않고 MVC를 사용하는 이유 : 분업
  • Controller, Model

    package hello.hellospring.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class HelloController {
    
        @GetMapping
        public String helloMvc(@RequestParam("name") String name, Model model){
            model.addAttribute("name",name);
            return "hello-template";
        }
    }
  • View

    • resources\templates\hello-template.html

      <html xmlns:th="http://www.thymeleaf.org">
      <body>
      <p th:text="'hello ' + ${name}">hello! empty</p>
      </body>
      </html>
  • 결과 (⇒ @RequestParam("name") String name?, =를 통해서 넘긴다.

  • 동작


API

  • MVC와 템플릿 엔진 : html정보를 넘긴다.

  • API : 데이터 정보를 넘긴다.

  • 예제 1)

    • Controller

      package hello.hellospring.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.ResponseBody;
      
      @Controller
      public class HelloController {
      
          @GetMapping("hello-string")
          @ResponseBody
          public String helloString(@RequestParam("name") String name){
              return "hello" + name; //html 문을 return에 직접 기입할 수도 있다.
          }
      }
      • @ResponseBody : html에 존재하는 <body>부에 ❤직접 데이터를 넣겠다.
    • 결과

  • 예제 2)

    • Controller

      package hello.hellospring.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.ResponseBody;
      
      @Controller
      public class HelloController {
      
          @GetMapping("hello-api")
          @ResponseBody
          public Hello hello(@RequestParam("name") String name){
              Hello hello = new Hello();
              hello.setName(name);
              return hello;
          }
      
          static class Hello{
              private String name;
      
              public String getName() {
                  return name;
              }
      
              public void setName(String name) {
                  this.name = name;
              }
      				//getter, setter : property 방식
          }
      }
    • 결과 (⇒ Json의 결과가 나타난다. {} : 객체)

    • 동작

      • @ResponseBody를 사용
        • HTTP의 BODY에 문자 내용을 직접 반환
        • viewResolver 대신에 HttpMessageConverter가 동작
        • 기본 문자처리: StringHttpMessageConverter
        • 기본 객체처리: MappingJackson2HttpMessageConverter
        • byte 처리 등등 기타 여러 HttpMessageConverter가 기본으로 등록되어 있음

좋은 웹페이지 즐겨찾기