直接调用JSP时,可以在Spring中使用Message Resource Bundle吗? [英] Can I use in Spring the Message Resource Bundle when calling a JSP directly
问题描述
我正在使用Spring 3.0并使用Spring Security进行登录.我已将login.jsp页面放置在webapp文件夹中,并且尝试使用消息进行本地化支持,例如:
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"/>
不幸的是,jsp找不到给出错误的消息:
Unfortunately, the jsp cannot find the message giving error:
javax.servlet.ServletException:javax.servlet.jsp.JspTagException:在代码"label.title"下找不到语言环境"en_US"的消息
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.title' for locale 'en_US'
当我在通过控制器的jsp中使用消息代码时,它工作正常.
When I use the message code in a jsp that goes through a controller, it works fine.
由于当用户未登录时spring安全性会重定向到login.jsp页面,因此无需控制器即可直接处理该页面.
Since the spring security redirects to the login.jsp page when user is not logged in, the page is processed without a controller (directly).
任何人都知道如何使jsp在不通过控制器时也能看到资源束吗?
Anyone knows how to make the jsp see the resource bundle also when not going through a controller?
谢谢!
推荐答案
您必须强制Spring渲染页面login.jsp,该页面实际上不是由Spring管理的.因此,创建一个虚拟控制器:
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() {
}
}
并根据您使用的ViewResolver(TilesViewResolver,UrlBasedViewResolver ...)创建名为登录"的视图
and create the view named "login" depending of your used ViewResolver (TilesViewResolver, UrlBasedViewResolver...)
这篇关于直接调用JSP时,可以在Spring中使用Message Resource Bundle吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!