SpringBoot/기초

스프링 웹 개발 기초

2023. 3. 10. 11:02
목차
  1. 정적 콘텐츠
  2. MVC와 템플릿 엔진
  3. API

정적 콘텐츠

스프링 부트 정적 콘텐츠 기능

https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-static-content

 

Spring Boot Features

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

 

<!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

 

Controller

HelloController.java

@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
    model.addAttribute("name", name);
    return "hello-template";
}

 

View

<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present]

 

Controller에서 파라미터로 name을 주기로 했는데 없어서 그렇다.

 

이렇게 name을 넘겨주면 제대로 나온다.

 

 

 

API

@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
    return "hello" + name;
}

ResponseBody는 Http에서 body부에 데이터를 직접 넣어주겠다는 의미이다. (html의 <body>가 아니다.)

소스를 보면 html 태그 등이 하나도 없이 그냥 문자가 그대로 들어간다.

 

@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@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;
    }
}

이것은 json이라는 방식이다.

 

'SpringBoot > 기초' 카테고리의 다른 글

스프링 DB 접근 기술(1) - 순수 JDBC  (0) 2023.03.10
회원 관리 예제 - 웹 MVC 개발  (0) 2023.03.10
스프링 빈과 의존관계  (0) 2023.03.10
회원 관리 예제 - 백엔드 개발  (0) 2023.03.10
프로젝트 환경설정  (0) 2023.03.10
  1. 정적 콘텐츠
  2. MVC와 템플릿 엔진
  3. API
'SpringBoot/기초' 카테고리의 다른 글
  • 회원 관리 예제 - 웹 MVC 개발
  • 스프링 빈과 의존관계
  • 회원 관리 예제 - 백엔드 개발
  • 프로젝트 환경설정
ewok
ewok
ewok
기록장
ewok
전체
오늘
어제
  • 분류 전체보기
    • 웹개발 교육
      • HTML
      • CSS
      • JavaScript
      • Database
      • Java
      • jQuery
      • Ajax
      • Bootstrap
      • jsp
      • Spring
      • MyBatis
      • 프로젝트
    • JAVA
    • SpringBoot
      • 기초
      • AWS
      • 개인프로젝트
    • Spring Security
    • JPA
    • 테스트코드
    • Error
    • CS
      • 컴퓨터 구조
      • 이산수학
    • 알고리즘
      • 정리
      • Java
    • SQL
    • 자격증
      • SQLD
      • 정보처리기사
    • Git

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • org.hibernate.tool.schema.spi.CommandAcceptanceException
  • merge commit
  • 노랭이
  • git bash
  • 브랜치
  • 생성자
  • org.springframework.beans.factory.UnsatisfiedDependencyException
  • branch
  • GIT
  • SQLD
  • sqld 자격증
  • this
  • 버전 관리
  • sqld 합격
  • base

최근 댓글

최근 글

hELLO · Designed By 정상우.
ewok
스프링 웹 개발 기초
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.