본문 바로가기

Programing/Struts

한글처리

앞서 예제들을 하면서 봐왔겠지만 한글처리가 되지 않는다. 다음과 같이 작업을 하면서 한글처리에 대해 알아보자!

 

1) 한글처리

 

한글 처리라는 것은 그때 그때 해도 되겠지만 Struts에서는 Servlet에서 사용되는 Filter기법을 응용한다.

1) Filter 상속받는 Controller클래스를 작성한다.

 

package convert;

 

import! java.io.IOException;

import! javax.servlet.Filter;

import! javax.servlet.FilterChain;

import! javax.servlet.FilterConfig;

import! javax.servlet.ServletException;

import! javax.servlet.ServletRequest;

import! javax.servlet.ServletResponse;

import! javax.servlet.UnavailableException;

 

public class KorConvert implements Filter {

    private String encoding;

    private FilterConfig fg;

    private boolean ignore = true;

    public void init(FilterConfig filterConfig) throws ServletException {

    fg = filterConfig;

    this.encoding = filterConfig.getInitParameter("encoding");

    String value = filterConfig.getInitParameter("ignore");

    if (value == null)

        this.ignore = true;

 

 

        else if (value.equalsIgnoreCase("true"))

            this.ignore = true;

        else if (value.equalsIgnoreCase("yes"))

            this.ignore = true;

        else

            this.ignore = false;

    }

 

    public void doFilter(ServletRequest request, ServletResponse response,

                                  FilterChain chain)throws IOException, ServletException {

        if (ignore || (request.getCharacterEncoding() == null)) {

            String encoding = selectEncoding(request);

            if (encoding != null)

                request.setCharacterEncoding(encoding);

        }

        chain.doFilter(request, response);

    }

 

    public void destroy() {

        this.encoding = null;

        fg = null;

    }

 

    protected String selectEncoding(ServletRequest request) {

        return encoding;

    }

}

2) web.xml에서 다음과 같이 filter를 추가한다.

             <filter>

                           <filter-name>Set Character Encoding</filter-name>

                           <filter-class>convert.KorConvert</filter-class>

                           <init-param>

                             <param-name>encoding</param-name>

                             <param-value>UTF-8</param-value>

                           </init-param>

             </filter>

 

3) Struts RequestProcessor클래스의 processPreprocess라는 메서드를 이용하여 다른 작업이 이루어지기 전에 미리 설정할 것들을 이 메서드에서 정의할 수 있다. 그러므로 한글 인코딩과 같은 기능을 processPreprocess라는 메서드에 정의하므로 Struts의 모든 부분에서 적용 되도록 한다.

 

package processor;

 

import! javax.servlet.http.*;

 

import! org.apache.struts.action.RequestProcessor;

 

public class ReqProcessor extends RequestProcessor { // 넷빈에서는

                           //org.apache.struts.tiles.TilesRequestProcessor객체를 상속받는다.

       

       protected boolean processPreprocess(

                HttpServletRequest request,

                HttpServletResponse response) {

               

                try {

                        request.setCharacterEncoding("UTF-8");

                        return true;

                } catch (Exception e) {

                        return false;

                }

        }

}

 

4) struts-config.xml문서에서 Controller 매핑을 걸어둔다.

<controller

            contentType="text/html;charset=utf-8"

            locale="true"

            nocache="true"

            processorClass="processor.ReqProcessor"/>