javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpServletResponse
javax.servlet.http.HttpSession
org.springframework.web.multipart.MultipartFile
@RequestParam
@RequestHeader
@RequestBody
@ModelAttribute
@PathVariable
org.springframework.web.servlet.ModelAndView
java.lang.String
저번에 만들어두었던 src/main/webapp/WEB-INF/views 경로에 생성
plusForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>plus form</title>
</head>
<body>
<form method="post" action="plus">
value1: <input type="text" name="value1"><br>
value2: <input type="text" name="value2"><br>
<input type="submit" value="확인">
</form>
</body>
</html>
plusResult.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>plus result</title>
</head>
<body>
${value1 } 더하기 ${value2 } 는 ${result } 입니다.
</body>
</html>
kr.or.connect.mvcexam.controller 패키지 생성 후 PlusController 클래스 생성
PlusController.java
package kr.or.connect.mvcexam.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class PlusController {
@GetMapping(path = "/plusform")
public String plusform() { // view name을 넘겨주어야 함
return "plusForm"; //요청이 들어왔을때 view name을 viewResolver에 넘김
}
@PostMapping(path = "/plus")
public String plus(@RequestParam(name="value1", required= true) int value1, @RequestParam(name = "value2", required = true) int value2, ModelMap modelMap) {
//@RequestParam에서 name과 일치하는 input의 value를 매핑
// 값 2개를 받아 더함
int result = value1+value2;
//request scope에 값들을 넣어 jsp로 전달
modelMap.addAttribute("value1", value1);
modelMap.addAttribute("value2", value2);
modelMap.addAttribute("result", result);
// view name을 viewResolver에게 보내 jsp 보여짐
return "plusResult";
}
}
web.xml 상단부를 다음과 같이 수정
<!-- servlet 버전 3.1으로 바꿔줌 -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">