Can I use in Spring the Message Resource Bundle when calling a JSP directly

空扰寡人 提交于 2019-12-11 09:25:09

问题


I am using Spring 3.0 and working with the spring security for login. I have placed the login.jsp page in the webapp folder and I am trying to use messages for localization support e.g.:

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

Unfortunately, the jsp cannot find the message giving error:

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

When I use the message code in a jsp that goes through a controller, it works fine.

Since the spring security redirects to the login.jsp page when user is not logged in, the page is processed without a controller (directly).

Anyone knows how to make the jsp see the resource bundle also when not going through a controller?

Thanks!


回答1:


You have to force Spring to render the page login.jsp, which is actually not managed by Spring. So, create a dummy controller:

/**
 * 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() {
    }
} 

and create the view named "login" depending of your used ViewResolver (TilesViewResolver, UrlBasedViewResolver...)




回答2:


Use

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

and then refer the content directly in jsp as

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


来源:https://stackoverflow.com/questions/11448237/can-i-use-in-spring-the-message-resource-bundle-when-calling-a-jsp-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!