일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 오라클
- 타입스크립트
- 정보처리기사실기정리
- 평일코딩
- ReactNative
- react
- VUE
- 자바스크립트
- spring
- 정보처리기사
- 정보처리기사요약
- php
- 정보처리기사정리
- Java의정석
- 코딩테스트
- 자바스크립트 코딩테스트
- 정보처리기사실기
- 자바의정석
- 정보처리기사실기요약
- 리액트네이티브
- CSS
- Oracle
- 스프링
- 이안의평일코딩
- 국비IT
- javascript
- 자스코테
- 국비코딩
- typescript
- 리액트
- Today
- Total
이안의 평일코딩
Spring 7일차 - XML MVC구조 본문
2020.11.02(월)
MVC => Model클래스 설정 : 요청처리 => 결과값 전송(B)
Model(Back-end)
~VO, ~DAO, ~Manager, ~Controller => 재사용 => Model + View = 서블릿 (DispatcherServlet)
View(Front-end)
CSS, JS, JSP, HTML => 재사용
* 프로그램 => 상태관리 (데이터관리) : 변수 (변수, 클래스)
MVC순서
list.do, insert.do
request
Model클래스의 RequestMapping
를 찾아주는 클래스 (HandlerMapping)
사용자 요청(*.do) ==> DispatcherServlet ==> Model클래스 ============>DAO
request @RequestMapping("insert.do")
JSP를 찾아서 request 전송 => ViewResolver
1. 사용자 요청 (*.do)
2. DispatcherServlet을 요청을 받는다
= 요청처리하는 클래스를 찾는다(HandlerMapping)
= 결과값을 JSP로 전송(ViewResolver)
3. JSP에서 결과값을 받아서 출력
==> 프로그래머 (Model, DAO, JSP)
==> 클래스 (Model 클래스) ==> 반드시 @Controller
OnLineSpringMVCStudy1
src/main/webapp/WEB-INF
web.xml
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
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Controller 등록 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/application-context.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 한글 변환 (request를 사용하지 않는다) -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- /의 형식으로 시작하는 url에 대하여 UTF-8로 인코딩 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
|
cs |
스프링 2.5 이하 => request
스프링 2.5 이상 => 권장 => request를 사용하지 않는다
보안 (request안에는 접속자의 IP를 확인이 가능) => 5.0 이상 (클래스)
데이터 처리 => 전송 (JSP)
src/main/webapp/WEB-INF/config
config폴더에 New-Spring Bean Configuration File
application-context.xml (aop, beans, context, p)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 1. 패키지 단위 등록 -->
<context:component-scan base-package="com.sist.*"/>
<!-- 2. ViewResolver처리 : JSP경로명, 확장자 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/"
p:suffix=".jsp"
/>
<!--
/main/input.jsp
-->
</beans>
|
cs |
src/main/java
com.sist.web
MainController.java
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
|
package com.sist.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
// ID, Password
// 1. 입력하는 JSP
@RequestMapping("main/output.do")
// 매개변수는 DispatcherServlet이 설정해준다 (순서도 상관없다)
public String main_output(HttpServletRequest request) {
String id=request.getParameter("id");
String pwd=request.getParameter("pwd");
request.setAttribute("id", id);
request.setAttribute("pwd", pwd);
return "main/output";
/*
* return "" => forward
* return "폴더명/JSP명"
* "main/main" ==> .jsp를 생략한다
* return "" => sendRedirect
* "redirect:main.do"
*/
}
@RequestMapping("main/input.do")
public String main_input() {
return "main/input";
}
// 전송 ==> Model
@RequestMapping("main/output2.do")
// MemberVO vo
public String main_output2(String id, String pwd, Model model) {
// Model => 인터페이스 (해당 JSP로 데이터 전송 역할)
// 전송
model.addAttribute("id", id);
model.addAttribute("pwd", pwd);
// Spring => 요청값 받는 경우 => 매개변수로 받는다, 전송시에는 Model을 이용해서 전송
/*
* public class Model {
* HttpSerevletRequest request;
* public void addAttribute(String key, Object obj){
* request.setAttribute(key,obj) => DispatcherServlet
* }
* }
*/
return"main/output2";
}
}
|
cs |
src/main/webapp/main
input.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<%@ 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>로그인</h1>
<form method="post" action="../main/output.do">
<input type=text name=id size=15><br>
<input type=password name=pwd size=15><br>
<button>전송</button>
<%--
<input type=submit>
<button>
<input type=image>
--%>
</form>
</body>
</html>
|
cs |
output.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ 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>결과값 출력</h1>
ID:${id }<br>
Password:${pwd }
</body>
</html>
|
cs |
OnLineSpringMVCStudy2
생성후 프로젝트 Properties에서 Java Compiler, Project Facets에서 버젼 1.8로 변경
pom.xml파일 변경
WEB-INF 필요없는 폴더 삭제 후 web.xml에서 셋팅
web.xml
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
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Controller 등록 -->
<servlet>
<!-- Spring에서 자동인식 파일
<servlet-name>-servlet.xml
<servlet-name>a</servlet-name> 이면
a-servlet.xml => WEB-INF
-->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 한글 변환 (request를 사용하지 않는다) -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- /의 형식으로 시작하는 url에 대하여 UTF-8로 인코딩 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
|
cs |
disptacher-servlet.xml (aop, beans, context, p) 메모리할당하고 클래스 찾을 수 있게 해줌
Controller의 return값 앞에 p:prefix 경로 "/main/"추가, p:suffix 뒤 ".jsp"추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.sist.*"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/main/"
p:suffix=".jsp"
/>
</beans>
|
cs |
@RequestMapping("main/")로하면 (반복코딩 제거)
main/생략해서 @GetMapping("main/emp.do") => @GetMapping("emp.do")가능
@RequestMapping => @GetMapping, @PostMapping으로 나뉘어짐 (검색 속도 향상)
src/main/java
com.sist.web
MainController.java
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
|
package com.sist.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("main/")
public class MainController {
/*
@RequestMapping("main/emp.do")
public String main_emp() {
return "main/emp"; // 입력 폼
}
@RequestMapping("main/emp_ok.do")
public String main_emp_ok(EmpVO vo, Model model) { //model=>전송을 위해
model.addAttribute("vo",vo);
return "main/emp_ok"; // request를 전송 => forward
}
*/
// <a> : GET
@GetMapping("emp.do")
public String main_emp() {
return "emp"; // 입력 폼
}
// <form>, ajax
@PostMapping("emp_ok.do")
public String main_emp_ok(EmpVO vo, Model model) { //model=>전송을 위해
model.addAttribute("vo",vo);
return "emp_ok"; // request를 전송 => forward
}
}
|
cs |
EmpVO.java
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
|
package com.sist.web;
/*
* 1. 단일변수
* 2. VO단위
* 3. [] => checkbox
* 4. List단위
*/
public class EmpVO {
private int empno;
private String ename;
private String job;
private String dept;
private String loc;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
|
cs |
form의 action 경로가
emp_ok.do이면 MainController에서 받음 (@Controller)
emp_ok.jsp면 emp_ok.jsp에서 받음
MainController의 @PostMapping("emp_ok.do")에서 return "emp_ok" -> emp_ok.jsp로 보냄
emp.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<%@ 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>사원정보</h1>
<form method="post" action="../main/emp_ok.do">
사번:<input type=text name=empno size=15><br>
이름:<input type=text name=ename size=15><br>
직위:<input type=text name=job size=15><br>
근무지:<input type=text name=dept size=15><br>
부서명:<input type=text name=loc size=15><br>
<button>전송</button>
</form>
</body>
</html>
|
cs |
emp_ok.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%@ 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>결과값 출력</h1>
사번:${vo.empno }<br>
이름:${vo.ename }<br>
직위:${vo.job }<br>
근무지:${vo.loc }<br>
부서명:${vo.dept }
</body>
</html>
|
cs |
'Back-end > Spring' 카테고리의 다른 글
Spring 10일차 - 스프링 MVC (2) | 2020.11.09 |
---|---|
Spring 8일차 - MyBatis 연동 (0) | 2020.11.03 |
Spring 6일차 - ComponentScan (0) | 2020.10.31 |
Spring 5일차 - Repository (0) | 2020.10.31 |
Spring 4일차 - Autowired (0) | 2020.10.23 |