form 데이터 전송 방법 중 GET 방식 막기
// @RequestMapping(value="/register/save", method=RequestMethod.POST)
@PostMapping("/register/save") // Spring 4.3 부터 추가
public String save() {
return "registerInfo";
}
}
@GetMapping 추가
package com.fastcampus.app;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class RegisterController {
// @RequestMapping("/register/add")
// @RequestMapping(value="/register/save", method= {RequestMethod.GET, RequestMethod.POST})
@GetMapping("/register/add")
public String register() {
return "registerForm";
}
// @RequestMapping(value="/register/save", method=RequestMethod.POST)
@PostMapping("/register/save") // Spring 4.3 부터 추가
public String save() {
return "registerInfo";
}
}
이제 Get 방식으로 요청해도 안된다
@GetMapping 대신 view-controller 사용하기
servlet-context.xml 에 코드 추가
<view-controller path="/register/add" view-name="registerForm"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.fastcampus.app" />
</beans:beans>
mvc 스키마는 생략한 상태
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<mvc:view-controller path="/register/add" view-name="registerForm"/>
<context:component-scan base-package="com.fastcampus.app" />
</beans:beans>
유효성 검사
msg 를 그대로 브라우저에 보내면 깨지기 때문에 URLEncoder 를 사용하자
package com.fastcampus.app;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class RegisterController {
// @RequestMapping("/register/add")
// @RequestMapping(value="/register/save", method= {RequestMethod.GET, RequestMethod.POST})
@GetMapping("/register/add")
public String register() {
return "registerForm";
}
// @RequestMapping(value="/register/save", method=RequestMethod.POST)
@PostMapping("/register/save") // Spring 4.3 부터 추가
public String save(User user, Model m) throws Exception {
// 1. 유효성 검사
if(!isValid(user)) {
String msg = URLEncoder.encode("id를 잘못입력하셨습니다.", "utf-8");
return "redirect:/register/add?msg="+msg; // URL 재작성(rewriting)
}
// 2. DB에 신규회원 정보를 저장
return "registerInfo";
}
private boolean isValid(User user) {
// TODO Auto-generated method stub
return false;
}
}
registerForm.jsp 에 msg 를 받는 코드를 추가
<div id="msg" class="msg">${param.msg}</div>
msg 가 출력 되지 않으면 decoder 추가
java.net.URLDecoder, import 해야함
<%@ page import="java.net.URLDecoder" %>
<div id="msg" class="msg">${URLDecoder.decode(param.msg)}</div>
Model 에 저장해서 넘기기
model 은 redirect 해서 요청하는 "register/add" 에서는 쓸 수 없고
mapping 한 "register/save" 에서만 사용가능 하다.
하지만 이를 Spring 에서
"redirect:/register/add?msg="+msg;
이런 형태로 만들어 주기 때문에 잘 작동된다.
package com.fastcampus.app;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class RegisterController {
// @RequestMapping("/register/add")
// @RequestMapping(value="/register/save", method= {RequestMethod.GET, RequestMethod.POST})
@GetMapping("/register/add")
public String register() {
return "registerForm";
}
// @RequestMapping(value="/register/save", method=RequestMethod.POST)
@PostMapping("/register/save") // Spring 4.3 부터 추가
public String save(User user, Model m) throws Exception {
// 1. 유효성 검사
if(!isValid(user)) {
String msg = URLEncoder.encode("id를 잘못입력하셨습니다.", "utf-8");
m.addAttribute("msg", msg);
// return "redirect:/register/add?msg="+msg; // URL 재작성(rewriting)
return "redirect:/register/add";
}
// 2. DB에 신규회원 정보를 저장
return "registerInfo";
}
private boolean isValid(User user) {
// TODO Auto-generated method stub
return false;
}
}
이제 isvalid 값을 true 로 바꿔 registerInfo 로 데이터가 잘 들어가는 지 확인하자
registerInfo.jsp 의 EL인 param 을 user로 바꾸자
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>id=${user.id}</h1>
<h1>pwd=${user.pwd}</h1>
<h1>name=${user.name}</h1>
<h1>email=${user.email}</h1>
<h1>birth=${user.birth}</h1>
<h1>sns=${user.sns}</h1>
</body>
</html>
private boolean isValid(User user) {
// TODO Auto-generated method stub
return true;
}
'Spring > Ch2. Spring MVC' 카테고리의 다른 글
Spring MVC - redirect와 forward (0) | 2022.09.19 |
---|---|
Spring MVC - @RequestMapping (0) | 2022.09.19 |
Spring MVC - 회원가입 페이지 만들기 (0) | 2022.09.18 |
Spring MVC - @ModelAttribute (0) | 2022.09.17 |
Spring MVC - @RequestParam (0) | 2022.09.17 |