Notice
Recent Posts
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- html
- 설정
- jstl
- 스프링
- jquery
- Spring
- jsp
- 깃허브
- 자바스크립트
- 제이쿼리
- Oracle
- 필터체인
- 설치
- EL태그
- 셋업
- 알고리즘
- 깃허브 간단요약
- 이클립스
- jsp 내부객체
- 오라클
- 면접
- MySQL
- springboot
- Eclipse
- 폼태그
- 마이바티스
- SESSION
- 자바
- java
- 버튼
Archives
- Today
- Total
은은하게 코드 뿌시기
[spring] - request parameter 전달 받는 방법 4가지! 예제 /Cotroller의 파라미터 수집 본문
웹/spring
[spring] - request parameter 전달 받는 방법 4가지! 예제 /Cotroller의 파라미터 수집
은은하게미친자 2022. 8. 8. 23:52728x90
Cotroller의 파라미터 수집
: Cotroller를작성할 때 가장 편리한 기능은 파라미터가 자동으로 수집되는 기능입니다!
이기능을 이용하면 매번 request.getParameter()를 이용하는 불편함을 줄일 수있습니다.
request의 파라미터를 전달 받는 방법 4가지
종류 | 예제 |
HttpServletRequest | req.getParameter("userId"); |
@RequestParam | @RequestParam("userId") String userId |
Map<String, Object> map | @RequestParam Map<string,string> info</string,string> |
BeanClass | @ModelAttribute("info") UserVO uservo |
+ 세션 주고받기 예제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package kr.co.dong;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
@RequestMapping(value = "login/loginForm", method = RequestMethod.GET)
public ModelAndView loginForm() {
logger.info("로그인폼입니다.");
System.out.println("==========================================================");
ModelAndView mav = new ModelAndView();
mav.addObject("test", "test Model");
mav.setViewName("login/loginForm");
return mav;
}
//1. request 파라미터 전달 받는 방법 : HttpServletRequest
@RequestMapping(value = "login/login", method = RequestMethod.POST)
public ModelAndView loginResult(HttpServletRequest req) throws Exception {
req.setCharacterEncoding("utf-8");
logger.info("로그인확인폼1입니다.");
System.out.println("==========================================================");
ModelAndView mav = new ModelAndView();
String u_id = req.getParameter("userId");
String u_name = req.getParameter("userName");
mav.addObject("userId", u_id);
mav.addObject("userName", u_name);
//db호출 여기서하면댐
mav.setViewName("login/result");
return mav;
}
//2. request 파라미터 전달 받는 방법 : @RequestParam 로 처리하는법
@RequestMapping(value = "login/login2", method = RequestMethod.POST)
public ModelAndView loginResult2(@RequestParam("userId") String userId,
@RequestParam("userName") String userName,
HttpServletRequest req) throws Exception {
req.setCharacterEncoding("utf-8");
logger.info("로그인확인폼2입니다.");
System.out.println("==========================================================");
ModelAndView mav = new ModelAndView();
mav.addObject("userId", userId);
mav.addObject("userName", userName);
//db호출 여기서하면댐
mav.setViewName("login/result");
return mav;
}
//3. request 파라미터 전달 받는법 : Map<String, Object> map
@RequestMapping(value = "login/login3", method = RequestMethod.POST)
public ModelAndView loginResult3(@RequestParam Map<String,String> info) {
logger.info("로그인확인폼3333입니다.");
System.out.println("==========================================================");
ModelAndView mav = new ModelAndView();
mav.addObject("info", info);
//db호출 여기서하면댐
mav.setViewName("login/result");
return mav;
}
//4. request 파라미터 전달 받는법 : BeanClass
@RequestMapping(value = "login/login4" , method = RequestMethod.POST)
public ModelAndView loginResult4(@ModelAttribute("info") UserVO uservo) {
// public ModelAndView loginResult4(UserVO uservo) { //위와같음.
logger.info("로그인확인폼4입니다.");
System.out.println("==========================================================");
ModelAndView mav = new ModelAndView();
// mav.addObject("info", uservo); //써도되고 필요없음
mav.setViewName("login/result");
return mav;
}
}
|
cs |
name을 꼭 설정할것
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="contextPath" value="${pageContext.request.contextPath }" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인폼</title>
</head>
<body>
<h1>로그인 폼 </h1>
${test}
<form action="${contextPath}/login/login4" method="post">
아이디 : <input type="text" name="userId"><br>
이름 : <input type="text" name="userName"><br>
<input type="submit" value="보내기">
</form>
</body>
</html>
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인결과창</title>
</head>
<body>
<h1>로그인 확인</h1>
아이디 : ${userId} <br>
이름 : ${userName}<br>
<hr>
아이디 : ${info.userId} <br>
이름 : ${info.userName}<br>
<hr>
</body>
</html>
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package kr.co.dong.board;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import kr.co.dong.emp.EmpVO;
@Controller
public class BoardCotroller {
@Inject
BoardService service;
//로그인 폼으로 매핑 board/login-> login(.jsp)
@RequestMapping(value = "board/login", method = RequestMethod.GET)
public String login() {
return "login";
}
//로그인 처리하기 (DAO) : 성공하면 / 또는 HOME.JSP, 세션 부여함.
@RequestMapping(value = "board/login", method = RequestMethod.POST)
public String login(@RequestParam Map<String, Object> map
, HttpServletRequest req
, HttpServletResponse res
, HttpSession session) throws Exception {
req.setCharacterEncoding("utf-8");
//서비스호출
System.out.println(map);
Map user = service.login(map);
if (user == null) {
//로그인실패
return "redirect:login"; // board/login 이라고 쓰면 board/board/login 됨
} else {
//로그인성공
session.setAttribute("user", user);
return "redirect:/";
}
}
}
|
cs |
728x90
'웹 > spring' 카테고리의 다른 글
MYBatis - Mapper xml파일 List로 리턴 (0) | 2022.08.23 |
---|---|
Spring MVC Framework/스프링 mvc 구조 이해/ 스프링 mvc 주요 어노테이션 (0) | 2022.08.16 |
[의존성주입]DI/Dependency Injdection/ 애노테이션/애너테이션/어노테이션- @Autowired, @Resource, @Inject (0) | 2022.08.12 |
스프링 기본 프로젝트 셋업 순서 연습/spring spring legacy project setup/ 스프링 셋업 버전 (0) | 2022.08.03 |
[스프링/spring] 설치 및 셋업 (0) | 2022.07.27 |
Comments