// 전달될 값을 만드는 방법 => Model 클래스 이용
@RequestMapping("/content") //url주소 적어야 넘어감 / 주소가 들어오면 url 밑에 값 실행
public String content(Model model) { //매개로 들어감
model.addAttribute("hackbun", 20240529); // 학번이라는 변수에 값 넣어서 전달
model.addAttribute("id", "nahye");
return "content"; // views -> content.jsp 연결
}
// 전달될 값 만드는 법 => ModelAndView 클래스 이용
@RequestMapping("/request")
public ModelAndView reply() {
ModelAndView mv = new ModelAndView();
mv.addObject("name", "아무개");
mv.setViewName("reply"); //views -> reply.jsp와 연결
return mv;
}
1. src.main.java 아래 vo패키지 생성
(이때 controller가 위치하는 패키지 이름과 동일한 게 좋음)
2. controller 클래스 생성
- 폼태그의 name과 멤버변수가 동일
- 테이블의 컬럼 이름과 동일하게
- 생성자는 상황에 따라서 매개변수 알아서 넣기
- getter/setter는 필수 必
// 결과화면 1 : @ModelAttribute("객체명") 참조할 클래스
@RequestMapping("/st1")
public String studentView1(@ModelAttribute("info") StudentVo studentVo) {
//StudentVo 타입의 info이름의 객체를 만듦
return "studentView1"; // views -> studentView1.jsp와 연결
}
// 결과화면 3-2 : @RequestParam + vo // 일부만 받고싶을 때
@RequestMapping("/st2")
public String studentView2(@RequestParam("name") String name, @RequestParam("age") String age,
Model model) { // 받는 거 따로 모델 따로
StudentVo student = new StudentVo();
student.setName(name);
student.setAge(age);
model.addAttribute("studentVo", student);
return "studentView2";
}
3. web > views > Input/View.jsp 파일 생성