/**
* @PathVariable 사용
* <p>
* request url 을 통해 parameter 를 받는 방법은 다음 방법들이 있다
* /API_NAME?key1=val1 <-- query string 사용
* /API_NAME/{value1} <-- path variable 사용
*/
@RequestMapping("/writePath/{name}/{subject}/{k3}")
@ResponseBody
public String writePathBoard(
@PathVariable String name,
@PathVariable String subject,
@PathVariable(name = "k3") String content
) {
return """
name: %s<br>
subject: %s<br>
content: %s<br>
""".formatted(name, subject, content);
}
//-----------------------------------
// redirect
// "redirect:{url}" 을 리턴 => redirect 를 response
// redirect 의 response code 는 3xx
@RequestMapping("/ageInput")
public void ageInput() {
}
@RequestMapping("/ageCheck")
public String chkAge(int age,
RedirectAttributes redirectAttributes) // redirect 되는 request 에 담을 parameter 지정, query string 에 담겨 간다
{
redirectAttributes.addAttribute("age", age);
if (age < 19) {
return "redirect:/user/underAge"; // view가 아닌 redirect 됨
} else {
return "redirect:/user/adult";
}
}
@RequestMapping("/underAge")
@ResponseBody
public String pageUnderAge(int age) {
return """
미성년자<br>
나이: %s살, %s년 뒤에 사용 가능
""".formatted(age, 19 - age);
}
@RequestMapping("/adult")
@ResponseBody
public String pageAdult(int age) {
return """
성인<br>
나이: %d살
""".formatted(age);
}

mysql: database url 반드시 명시해야한다. database url 설정을 주의하자
public class Post {
private Long id;
private String subject;
private String content;
// getter, setter...
}
public interface PostRepository {
int save(Post post);
Post findById(Long id);
// ...
}
<insert id="save">
INSERT INTO post(subject, content)
VALUES(#{subject}, #{content})
</insert>
@Service
public class PostService {
private final PostRepository repository;
public void write(Post post) {
repository.save(post);
}
}
@Controller
public class PostController {
private final PostService service;
@PostMapping("/write")
public String write(Post post) {
service.write(post);
return "redirect:/list";
}
}