programming/Spring

Spring - 기초편3

히연쓰 2021. 6. 1. 17:12

1) 파일 이름 : 03-SpringParameter

2) 앞에서 사용한 기본적인 파일 

    1. com.heeyeon.config -> package

       ① . RootAppContext.java

       ② . ServletAppContext.java

       ③ . SpringConfigClass.java

     2. com.heeyeon.controller -> package

       ① . HomeController.java

3) 파일 자체에 pom.xml 넣기

 

-----------------------------------------------------------------------------------------------------------

 

1) TestController

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.heeyeon.controller;
 
import javax.servlet.http.HttpServletRequest;
 
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.RequestParam;
 
@Controller
public class TestController {
    
    @GetMapping("/Parameter01.do")
    public String parameter01(HttpServletRequest request) {
        
        String data01 = request.getParameter("data01");
        String data02 = request.getParameter("data02");
        String [] data03 = request.getParameterValues("data03");
        System.out.println("data01=="+data01);
        System.out.println("data02=="+data02);
        System.out.println("data02=="+(data01+data02));
        
        //System.out.println("data03=="+data03);
        for(String tempStr : data03) {
            System.out.println("data03==="+tempStr);
        }
        return "parameter01";
    }
    
    @PostMapping("/Parameter02.do")
    public String parameter02(HttpServletRequest request) {
        String data01 = request.getParameter("data01");
        String data02 = request.getParameter("data02");
        String [] data03 = request.getParameterValues("data03");
        System.out.println("data01=="+data01);
        System.out.println("data02=="+data02);
        //System.out.println("data03=="+data03);
        for(String tempStr : data03) {
            System.out.println("data03==="+tempStr);
        }
        return "parameter02";
    }
    
    @GetMapping("/Parameter03.do")
    public String parameter03(
                                @RequestParam(value="data01"int data01,
                                @RequestParam(value="data02"int data02,
                                @RequestParam(value="data03"int [] data03
            
                             ) {
        System.out.println("data01==="+data01);
        System.out.println("data02==="+data02);
        System.out.println("data01+data02==="+(data01+data02));
        for(int tempInt : data03) {
            System.out.println("data03==="+tempInt);
        }
        return "parameter03";
    }
    
    
    @GetMapping("/Parameter04.do"
    public String parameter04 (
                @RequestParam int data01,
                @RequestParam int data02,
                @RequestParam int [] data03
             ) {
    // @RequestParam(value=="data01" int data01,
                // @RequestParam(value=="data01") 넘어오는 데이터
                // int data01 넘어오는 데이터를 내가 만든 변수에 담을  때
                System.out.println("data01===" +data01);
                System.out.println("data02===" +data02);
                System.out.println("data01+data02===" + (data01+data02));
                
            for(int tempInt : data03) {
                System.out.println("data03===" +tempInt);
            }
        return "parameter04";
    }
    
    @GetMapping("/Parameter05.do"
    public String parameter05 (
                @RequestParam int data01,
                // (required = false) String 이걸 써주면 체크 안하거나 글을 안 써도 넘어간다. int 에서 String으로 바꿔줘야함.
                @RequestParam (required = falseString data02,
                // data03이 비어도(체크 안해도) 값이 넘어가도록 함 (defaultValue = "0" )
                @RequestParam (defaultValue = "0" ) int data03
             ) {
    // @RequestParam(value=="data01" int data01,
                // @RequestParam(value=="data01") 넘어오는 데이터
                // int data01 넘어오는 데이터를 내가 만든 변수에 담을  때
                System.out.println("data01===" +data01);
                System.out.println("data02===" +data02);
                System.out.println("data01+data02===" + (data01+data02));
                System.out.println("data03===" +data03);
        
        return "parameter05";
    }
    
    
}
 
cs

 

-----------------------------------------------------------------------------------------------------------

 

1) parameter01.jsp

 

 

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<h1>parameter01</h1>
</body>
</html>
cs

 

2) parameter02.jsp

 

 

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<h1>parameter02</h1>
</body>
</html>
cs

 

3) parameter03.jsp

 

 

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<h1>parameter03</h1>
</body>
</html>
cs

 

4) parameter04.jsp

 

 

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<h1>parameter04</h1>
</body>
</html>
cs

 

5) parameter05.jsp

 

 

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<h1>parameter05</h1>
</body>
</html>
cs

 

-----------------------------------------------------------------------------------------------------------

 

→ index.jsp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<%@ 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>
<ul>
<li>
<a href="Parameter01.do?data01=10&data02=20&data03=30&data03=40">
    parameter01-queryString으로 데이터 보내기
</a>
</li>
</ul>
 
<form method="POST" action="Parameter02.do">
    <div>
        <label>
            <span>data01</span>
            <input type="text" name="data01">
        </label>
    </div>
    <div>
        <label>
            <span>data02</span>
            <input type="text" name="data02">
        </label>
    </div>
    <div>
        <label>
            <span>data03-30</span>
            <input type="checkbox" name="data03" value="30">
        </label>
        <label>
            <span>data03-40</span>
            <input type="checkbox" name="data03" value="40">
        </label>
        <label>
            <span>data03-50</span>
            <input type="checkbox" name="data03" value="50">
        </label>
    </div>
    <button type="submit">Parameter02 Submit </button>
</form>
<hr>
<form method="GET" action="Parameter03.do">
    <div>
        <label>
            <span>data01</span>
            <input type="text" name="data01">
        </label>
    </div>
    <div>
        <label>
            <span>data02</span>
            <input type="text" name="data02">
        </label>
    </div>
    <div>
        <label>
            <span>data03-30</span>
            <input type="checkbox" name="data03" value="30">
        </label>
        <label>
            <span>data03-40</span>
            <input type="checkbox" name="data03" value="40">
        </label>
        <label>
            <span>data03-50</span>
            <input type="checkbox" name="data03" value="50">
        </label>
    </div>
    <button type="submit">Parameter03 Submit</button>
</form>
<hr>
<form method="GET" action="Parameter04.do">
    <div>
        <label>
            <span>data01</span>
            <input type="text" name="data01">
        </label>
    </div>
    <div>
        <label>
            <span>data02</span>
            <input type="text" name="data02">
        </label>
    </div>
    <div>
        <label>
            <span>data03-30</span>
            <input type="checkbox" name="data03" value="30">
        </label>
        <label>
            <span>data03-40</span>
            <input type="checkbox" name="data03" value="40">
        </label>
        <label>
            <span>data03-50</span>
            <input type="checkbox" name="data03" value="50">
        </label>
    </div>
    <button type="submit">Parameter04 Submit</button>
</form>
<hr>
<form method="GET" action="Parameter05.do">
    <div>
        <label>
            <span>data01</span>
            <input type="text" name="data01">
        </label>
    </div>
    <div>
        <label>
            <span>data02</span>
            <input type="text" name="data02">
        </label>
    </div>
    <div>
        <label>
            <span>data03-30</span>
            <input type="checkbox" name="data03" value="30">
        </label>
        <label>
            <span>data03-40</span>
            <input type="checkbox" name="data03" value="40">
        </label>
        <label>
            <span>data03-50</span>
            <input type="checkbox" name="data03" value="50">
        </label>
    </div>
    <button type="submit">Parameter05 Submit</button>
</form>
<hr>
 
</body>
</html>
 
cs

 

-----------------------------------------------------------------------------------------------------------

 

Project Explorer

 

-----------------------------------------------------------------------------------------------------------

 

결과 화면

 

012