\#springMessage 오류 분석

2448 단어 springvelocity
...에 있다 프로젝트 에서\#springMessage 를 사용 하여 오류 코드 를 오류 메시지 로 해석 하 였 으 나 특정한 장면 에서 발생 하 였 습 니 다.
      $springMacroRequestContext.getMessage($code)
 
#springMessage      org/spring from work/web/servlet/view/velocity/spring.vm 파일 에서 매크로 를 정의 합 니 다.그 내용 은:
 
#**
 * springMessage
 *
 * Macro to translate a message code into a message.
 *#
#macro( springMessage $code )$springMacroRequestContext.getMessage($code)#end

 
 springMacroRequestContext 또 뭐 죠?
분석 봄 의 코드 를 발견 할 수 있 습 니 다:
이것 은 AbastractTemplate View 에서 북쪽 으로 정 의 된 값 입 니 다.
public abstract class AbstractTemplateView extends AbstractUrlBasedView {

    /**
     * Variable name of the RequestContext instance in the template model,
     * available to Spring's macros: e.g. for creating BindStatus objects.
     */
    public static final String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE = "springMacroRequestContext";

 
if (this.exposeSpringMacroHelpers) {
			if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) {
				throw new ServletException(
						"Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE +
						"' because of an existing model object of the same name");
			}
			// Expose RequestContext instance for Spring macros.
			model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE,
					new RequestContext(request, response, getServletContext(), model));
		}

 이로써 우 리 는 발견 할 수 있다 이 대상 은 org.springframework.web.servlet.support.RequestContext 입 니 다. 대상
 최종 호출 된 방법 은:
	/**
	 * Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
	 * @param code code of the message
	 * @return the message
	 * @throws org.springframework.context.NoSuchMessageException if not found
	 */
	public String getMessage(String code) throws NoSuchMessageException {
		return getMessage(code, null, isDefaultHtmlEscape());
	}

 
   그러나 최종 적 으로 페이지 에 표 시 된 것 은:
         $springMacroRequestContext.getMessage($code)
이 출력 을 통 해 이 매크로 가 정의 되 지 않 았 기 때 문 임 을 알 수 있 습 니 다. 왜 정의 되 지 않 았 는 지 자세히 생각해 보 세 요.이상 할 때 들 어 오 는 code 입 니 다. String 이 아니에요. 유형

좋은 웹페이지 즐겨찾기