์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[spring] Spring MVC: Model Interface, RequestParam Annotation, Spring MVC form

PROGRAMMING/Backend

by koharin 2022. 1. 19. 23:15

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ป Model

  • Spring MVC์—์„œ model์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” container์ด๋‹ค.
  • ๋ฐ์ดํ„ฐ๋Š” ๊ฐ์ฒด, ์ŠคํŠธ๋ง, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ๋ถ€ํ„ฐ์˜ ์ •๋ณด ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
  • model interface๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด controller์— ์ค˜์•ผ ํ•œ๋‹ค.
  • HttpServletRequest ๊ฐ์ฒด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •๋ณด๋ฅผ ์ฝ์–ด์„œ Model ์ธํ„ฐํŽ˜์ด์Šค์— ์ „๋‹ฌํ•˜๊ณ , view ํŽ˜์ด์ง€์—์„œ๋Š” model๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•ด์„œ ์ฝ๋Š”๋‹ค.

 

๐Ÿ’ป Model Interface

Method Description
Model addAllAttributes(Collection<?> arg) Collection์—์„œ Map์œผ๋กœ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
Model addAllAttributes(Map<String,?> arg) Map์—์„œ Map์œผ๋กœ ๋ชจ๋“  ์†์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
Model addAllAttribute(Object arg) ์ œ๊ณตํ•œ ์ด๋ฆ„์œผ๋กœ Map์— ๋ชจ๋“  ์†์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
Model addAllAttribute(String arg0, Object arg1) ์ œ๊ณตํ•œ ์ด๋ฆ„์œผ๋กœ ์†์„ฑ์„ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
Map<String, Object> asMap() ํ˜„์žฌ model์˜ ์†์„ฑ๋“ค์„ Map ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Model mergeAttributes(Map<String, ?> arg) ์ด์ „์— ๋งŒ๋“  ์กด์žฌํ•˜๋Š” ๋™์ผํ•œ ์ด๋ฆ„์˜ ๊ฐ์ฒด์™€ ํ•จ๊ป˜ ๋ชจ๋“  ์†์„ฑ๋“ค์„ ์ œ๊ณตํ•œ Map์—์„œ Map์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.
boolean containsAttribute(String arg) ํ•ด๋‹น model์ด ์ฃผ์–ด์ง„ name์— ๋Œ€ํ•œ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

 

๐Ÿ’ป Spring MVC Model & RequestParam Annotation 

HomeController.java

package org.smu.blood;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);

		return "home";
	}
}

 

UserController.java

package org.smu.blood;

import org.springframework.beans.factory.annotation.Autowired;
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
public class UserController {
	
	@RequestMapping(value="/signIn")
	public String post(@RequestParam("userId") String userId, @RequestParam("password") String password, @RequestParam("nickname") String nickname, Model model) {
		if(password.equals("admin")) {
			model.addAttribute("message", "Hello, " + nickname);
			return "viewPage";
		}
		else return "errorPage";
	}
	
}
  • signIn ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด UserController์—์„œ ํ•ด๋‹น ์š”์ฒญ์„ ๋ฐ›๋Š”๋‹ค.
  • ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
  • password ๊ฐ’์ด admin์ธ ๊ฒฝ์šฐ model์˜ message ์†์„ฑ ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  viewPage๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ ๋กœ๋“œํ•˜๋„๋ก ํ•œ๋‹ค.
  • password๊ฐ€ admin์ด ์•„๋‹Œ ๊ฒฝ์šฐ errorPage ์ด๋ฆ„์˜ view๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ errorPage.jsp๋ฅผ ๋กœ๋“œํ•˜๋„๋ก ํ•œ๋‹ค.

 

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<body>
<h1>
	Sign In  
</h1>
<form action="signIn">
	UserName <input type="text" name="userId"/> <br>
	Password <input type="text" name="password"/> <br>
	nickName <input type="text" name="nickname"/> <br>
<br>
<input type="submit" name="submit"/>
</form>
<br>
</body>
</html>
  • ํผ์„ ์ œ์ถœํ•˜๋ฉด signIn ์š”์ฒญ์ด DispatcherServlet์— ์ „๋‹ฌ๋ผ์„œ UserController์— ํ•ด๋‹น ์š”์ฒญ์ด ์ „๋‹ฌ๋˜์–ด ์ฒ˜๋ฆฌ๋œ๋‹ค.

 

viewPage.jsp

<%@ 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>
${message}
</body>
</html>

 

errorPage.jsp

<%@ 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>
<p> No Such User Information </p>
<input type="button" value="back" actoin="goBack"/>
</body>
</html>

 

password๊ฐ€ admin์ธ ๊ฒฝ์šฐ

 

 

password๊ฐ€ admin์ด ์•„๋‹Œ ๊ฒฝ์šฐ

 

 

๐Ÿ’ป Spring MVC Form

User.java

package org.smu.blood.database;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/*
 * Spring Data MongoDB์—์„œ User ํด๋ž˜์Šค๋ฅผ ์ž๋™์œผ๋กœ User ์ด๋ฆ„์˜ collection์— ๋งคํ•‘ํ•ด์„œ 
 * @Document annotation ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ์ƒ๊ด€์—†์Œ. 
 */
@Document(collection="User")
public class User {
	@Id
	private String userId; // _id๋กœ ์ง€์ •
	private String password;
	private String nickname;
	
	public User() {}
	
	public User(String userId, String password, String nickname) {
		this.userId = userId;
		this.password = password;
		this.nickname = nickname;
	}
	public String getuserId() {
		return userId;
	}
	public String getpassword() {
		return password;
	}
	public String getnickname() {
		return nickname;
	}
	public String toString() {
		return String.format("User[userId:%s, password: %s, nickname: %s]", userId, password, nickname);
	}
}

 

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<body>
<h1>
	Sign In  
</h1>
<form:form action="signIn" modelAttribute="User">
	UserName <input type="text" name="userId"/> <br>
	Password <input type="text" name="password"/> <br>
	nickName <input type="text" name="nickname"/> <br>
<br>
<input type="submit" name="submit"/>
</form:form>
<br>
</body>
</html>

 

viewPage.jsp

<%@ 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>
Welcome, ${user.nickname} <br>
userId: ${user.userId } <br>
password: ${user.password } <br>
nickname: ${user.nickname } <br> 
</body>
</html>

 

UserController.java

package org.smu.blood;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import org.smu.blood.database.UserRepository;
import org.smu.blood.database.User;

@Controller
public class UserController {
	
	@RequestMapping(value="/signIn")
	public String post(@RequestParam("userId") String userId, @RequestParam("password") String password, @RequestParam("nickname") String nickname, Model model) {
		User user = new User(userId, password, nickname);
		model.addAttribute("user", user);
		return "viewPage";
	}
	
	
}

 

ํƒ€์ž… ~ ์—์„œ ํ”„๋กœํผํ‹ฐ (์†์„ฑ)์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์˜ค๋ฅ˜์˜ ๊ฒฝ์šฐ, Entity์ธ User ํด๋ž˜์Šค์—์„œ ๊ฐ ์†์„ฑ์— ๋Œ€ํ•œ getter ํด๋ž˜์Šค๋ฅผ   get + ์†์„ฑ ์ด๋ฆ„์œผ๋กœ ์ค˜์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ