본문 바로가기
Spring/이론

Form데이터 //HttpServletRequest == @RequestParam//

by SEOKIHOUSE 2023. 7. 18.
  • HttpServletRequest

쿼리스트링으로 직접 적어주자


위에거 다른방식으로 하는것 같다

  • @RequestParam

package com.study.springboot;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

	@RequestMapping("/")
	public @ResponseBody String root() {
		return "Form데이터 전달받아 사용하기";
	}

	@RequestMapping("/test1")
	public String test1(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		String name = request.getParameter("name");

		model.addAttribute("id", id);
		model.addAttribute("name", name);

		return "test1";
	}

	@RequestMapping("/test2")
	public String test2(@RequestParam("id") String id, 
						@RequestParam("name") String name, 
						Model model) {
		
		model.addAttribute("id",id);
		model.addAttribute("name",name);
		
		return "test2";
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		out.print("#01 : Hello World");
	%>
	<br>
	
	당신의 아이디는 ${id }입니다.
	당신의 이름은 ${name }입니다.
</body>
</html>