2021. 1. 28. 17:52ㆍBACK END/SPRING
* p.248
스프링이 제공하는 인터페이스나 클래스
DispatcherServlet
- 창구 역할
- 요청이 들어오면 어디로 보낼지 정함
- Controller로부터 return 받음 (model 등)
HandlerMapping
- 관련 url 검색을 하고 나면 요청 url과 매핑되는 Controller 검색을 HandlerMapping에서 처리
View
- 응답결과를 출력해줌
- 예시 hello.jsp
개발자가 직접 구현해야 할 것
- 클라이언트의 요청을 처리해주는 Controller
- 클라이언트의 응답 결과를 보여 줄, 결과물 JSP 파일
p.290
Human Bean 회원가입 양식 만들기
라디오 버튼, 체크 박스, 콤보박스 등 하드코딩하지 않고 동적으로 만들기 (유효성 검사도 ... )
실행되는 프로세스는 다음과 같습니다.
• human.jsp → "/second/mycaller" 요청 → humanChoice.jsp → "second/caller3" 요청 →
humanStart.jsp → "second/caller3" 요청 → humanEnd.jsp
human.jsp
- 파일 생성(HelloWorld.jsp 복붙)
<html>
<head>
<meta charset="UTF-8">
<title>최초 시작 파일입니다.</title>
</head>
<body>
<%
String contextPath = request.getContextPath();
String gotopage = contextPath + "/mycaller1.hm";
response.sendRedirect(gotopage);
%>
</body>
</html>
web.xml
human.jsp의 확장자를 통해 경로 확인
human-context.xml
beans:bean태그 human으로 수정 -> 최종 경로
context:component-scan태그 controller로 수정 -> 컨트롤러
HumanController.java 생성
스캔을 위해 @Controller, @RequestMapping 등 코딩
@Controller
public class HumanController {
@RequestMapping(value="/mycaller1.hm", method = RequestMethod.GET)
public ModelAndView form() {
ModelAndView mav = new ModelAndView();
mav.setViewName("humanStart");
return mav;
}
//GetMapping, PostMapping 등을 사용할 수 있음
@RequestMapping(value="/caller3.hm", method = RequestMethod.POST)
public ModelAndView doPost(Human human) {
ModelAndView mav = new ModelAndView();
System.out.println(human.toString());
mav.setViewName("humanEnd");
return mav;
}
human폴더 생성 > humanStart.jsp 생성
테스트 human.jsp 파일 실행
http://localhost:8989/WebProject/mycaller1.hm
위 링크 생성 확인하기
humanStart.jsp
post방식으로 양식 만들기
아래 form, table 만들기
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>humanStart.jsp 파일입니다.</h2>
<!-- 확장자 hm, post 방식으로 넘어감 -->
<form action="<%=request.getContextPath()%>/caller3.hm" method="post">
<table border="1">
<tr>
<td width="25%" align="center">
아이디
</td>
<td width="75%" align="left">
<input id="id" name="id" value="" />
</td>
</tr>
<tr>
<td width="25%" align="center">
이름
</td>
<td width="75%" align="left">
<input id="name" name="name" value="" />
</td>
</tr>
<!-- 취미 type="radio", input태그의 id는 중복되기 때문에 삭제 -->
<tr>
<td width="25%" align="center">
취미
</td>
<td width="75%" align="left">
<input name="hobby" type="radio" value="독서" />독서
<input name="hobby" type="radio" value="야구" />야구
</td>
</tr>
<!-- 특기 type="checkbox", input태그의 id는 중복되기 때문에 삭제 -->
<tr>
<td width="25%" align="center">
특기
</td>
<td width="75%" align="left">
<input name="special" type="checkbox" value="중국어" />중국어
<input name="special" type="checkbox" value="아랍어" />아랍어
</td>
</tr>
<tr>
<td width="25%" align="center">
생일
</td>
<td width="75%" align="left">
<input id="birth" name="birth" value="" />
</td>
</tr>
<!-- 직업 select태그, option태그 -->
<tr>
<td width="25%" align="center">
직업
</td>
<td width="75%" align="left">
<select name="job">
<option value="--">---선택해 주세요.
<option value="직원">직원
<option value="학생">학생
</select>
</td>
</tr>
<tr>
<td width="25%" align="center">
가입일자
</td>
<td width="75%" align="left">
<input id="regdate" name="regdate" value="" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="가입하기" />
</td>
</tr>
</table>
</form>
</body>
</html>
커멘드클래스(Human.java) 생성
- jsp에서는 한건을 의미하는 객체를 bean 객체 또는 bean 클래스라고 부르는데,
스프링에서는 이것을 커멘드(command)객체라고 부릅니다.
- humanStart.jsp에 들어간 파라미터(name)들을 모두 변수로 생성합니다.
- getter, setter, 생성자, toString() 메소드 구현합니다.
humanStart.jsp
- action태그 지정
<form action="<%=request.getContextPath()%>/caller3.hm" method="post">
Controller파일 코딩
HumanController
@RequestMapping(value="/caller3.hm", method = RequestMethod.POST) 작성
doPost()메소드 작성
humanEnd파일 작성해야 함
humanEnd.jsp 파일 생성
human폴더 생성 > humanEnd.jsp
텍스트 작성...
+설명)
컨트롤러 > 메소드 > jsp로 이동
humanStart.jsp의 파라미터 7개니까 HumanController에서도 작성...
하지만, 이전 처럼 파라미터를 하나하나 갖고 오지 않음..
HumanController.java
기존 방식) jsp의 파라미터만큼 코드를 만들어야 하는데,,
그럼 100개의 파라미터일 때 Param도 100개..?
public ModelAndView doPost(
@RequestParam(value="id", required=true) String id,
@RequestParam(value="name", required=true) String name
)
새로운 방식) 아래처럼 커멘드 객체 Human human로 전체를 가져옴
public ModelAndView doPost( Human human ) {
...
System.out.println(human.toString());
}
Servlet필터 이용하여 한글 깨짐에 대한 필터 구현
* p.253
이전 인코딩 방식) request.CharacterEncoding("UTF-8");
하지만 파일 하나하나에 해당 코드를 추가해야하는 불편함이 있음
새로운 방식) web.xml > filter
<!-- 인코딩 필터 등록 -->
<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>
모든 요청에 대해서 이 필터가 개입하여 인코딩을 UTF-8로 하게 함.
forceEncoding - 강제로 변환
'BACK END > SPRING' 카테고리의 다른 글
Mybatis, DB table, CRUD 작업 수행 (0) | 2021.02.02 |
---|---|
MVC 모델2 회원가입 예제 (0) | 2021.02.01 |
MvcBasic 프로젝트 생성 (0) | 2021.01.28 |
20210126 Spring 복습 (0) | 2021.01.26 |
20210125 Spring (0) | 2021.01.25 |