[spring] Spring MVC: Model Interface, RequestParam Annotation, Spring MVC form
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์ ๋ํ ์์ฑ์ ๊ฐ์ง๊ณ ์๋์ง ๋ํ๋ธ๋ค. |
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";
}
}
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>
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์ด ์๋ ๊ฒฝ์ฐ
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 + ์์ฑ ์ด๋ฆ์ผ๋ก ์ค์ ํด๊ฒฐํ ์ ์์๋ค.
JWT (JSON Web Token) (0) | 2022.01.28 |
---|---|
[Spring] Android, Spring ์ฐ๋ (retrofit2) (1) | 2022.01.24 |
[Spring] Spring, MongoDB ์ฐ๋ (Spring Data MongoDB) (0) | 2022.01.20 |
[Spring] Spring MVC : Multiple view pages, Multiple Controllers (0) | 2022.01.18 |
[Spring] Eclipse Spring ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ถ (0) | 2022.01.17 |