Spring MVC - @RequestMapping, @RequestParam, ModelAndView
by rlaehddnd0422์คํ๋ง์ด ์ ๊ณตํ๋ ์ปจํธ๋กค๋ฌ๋ ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ์ผ๋ก ๋์ํด์ ์ ์ฐํ๊ณ ์ค์ฉ์ ์ ๋๋ค.
v1 - @RequestMapping ๋์
@RequestMapping ๊ธฐ๋ฐ์ ์ด๋ ธํ ์ด์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฝ๋๋ฅผ ํตํด ๋จผ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
1. MemberForm
SpringMemberFormControllerV1
@Controller
public class SpringMemberFormControllerV1 {
@RequestMapping("springmvc/v1/members/new-form")
public ModelAndView process()
{
return new ModelAndView("new-form");
}
}
@Controller
- ์คํ๋ง์ด ์๋์ผ๋ก ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก ( @Controller ์ด๋ ธํ ์ด์ ๋ด๋ถ์ @Component๊ฐ ์์ด์ ์ปดํฌ๋ํธ ์ค์บ ๋์์ผ๋ก ๋ฑ๋ก )
- ์คํ๋ง MVC์์ ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ ์ปจํธ๋กค๋ฌ๋ก ์ธ์
@RequestMapping
- ์์ฒญ ์ ๋ณด๋ฅผ ๋งคํ. ํ๋ผ๋ฏธํฐ๋ก ์ ๋ ฅํ URL์ด ํธ์ถ๋๋ฉด ์ด ๋ฉ์๋๊ฐ ํธ์ถ๋ฉ๋๋ค.
- ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ ๋์์ผ๋ก ๋ฉ์๋์ ์ด๋ฆ์ ์์๋ก ์ง์ ์ ์์ต๋๋ค.
ModelAndView
- Model๊ณผ View์ ์ ๋ณด๋ฅผ ๋ด์์ ๋ฆฌํด
- Spring์์ ์ ๊ณตํ๋ ๋ชจ๋
- Model๋ ์์ง๋ง ModelAndView๋ view์ค์ , model์ค์ , ์ํ ์ค์ ๊น์ง ๊ฐ๋ฅํฉ๋๋ค.
@Controller ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ง ์๊ณ ์ง์ @Component๋ฅผ ์ฌ์ฉํด ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํด๋ ๋์ผํ๊ฒ ๋์ํฉ๋๋ค.
@Component
@RequestMapping
public class SpringMemberFormControllerV1 {
@RequestMapping("/springmvc/v1/members/new-form")
public ModelAndView process()
{
return new ModelAndView("new-form");
}
}
๋๋ ์๋ ์ฒ๋ผ ์ ํ๋ฆฌ์ผ์ด์ ์ ์๋์ผ๋ก ๋น์ผ๋ก ๋ฑ๋กํด๋ ๋์ผํ๊ฒ ๋์ํฉ๋๋ค.
@ServletComponentScan
@SpringBootApplication
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
@Bean
SpringMemberFormControllerV1 springMemberFormControllerV1(){
return new SpringMemberFormControllerV1();
}
}
package hello.servlet.web.springmvc.v1;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//@Component
//@RequestMapping
@RequestMapping
public class SpringMemberFormControllerV1 {
@RequestMapping("/springmvc/v1/members/new-form")
public ModelAndView process()
{
return new ModelAndView("new-form");
}
}
์ผ๋ฐ์ ์ผ๋ก๋ @Controller ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, ์ด์ ๋ถํฐ๋ ๋น์ @Component๋ฅผ ์ง์ ์ฌ์ฉํด ๋ฑ๋กํ๊ฑฐ๋, ์๋์ผ๋ก ๋ฑ๋กํ์ง ์๊ณ @Controller๋ฅผ ์ฌ์ฉํ๋๋ก ํ๊ฒ ์ต๋๋ค.
2. MemberSave
@Controller
public class SpringMemberSaveControllerV1 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@RequestMapping("/springmvc/v1/members/save")
public ModelAndView process(HttpServletRequest request, HttpServletResponse response)
{
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
Member member = new Member(username,age);
memberRepository.save(member);
ModelAndView mv = new ModelAndView("save-result");
mv.addObject("member",member);
return mv;
}
}
์ด๋ ธํ ์ด์ ์ค๋ช ์์ด ๋ก์ง๋ง ์์ฝํ๊ณ ๋์ด๊ฐ๊ฒ ์ต๋๋ค.
- springmvc/v1/members/save URL ํธ์ถ
- request๋ก๋ถํฐ "username", "age" parameter โฃ String username, int age ๊ฐ๊ฐ ์ ์ฅ
- Member ๊ฐ์ฒด username, age๋ฅผ ํตํด ์์ฑ
- ์ฑ๊ธํค MemberRepository์ ์ ์ฅ
- ModelAndView ์์ฑ - ์์ฑ์๋ ํ๋ผ๋ฏธํฐ๋ ๋ทฐ ๋ ผ๋ฆฌ์ด๋ฆ
- Model์ member๊ฐ์ฒด "member"๋ก ๋ด๊ณ ๋ฆฌํด
- mv.addObject("member",member) : ์คํ๋ง์ด ์ ๊ณตํ๋ ModelAndView๋ฅผ ํตํด Model ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ ๋๋ addObject()๋ฅผ ์ฌ์ฉํ๋ฉฐ ๋ฉ๋๋ค.
3. MemberList
@Controller
public class SpringMemberListControllerV1 {
private final MemberRepository memberRepository = MemberRepository.getInstance();
@RequestMapping("/springmvc/v1/members")
public ModelAndView process(HttpServletRequest request, HttpServletResponse response)
{
List<Member> members = memberRepository.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("members",members);
return mv;
}
}
logic
1. /springmvc/v1/members URL ํธ์ถ
2. ๋ฆฌํฌ์งํ ๋ฆฌ -> FindAll() -> List<Member> member์ ์ ์ฅ
3. ModelAndView ์์ฑ -> ๋ทฐ ๋ ผ๋ฆฌ์ด๋ฆ "members"
4. ModelAndView ๊ฐ์ฒด์ addObject()๋ฅผ ํตํด List ๋ด์
5. ModelAndView return
v2 - Controller ํตํฉ
package hello.servlet.web.springmvc.v2;
import hello.servlet.basic.domain.member.Member;
import hello.servlet.basic.domain.member.MemberRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
@RequestMapping("/springmvc/v2/members")
public class SpringMemberControllerV2 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@RequestMapping("/new-form")
@ResponseBody
public ModelAndView newForm() {
return new ModelAndView("new-form");
}
@RequestMapping("/save")
public ModelAndView save(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
Member member = new Member(username, age);
memberRepository.save(member);
ModelAndView mv = new ModelAndView("save-result");
mv.addObject("member", member);
return mv;
}
@RequestMapping
public ModelAndView members(HttpServletRequest request, HttpServletResponse response) {
List<Member> members = memberRepository.findAll();
ModelAndView mv = new ModelAndView("members");
mv.addObject("members", members);
return mv;
}
}
์ปจํธ๋กค๋ฌ ํด๋์ค๋ฅผ ํตํฉํ๋ ๊ฒ ๋ฟ๋ง ์๋๋ผ ์กฐํฉ๋ ๊ฐ๋ฅํฉ๋๋ค.
์ฒซ ๋ฒ์งธ ๋ฐฉ์์์ ๊ฐ ์ปจํธ๋กค๋ฌ๋ "/springmvc/v1/members" URL์ ์ค๋ณต์ด ์์์ต๋๋ค.
๋ชจ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ํ ํด๋์ค์ ํตํฉํ๊ณ ์์ ๊ฐ์ด ํด๋์ค๋ ๋ฒจ์ ์ค๋ณต URL์ @RequestMapping ํด์ฃผ๋ฉด ํจ์ฌ ํด๋ฆฐํ๊ฒ ์์ฑํ ์ ์์ต๋๋ค.
v3 - ์ค์ฉ์ฑ ์ฌ๋ฆฌ๊ธฐ ( ์ค๋ฌด )
SpringMemberControllerV3
package hello.servlet.web.springmvc.v3;
import hello.servlet.basic.domain.member.Member;
import hello.servlet.basic.domain.member.MemberRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
@RequestMapping("/springmvc/v3/members")
public class SpringMemberControllerV3 {
private MemberRepository memberRepository = MemberRepository.getInstance();
// @GetMapping(value="/new-form")
@RequestMapping(value="/new-form", method= RequestMethod.GET)
public String newForm()
{
return "new-form";
}
// @PostMapping("/save")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(
@RequestParam("username") String username,
@RequestParam("age") int age,
Model model)
{
Member member = new Member(username,age);
memberRepository.save(member);
model.addAttribute("member",member);
return "save-result";
}
// @GetMapping
@RequestMapping(method = RequestMethod.GET)
public String members(Model model)
{
List<Member> members = memberRepository.findAll();
model.addAttribute("members",members);
return "members";
}
}
1. Model ๋์
- save(), members()๋ฅผ ๋ณด๋ฉด Model์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
2. ModelAndView๊ฐ ์๋ String์ผ๋ก ViewName ์ง์ ๋ฐํ
- view์ ๋ ผ๋ฆฌ์ด๋ฆ์ String ํํ๋ก ์ง์ ๋ฆฌํด
3. @RequestParam ์ฌ์ฉ
- ์คํ๋ง์ HTTP ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ @RequestParam์ผ๋ก ๋ฐ์ ์ ์์ต๋๋ค.
- @RequestParam("username")์ request.getParameter("username")๊ณผ ๊ฐ์ ์ฝ๋๋ผ ์๊ฐํ์๋ฉด ๋ฉ๋๋ค.
- ๋ฌผ๋ก GET ๋ฐฉ์(์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ), POST(๋ฉ์์ง ๋ฐ๋) ๋ฐฉ์ ๋ชจ๋ ์ง์
4. @RequestMapping โฃ @GetMapping, @PostMapping
- @RequestMapping์ ํ๋ผ๋ฏธํฐ๋ก method=RequestMethod.GET ๋๋ method=RequestMethod.POST ๋ฅผ ์ฌ์ฉํด์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ์ ์กํ ์ง(GET), ๋ฉ์์ง ๋ฐ๋๋ก ์ ๋ฌํ ์ง(POST)๋ฅผ ์ ํํ ์๋ ์์ต๋๋ค๋ง, @GetMapping, @PostMapping์ ์ด์ฉํด ๋ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- Get,Post,Put,Delete,Patch ๋ชจ๋ ์ด๋ ธํ ์ด์ ์ด ์ค๋น๋์ด ์์ต๋๋ค.
<์ฐธ๊ณ ์๋ฃ>
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1/dashboard
https://bestkingit.tistory.com/155
'๐ Backend > MVC Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring MVC ๊ธฐ๋ณธ ๊ธฐ๋ฅ - ์์ฒญ ๋งคํ @RequestMapping, @RequestParam (0) | 2023.03.12 |
---|---|
Spring MVC ๊ธฐ๋ณธ ๊ธฐ๋ฅ - Logging (0) | 2023.03.10 |
๋ค์ํ ๋ฐฉ์์ HandlerMapping, HandlerAdapter, ViewResolver (0) | 2023.03.03 |
Spring MVC ๊ตฌ์กฐ (0) | 2023.03.03 |
Adapter ํจํด (0) | 2023.03.02 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Study Repository
rlaehddnd0422