SpringMvc 차단기 데이터 반환 문제 해결

본인은 다음과 같은 문제에 부딪혔습니다.
springmvc 차단기를 정확하게 설정한 후 차단기에서 데이터를 직접 되돌려야 할 경우, 되돌려주는 값은 난코드입니다.본인 프로젝트 번호는 UTF-8
 
1. 차단기 구현
package cn.test.interceptor;

import net.sf.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanghaiwei
 * @version 1.0
 * @date 16/1/13   10:00
 */
public class TestInterceptor extends HandlerInterceptorAdapter {
	/**
	 * controller                  , postHandle    controller          。
	 */
	@Override
	public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		super.afterConcurrentHandlingStarted(request, response, handler);
	}

	/**
	 * preHandle            ,    ,     Controller        ,SpringMVC  Interceptor       ,      
	 *   Interceptor,  SpringMVC                  ,     Interceptor  preHandle     
	 * Controller        。SpringMVC   Interceptor             ,        preHandle  
	 *    false, preHandle     false           。
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("error", "        ^_^");
	        response.getWriter().write(JSONObject.fromObject(result).toString());
		response.getWriter().close();
		return false;//    
	}

	/**
	 *            Interceptor preHandle      true       。postHandle          ,                
	 *  ,    Controller         ,     DispatcherServlet           ,              ModelAndView   
	 *  。                     ,        Interceptor            ,  Struts2              ,
	 *   Struts2   intercept         ActionInvocation invoke  ,Struts2   ActionInvocation invoke         Interceptor
	 *      action,    Interceptor            invoke  ,  Interceptor            invoke    。
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 *             Interceptor preHandle       true     。             ,   DispatcherServlet       ,
	 *                  ,              Interceptor preHandle       true     。
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}

 
 
2. 차단기 설정
<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/> 
        <bean class="cn.test.interceptor.TestInterceptor"></bean>
        </mvc:interceptor>
</mvc:interceptors>

 
 
3. 난자 문제 해결
응답 헤더에 인코딩 추가하기
	/**
	 * preHandle            ,    ,     Controller        ,SpringMVC  Interceptor       ,      
	 *   Interceptor,  SpringMVC                  ,     Interceptor  preHandle     
	 * Controller        。SpringMVC   Interceptor             ,        preHandle  
	 *    false, preHandle     false           。
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("error", "            ^_^");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Type", "text/html;charset=UTF-8");//         
		response.getWriter().write(JSONObject.fromObject(result).toString());
		response.getWriter().close();
		return false;//    
	}

 
 
 
참조 문서 주소:
SpringMVC에서 Interceptor 블로커 순서 사용 등:http://blog.csdn.net/ufo2910628/article/details/24423413
Spring MVC에서 HandlerInterceptorAdapter 사용:http://blog.csdn.net/liuwenbo0920/article/details/7283757
SpringMVC는 시스템 로그를 기록하는 몇 가지 방법을 제공합니다.http://www.cnblogs.com/wang3680/p/4863661.html

좋은 웹페이지 즐겨찾기