728x90
Controller
package com.app.test;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
@Controller
public class TestController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String main(LoginDTO dto) {
System.out.println("login - loginForm - GET" + dto);
return "loginForm";
}
@RequestMapping(value = "/login1", method = RequestMethod.POST)
public String login(LoginDTO dto) { //자동으로 request.SetAttribute("loginDTO", "dto")
System.out.println("login1 - loginForm - POST" +dto);
return "login1"; //자동으로 데이터 fowarding
}
@RequestMapping(value = "/login2", method = RequestMethod.POST)
public ModelAndView login2(LoginDTO dto) {
ModelAndView mav = new ModelAndView();
mav.addObject("login", dto);
mav.setViewName("login2");
System.out.println("login2 - loginForm - POST" +dto);
return mav; //자동으로 데이터 fowarding
}
@RequestMapping(value = "/login3", method = RequestMethod.POST)
public String login3(@ModelAttribute("xxx") LoginDTO dto) { //dto의 키값이 xxx
System.out.println("login3 - loginForm - POST" +dto);
return "login3"; //자동으로 데이터 fowarding
}
}
login2_jsp
<%@page import="com.app.test.LoginDTO"%>
<%@ 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>
out.print:
<%
LoginDTO dto = (LoginDTO)request.getAttribute("loginDTO");
String name = dto.getUserid();
out.print(name);
%><br>
getAttribute: <%= dto.getUserid() + "\t" + dto.getPasswd()%><br>
getParameter: <%= request.getParameter("userid") + "\t" + request.getParameter("passwd") %><br>
EL: ${login} / ${login.userid} / ${login.passwd}
</body>
</html>
login3_jsp
<%@page import="com.app.test.LoginDTO"%>
<%@ 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>
out.print:
<%
LoginDTO dto = (LoginDTO)request.getAttribute("xxx");
String name = dto.getUserid();
out.print(name);
%><br>
getAttribute: <%= dto.getUserid() + "\t" + dto.getPasswd()%><br>
getParameter: <%= request.getParameter("userid") + "\t" + request.getParameter("passwd") %><br>
4번: ${login}
</body>
</html>
============================================================================================
Controller2
package com.app.test;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
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 TestController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String main() {
System.out.println("main - loginForm - GET");
return "loginForm";
}
@RequestMapping(value = "/aaa", method = RequestMethod.GET)
public ModelAndView xxx(ArrayList<String> list) {
list.add("Hong1");
list.add("Hong2");
ModelAndView mav = new ModelAndView();
mav.addObject("list",list); //JSP에서 사용할 수 있는 KV
mav.setViewName("main"); //보낼 JSP파일 명
System.out.println("login - loginForm - GET");
return mav;
}
@RequestMapping(value = "/bbb", method = RequestMethod.GET)
public String xxx2(@ModelAttribute("xxx") ArrayList<String> list) {
list.add("Lee1");
list.add("Lee2");
System.out.println("login - loginForm - GET");
return "main2";
}
}
main
<% List<String> list = (List<String>)request.getAttribute("list"); %>
EL: ${list}<br>
<%= list %>
main2
<% List<String> list = (List<String>)request.getAttribute("xxx"); %>
el tag: ${xxx}2<br>
request.getAttribute: <%= list %>
Controller3_Get
package com.app.test;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
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 TestController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String main(LoginDTO dto) {
System.out.println("login - loginForm - GET" + dto);
return "loginForm";
}
@ModelAttribute("xxx") //리턴되는 객체에 xxx키 값 부여(이 객체를 다른 함수에서 사용 가능)
public ArrayList<String> getList() { //자동으로 request.SetAttribute("loginDTO", "dto")
System.out.println("login1 - loginForm - POST");
ArrayList<String> list = new ArrayList<String>();
list.add("hong1");
list.add("hong2");
return list; //자동으로 데이터 fowarding
}
@RequestMapping(value = "/aaa")
public String aaa(@ModelAttribute("xxx") ArrayList<String> list) {
System.out.println("aaa 추가요청 작업");
list.add("aaa");
return "main2"; //키: xxx
}
}
Controller_Model
package com.app.test;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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 TestController {
@RequestMapping(value = "/xxx", method = RequestMethod.GET)//***************************************
public String xxx(Model m) {
m.addAttribute("username", "홍길동"); //request.setAttribute와 동일(req.getAtt로 사용)
m.addAttribute("login", new LoginDTO("aaa","111")); //login key에 object 저장
System.out.println("login - loginForm - GET - MODEL");
return "main";
}
@RequestMapping(value = "/xxx2", method = RequestMethod.GET)
public String xxx2(Map<String, String> map) {
map.put("address", "서울");
map.put("username", "홍길동");
System.out.println("login - loginForm - GET - MODEL");
return "main2";
}
@RequestMapping(value = "/xxx3", method = RequestMethod.GET)
public String xxx3(HttpServletRequest request) {
request.setAttribute("username", "이순신");
request.setAttribute("address", "경기");
System.out.println("login - loginForm - GET - MODEL");
return "main3";
}
}
Main1
<%@page import="com.app.test.LoginDTO"%> <%@ 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> out.pring: <% LoginDTO dto = (LoginDTO)request.getAttribute("login"); String name = dto.getUserid(); out.print(name); %><br> getAttribute: <%= dto %><br> EL태그: ${login}<br> EL태그: ${login.userid} / ${login.passwd} <br> EL태그: ${username} </body> </html>
Main2
<%@page import="com.app.test.LoginDTO"%>
<%@ 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>
getAttribute: <%= request.getAttribute("address") %> /
getAttribute: <%= request.getAttribute("username") %><br>
EL태그: ${address} / ${username} <br>
</body>
</html>
Main3
<%@page import="com.app.test.LoginDTO"%>
<%@ 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>
getAttribute: <%= request.getAttribute("address") %> /
getAttribute: <%= request.getAttribute("username") %><br>
EL태그: ${address} / ${username} <br>
</body>
</html>
'단순 코드 기록 > Spring' 카테고리의 다른 글
Spring_Redirect (0) | 2024.02.13 |
---|---|
Spring_Filter (0) | 2024.02.13 |
Spring_Cookie (0) | 2024.02.13 |
Spring_Parsing (0) | 2024.02.08 |
Spring_Multi_controller (0) | 2024.02.08 |