사용자가 Web server에 요청을 보냈을 때
-> Web server에서 직접 처리 가능한 작업은 사용자에게 직접 응답을 보냄
-> Web server에서 직접 처리 불가능한 작업은 Web server에서 WAS로 요청을 보내고 WAS에서 Web server를 통해 사용자에게 응답을 보내게 됨
파라미터를 넘길 때는 key=value
로 매핑해서 넘겨줌
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
${hi}
</h1>
<a href="test.do?firstName=yeji&lastName=kim&age=29&job=none">click</a>
<form action="test.do" method="post">
<input type="text" placeholder="firstName" name="firstName"> <!-- name:= key -->
<input type="text" placeholder="lastName" name="lastName">
<input type="text" placeholder="age" name="age">
<input type="text" placeholder="job" name="job">
<button type="submit">Submit</button>
</form>
</body>
</html>
HomeVO.java
package com.spring.boardweb;
// VO: ValueObjec. 데이터 테이블과 1:1 매칭되도록 구현하는 게 원칙
// := DTO: Data Transfer Object
public class HomeVO {
private String firstName;
private String lastName;
private int age;
private String job;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
HomeController.java
package com.spring.boardweb.controller;
import java.util.Map;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.spring.boardweb.HomeVO;
import com.spring.boardweb.service.HomeService;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
// @Autowired는 의존성을 주입할 때 모양만 비교함
// HomeService 모양으로 생성된 객체가 있는 지 없는 지 찾게됨
// 있으면 주입, 없으면 에러발생
// HomeService 모양의 객체가 여러개 존재하면 에러 발생
// @Autowired // homeService = new HomeServiceImpl();
// @Qualifier는 의존성을 주입할 때 모양과 이름을 비교함
// 같은 모양의 객체가 여러개 존재할 때 이름으로 비교하여 의존성 주입함
@Qualifier("homeServiceImpl")
HomeService homeService;
// HomeService homeService2 = new HomeServiceImpl();
// HomeService homeService3 = new HomeServiceImplTwo();
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
model.addAttribute("hi", "Hello spring");
return "home";
}
// 화면단에서 전달되는 파라미터 받기
// 1. @RequestParam 이용
// @RequestMapping(value = "test.do", method = RequestMethod.GET)
// public String testGet(@RequestParam("firstName") String firstName,
// @RequestParam("lastName") String lastName) {
// System.out.println("get요청 처리");
// System.out.println("get firstName =====" + firstName);
// System.out.println("get lastName =====" + lastName);
// return "home";
// }
//
// @RequestMapping(value = "test.do", method = RequestMethod.POST)
// public String testPost(@RequestParam("firstName") String firstName,
// @RequestParam("lastName") String lastName) {
// System.out.println("post요청 처리");
// System.out.println("get firstName =====" + firstName);
// System.out.println("get lastName =====" + lastName);
// return "home";
// }
// 2. @RequestParam 이용하여 Map에 할당
// @RequestMapping(value = "test.do", method = RequestMethod.GET)
// public String testGet(@RequestParam Map<String, Object> paramMap) {
// System.out.println("get요청 처리");
// System.out.println("get firstName =====" + paramMap.get("firstName"));
// System.out.println("get lastName =====" + paramMap.get("lastName"));
// System.out.println("get age =====" + paramMap.get("age"));
// System.out.println("get job =====" + paramMap.get("job"));
// // return "home" : /WEB-INF/views/home.jsp로 이동하게 됨
// return "home";
// }
//
// @RequestMapping(value = "test.do", method = RequestMethod.POST)
// public String testPost(@RequestParam Map<String, Object> paramMap) {
// System.out.println("post요청 처리");
// System.out.println("get firstName =====" + paramMap.get("firstName"));
// System.out.println("get lastName =====" + paramMap.get("lastName"));
// System.out.println("get age =====" + paramMap.get("age"));
// System.out.println("get job =====" + paramMap.get("job"));
// // return "redirect:/" : viewResolver를 통지 않고 주소가 "localhost:8090/"으로 바로 이동하도록 설정 가능
// return "redirect:/";
// 3. Command 객체를 이용해서 파라미터 받기
// Command 객체: 함수의 매개변수 안에 선언된 객체
@RequestMapping(value = "test.do", method = RequestMethod.GET)
public String testGet(HomeVO homeVO) {
System.out.println("get요청 처리");
System.out.println("get firstName =====" + homeVO.getFirstName());
System.out.println("get lastName =====" + homeVO.getLastName());
System.out.println("get age =====" + homeVO.getAge());
System.out.println("get job =====" + homeVO.getJob());
return "home";
}
@RequestMapping(value = "test.do", method = RequestMethod.POST)
public String testPost(HomeVO homeVO) {
System.out.println("post요청 처리");
System.out.println("get firstName =====" + homeVO.getFirstName());
System.out.println("get lastName =====" + homeVO.getLastName());
System.out.println("get age =====" + homeVO.getAge());
System.out.println("get job =====" + homeVO.getJob());
return "redirect:/";
}
}