🌈Backend/Spring

#Spring @RequestParam사용법

창히 2021. 1. 18. 22:40

2021.01.18

@RequestParam

-요청 파라미터 값을 저장하기 위한 객체를 설정한다.

기본적으로 어노테이션 없이 input태그의 name값으로 파라미터를 갖고 올 수도 있지만 여러개인 경우 

맵객체로 받아와 사용 가능함.

 

Controller

	@PostMapping("password")
	public ModelAndView loginCheckpassword(@RequestParam Map<String,String> param, HttpSession session ) {
		ModelAndView mav = new ModelAndView();
		System.out.println(param);
		return mav;
	}

메소드 요청 방식이 Post방식인 경우 PostMapping에 의해 loginCheckpassword가 호출

 

jsp

	<form action="password.shop" method="post" name="f"
		onsubmit="return inchk(this)">
		<table>
			<caption>비밀번호 변경</caption>
			<tr>
				<th>현재 비밀번호</th>
				<td><input type="password" name="password"></td>
			</tr>
			<tr>
				<th>변경 비밀번호</th>
				<td><input type="password" name="chgpass"></td>
			</tr>
			<tr>
				<th>변경 비밀번호 재입력</th>
				<td><input type="password" name="chgpass2"></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input
					class="w3-submit w3-black" type="submit" value="비밀번호 변경"></td>
			</tr>
		</table>
			</form>

form태그 내부의 파라미터 값들이 Map객체로 들어간다.

key - input태그의 name값 /  Value - input태그의 value값

 

위 예제같은 경우는 key값으로는 password, chgpass, chgpass2가 들어갈 수 있고 각각 입력해준 값들이 Value값으로 들어가게 되는 것이다.

 

 

호출시 결과 - {password=입력값1, chgpass=입력값2, chgpass2=입력값3}

 

 

번외

 

요청파라미터 총정리

  1.  파라미터의 이름과 매개변수의 이름이 같은 경우 
  2.  Bean클래스의 프로퍼티와 파라미터가 같은 경우 Bean클래스의 객체에 저장
  3.  Map객체를 이용하여 파라미터 저장.
반응형