package com.sp.app.test1;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller("test1.testController")
@RequestMapping("/test1/*")
public class TestController {
@RequestMapping("main")
public String main() throws Exception {
return "test1/main";
}
// Map을 리턴하면 모델을 설정
@RequestMapping("hello")
public Map<String, Object> execute() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("msg", "Map 인터페이스를 리턴 타입으로 포워딩 JSP에 값 전달");
return map;
}
// void를 리턴하는 경우는 뷰가 필요 없는 경우(다운로드 등)
@RequestMapping("hello2")
public void execute2(
HttpServletRequest req,
HttpServletResponse resp) throws Exception {
// HttpServletResponse가 있으면 뷰를 자동 설정 하지 않음.
try {
String a = req.getParameter("name");
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("<script>alert('" + a + "님 반가워요');history.back();</script>");
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("calc")
public String calcForm(
@RequestParam String num,
Model model) throws Exception {
try {
int n = Integer.parseInt(num);
int s = 0;
for(int i=1; i<=n; i++) {
s+=i;
}
model.addAttribute("msg", "결과:"+s);
} catch (Exception e) {
return "redirect:/test1/error"; // 리다이렉트
}
return "test1/hello";
}
@RequestMapping("error")
public String errorForm() throws Exception {
return "test1/error";
}
}
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3> 리턴 타입 </h3>
<p>
<a href="${pageContext.request.contextPath}/test1/hello">확인</a>
</p>
<p>
<a href="${pageContext.request.contextPath}/test1/hello2?name=kim">확인</a>
</p>
<p>
<a href="${pageContext.request.contextPath}/test1/calc?num=a">계산</a>
</p>
<!--
- @RequestMapping 메소드의 리턴 타입
String : 뷰의 이름 -> ModelAndView 로 변환하여 처리
ModelAndView : 모델과 뷰의 이름 설정
req.setAttribute("이름", 값); <-- 모델
Map, Model, ModelMap : 모델을 설정, 뷰는 viewResolver가 등록된 경우 뷰의 이름은 자동으로 uri를 이용하여 설정한다.
uri가 test1/hello 이면 JSP이면 /WEB-INF/views/test1/hello.jsp 가 됨
void : HttpServletResponse 파라미터가 존재하지 않으면 뷰가 자동으로 설정
-->
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<p> 시스템 점검 중입니다. </p>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<p> ${msg} </p>
</body>
</html>