2012-07-12 59 views
1

我正在使用Spring 3.0並使用Spring安全性進行登錄。我已經把login.jsp頁面的Web應用程序文件夾中,我試圖用對本地化的支持如消息:在直接調用JSP時,我可以在Spring中使用消息資源包

<spring:message code="label.title"/> 

遺憾的是,JSP無法找到消息給錯誤:

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.title' for locale 'en_US'

當我在通過控制器的jsp中使用消息代碼,它工作正常。

由於當用戶未登錄時,spring安全性重定向到login.jsp頁面,因此該頁面在沒有控制器的情況下(直接)處理。

任何人都知道如何讓jsp在不通過控制器的情況下查看資源包?

謝謝!

回答

3

您必須強制春天來渲染頁面的login.jsp,這實際上是不被Spring管理。因此,創建一個虛擬控制器:

/** 
* UI Controller that renders the signin-form. 
*/ 
@Controller 
public class SigninController { 

    /** 
    * Render the signin form to the person as HTML in their web browser. 
    * Returns void and relies in request-to-view-name translation to kick-in to 
    * resolve the view template to render. 
    */ 
    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public void login() { 
    } 
} 

,並創建一個名爲「登錄」,這取決於你所使用的ViewResolver(TilesViewResolver,作爲UrlBasedViewResolver ...)的觀點

+0

我這樣做,它的工作。我只是覺得這令人討厭,春天迫使你開發虛擬代碼。爲什麼不能使用jsp標籤做到這一點?看起來很簡單。謝謝您的幫助! – checklist 2012-07-13 14:19:12

+0

如果你沒有使用控制器,你不使用Spring MVC。因此,Spring JSP標記不起作用。 – Clay 2012-10-18 18:06:44

1

使用

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 

,然後在JSP中直接引用的內容

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

這就是我一直在做的事情。但它似乎沒有找到消息。我的猜測是,因爲它直接進入jsp而不是通過控制器,所以不會加載消息。 – checklist 2012-07-13 07:57:25

相關問題