プロジェクト

全般

プロフィール

« | » 

リビジョン b15ab8d2

高徹 高橋 徹 さんが9ヶ月前に追加

Add validation for form

差分を表示:

learn/java/spring/SpringThoroughIntroduction/hello_mvc_thymeleaf/build.gradle.kts
dependencies {
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
learn/java/spring/SpringThoroughIntroduction/hello_mvc_thymeleaf/src/main/java/com/torutk/spring/echo/EchoController.java
package com.torutk.spring.echo;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......
}
@RequestMapping(method = RequestMethod.POST)
public String echo(EchoForm form) {
public String echo(@Valid EchoForm form, BindingResult result) {
if (result.hasErrors()) {
return "echo/input";
}
return "echo/output";
}
}
learn/java/spring/SpringThoroughIntroduction/hello_mvc_thymeleaf/src/main/java/com/torutk/spring/echo/EchoForm.java
package com.torutk.spring.echo;
import jakarta.validation.constraints.NotEmpty;
import java.io.Serial;
import java.io.Serializable;
......
@Serial
private static final long serialVersionUID = 1L;
@NotEmpty
private String text;
public String getText() {

他の形式にエクスポート: Unified diff