Spring MVC, Sptring boot/spring-io-guides

Handling Form Submission

고인돌개발자 2021. 8. 22. 23:16

Spring guide (스프링 가이드 실습) https://spring.io/guides/gs/handling-form-submission/  


▶ 학습목표 
    1.  model.addAttribute 에 멤버변수 클랙스를 이용하여 값을 주고 받을 수 있다.
     2. @ModelAttribute 어노테이션을 통해 Form 값의 파라메타를 받을 수 있다.


▶ Dependency - build.gradle

더보기

plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'com.guides'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
useJUnitPlatform()
}


▶ 자바코드
    1. Controller 
    2. 멤버변수를 갖는 클래스 

더보기

 

package com.guides.spring;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        /* Greeting 클래스를 속성값으로 전달하여 해당 클래스의 멤버변수를
           templates 에서 사용할 수 있도록 한다.
           주의. 매핑은 set , get 뒤의 이름의 소문자로 주고 받는다.
                해당 멤버변수에 직접 매핑하지 않는다는 의미 
         */
        model.addAttribute("greeting", new Greeting());

        System.out.println("greeting1");
        return "greeting";
    }

    @PostMapping("/greeting")
    public String greetingSubmit( @ModelAttribute Greeting greeting, Model model) {
          /* @ModelAttribute 어노테이션을 통해 넘어오는 파라메터를
             Greeting 클래스에서 매핑하여 받도록 한다.
             주의. 매핑은 set , get 뒤의 이름의 소문자로 주고 받는다.
                  해당 멤버변수에 직접 매핑하지 않는다는 의미 
         */

        System.out.println("greeting2");
        model.addAttribute("greeting", greeting);
        return "result";
    }

}

 

package com.guides.spring;

public class Greeting {

    private long id=2020;
    private String content="hi-jar";

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

▶ templates (Thymeleaf)
    1. greeting.html 
    2. result.html

더보기

greeting.html

<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
    <p>Id: <input type="text" th:field="*{id}" /></p>
    <p>Message: <input type="text" th:field="*{content}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

result.html

<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
  <title>Getting Started: Handling Form Submission</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<a href="/greeting">Submit another message</a>
</body>
</html>

▶ 실행화면 

'Spring MVC, Sptring boot > spring-io-guides' 카테고리의 다른 글

Scheduling Tasks  (0) 2021.12.06
Validating Form Input  (0) 2021.08.22