본문 바로가기
[study]이론정리/Spring Boot

Spring MVC - 요청파라미터얻기2 (멀티값)

by yoon9i 2024. 7. 2.

4-1> 요청 파라미터 얻기2-멀티값(name 하나에 value 여러개)
- 이전 서블릿/jsp 의 request.getParameterValues("name") 역할

방법1: @RequestParam + 배열로 처리

  @PostMapping("/member1")
  public String member1(@RequestParam("userid") String userid,
                        @RequestParam("passwd") String passwd,
                        @RequestParam("email") String [] email) {

    logger.info("logger: userid:{}, passwd: {}, email: {}", userid, passwd, Arrays.toString(email));
    
    return "redirect:xxx"; 
  }

방법2: @RequestParam + List 처리

  @PostMapping("/member2")
public String member2(@RequestParam("userid") String userid,
                        @RequestParam("passwd") String passwd,
                        @RequestParam("email") List<String> email) {

logger.info("logger: userid:{}, passwd: {}, email: {}", userid, passwd, email);

return "redirect:xxx"; 
}

방법3: DTO (*)

  @PostMapping("/member")
public String member(MemberDTO dto) {

logger.info("logger: MemberDTO: {}", dto);

return "redirect:xxx"; 
}

 

package com.exam;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
package com.exam.controller;

import java.util.Arrays;
import java.util.List;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.exam.dto.MemberDTO;

@Controller
public class MainController {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	// 화면 요청
	@GetMapping("/member")
	public String list() {
		return "memberForm"; 
	}
	
	
	// 로직 요청
	// 방법1
	@PostMapping("/member1")
	public String member1(@RequestParam("userid") String userid,
						 @RequestParam("passwd") String passwd,
						 @RequestParam("email") String [] email) {

		logger.info("logger: userid:{}, passwd: {}, email: {}", userid, passwd, Arrays.toString(email));
		
		return "redirect:xxx"; 
	}
	
	// 방법2
	@PostMapping("/member2")
	public String member2(@RequestParam("userid") String userid,
						 @RequestParam("passwd") String passwd,
						 @RequestParam("email") List<String> email) {
			
		logger.info("logger: userid:{}, passwd: {}, email: {}", userid, passwd, email);
		
		return "redirect:xxx"; 
	}
	
	// 방법3
	@PostMapping("/member")
	public String member(MemberDTO dto) {
			
		logger.info("logger: MemberDTO: {}", dto);
		
		return "redirect:xxx"; 
	}
	
	
	
	// PRG 패턴적용
	@GetMapping("/xxx")
	public String hello() {
		return "hello"; 
	}

}
package com.exam.dto;

import java.util.List;

public class MemberDTO {
	
	String userid;
	String passwd;
	
	List<String> email;

	// 생성자
	public MemberDTO() {
		super();
		// TODO Auto-generated constructor stub
	}

	public MemberDTO(String userid, String passwd, List<String> email) {
		super();
		this.userid = userid;
		this.passwd = passwd;
		this.email = email;
	}

	// getter & setter
	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public List<String> getEmail() {
		return email;
	}

	public void setEmail(List<String> email) {
		this.email = email;
	}

	// toString
	@Override
	public String toString() {
		return "LoginDTO [userid=" + userid + ", passwd=" + passwd + ", email=" + email + "]";
	}
	
	
}
<%@ page 
         contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"        
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>안녕하세요</p>
<p>Hello</p>
</body>
</html>
<%@ page 
         contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"        
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
	<title>Insert title here</title>
</head>
<body>
	<h2>로그인화면</h2>
	<form action="member"  method="post">
	아이디:<input type="text" name="userid"><br>
	비번:<input type="text" name="passwd"><br>
	
	이메일:<input type="text" name="email"><br>
	이메일:<input type="text" name="email"><br>
	
	<input type="submit" value="저장">
</form>
</body>
</html>
# application.properties
logging.level.org.springframework=info

# tomcat port 번호 변경
server.port=8090

# context 명 변경
server.servlet.context-path=/app

# jsp의 경로와 확장자 지정
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp


spring.mvc.pathmatch.matching-strategy=ant-path-matcher
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.18</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.exam</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-devtools</artifactId>
		</dependency>
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<scope>provided</scope>
	</dependency> 
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
	</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>