JAVA 웹개발 과정 국비 81일차 정리

81일차

※ 배우는 과정이므로 정확하지 않을 수 있습니다.
잘못된 점은 알려주시면 수정하도록 하겠습니다!


1. Spring Framework 이어서..

1) 한글 깨짐 방지

MVC2 패턴에서는 request.setChracterEncoding("utf8");을 이용하여 한글깨짐을 방지했었다.
스프링에서는 메소드가 나뉘어져 있기 때문에 한글이 들어오는 메소드마다 이 코드를 다 쳐줘야하는 불편함이 있다.

ServletFilter를 쓰면 간편하게 한글 깨짐을 방지할 수 있다.
Filter라는 단어에서 알 수 있듯이 한번 걸러주는 것인데, 아래 그림과 같은 느낌이다.
ss
모든 request가 Filter를 거치게끔 한번만 처리를 해주면 되니 간편해진다.
web.xml 안에 아래 코드를 추가하면 된다.

<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>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

2) DataTables

그렇게 찾고 또 찾았던 게시판 API이다.
가져다 쓰기만 하면 리스트 출력부터 페이징까지 다 처리해준다.
단점은 모든 API가 그렇듯 사용법을 익혀야 한다는 것이다.
또한 클라이언트 쪽 부하가 많이 생겨서 데이터가 많아지면 로딩이 길어질 수도 있다.
급할때는 사용하기 좋을 것 같다.

3) ajax 한글 깨짐

redirectforward를 통해 jsp로 한글 데이터가 도착하면 아래 코드가 있기 때문에 한글이 깨지지 않는다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

그러나 ajax로 한글 데이터를 받는 것은 위의 코드가 관여하는 것이 아니므로 따로 설정해줘야한다.
컨트롤러에서 ajax로 응답을 돌려보낼 메소드 @RequestMapping에 아래처럼 적어준다.
쉽게 말해 돌려보내는 ajax 응답에 한글처리 규칙을 넣어준다고 생각하면 된다.

@ResponseBody
@RequestMapping(value="idDuplCheck", produces="text/html;charset=utf8")
public String idDuplCheck(String id) throws Exception {
	int result = dao.idDuplCheck(id);
	System.out.println(result);
	return String.valueOf(result);
}

2. MyBatis

1) MyBatis란?

데이터베이스 관련 Framework이다.
영속성 프레임워크라고도 하는데 한번 만들어놓으면 오랜동안 사용할 수 있고, DB와 관련된 프레임워크다라는 뜻도 가지고 있기 때문이다.

2) 특징

  • Query문과 자바가 분리됨(이식성 향상)
  • 작성 문법의 경제성 향상
  • 동적 쿼리 생성에 유리하다.
  • 상대적으로 쉽다.

3) 환경설정

① 라이브러리 가져오기

Maven Repository에서 MyBatisMyBatis Spring을 둘 다 가져온다.

② 인스턴스 생성 코드 적어주기

root-context.xml에 bean태그로 인스턴스 생성 코드를 아래와 같이 적어준다.
DBCP는 똑같이 써야하기 때문에 dataSource를 setter로 넣어준다.
두번째 setter 코드는 mybatis가 xml문서를 끌어다 써야하는데 그 xml문서의 위치가 어딨는지 알려주는 것이다.
classpathsrc/main/resources 폴더를 가리킨다.

<bean id="seqSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<property name="mapperLocations" value="classpath:/mybatis/*-mapper.xml"/>
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>

댓글남기기