2009-11-08 24 views
6

Tôi làm cách nào để truy cập thư từ gói tài nguyên trong Spring MVC bên trong một lớp mở rộng từ AbstractController?Resource Bundle Spring

Tôi đã thử getMessageSourceAccessor().getMessage("a.message"); nhưng nó nó throws Exception này:

org.springframework.web.util.NestedServletException: Yêu cầu xử lý thất bại; lồng nhau ngoại lệ là org.springframework.context.NoSuchMessageException: Không tìm thấy thư nào theo mã 'a.message' cho ngôn ngữ 'en'.

Khi tôi làm như vậy trong một jsp nó hoạt động

<spring:message code="a.message"/> 
+0

Nếu có "a.message" thay vì "error.emptyUsername" trong thông báo lỗi của bạn? – martsraits

+0

Xin lỗi tôi đã chỉnh sửa lại câu hỏi. – Enrique

Trả lời

9

Bạn có thể sử dụng một trong những phương pháp getMessage() biến thể trên lớp org.springframework.web.servlet.support.RequestContext. Một cái gì đó như thế này hoạt động:

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    RequestContext ctx = new RequestContext(request); 
    String messageFromBundle = ctx.getMessage("a.message"); 
}