본문 바로가기

IT칼럼/JAVA

iframe 으로 get request 이용하여 데이터 전달하기 + spring boot

1.  main.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>iframe main</title> 
    </head>
    <body>
        <form id="loginForm" target="myFrame"  action="/iframe" method="get"> 
            <input type="text" name="username" value=“username” />
            <input type="text" name="password" value="password" /> 
            <input type="submit">
        </form>
        <iframe name="myFrame" src="/iframe">
            Your browser does not support inline frames.
        </iframe>
    </body>
</html>

2. iframe.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <p th:text="${username}"></p>
        <p th:text="${password}"></p>
    </body>
</html>

3. spring controller

@GetMapping(“/index”)
public String iframeHome(){
    return “/index”;
}

@GetMapping("/iframe")
public String iframeTest(@RequestParam Map<String, String> requestParams, Model model){
    String username = requestParams.get("username");
    String password = requestParams.get("password");
    model.addAttribute("username", username);
    model.addAttribute("password", password);
    return "/iframe_test";