[英]Spring-MVC with JSP. How to access a singleton-scoped object from a JSP page?
Got a custom formatter bean in a singleton scope. 在单例作用域中获得了一个自定义格式化程序bean。 And want to use it in a JSP page.
并希望在JSP页面中使用它。
First way: 第一种方式:
==Code== == ==代码
@RequestMapping(method = RequestMethod.GET)
public ModelAndView get() {
ModelAndView result = new ModelAndView( "view" );
result.addObject( "format", format );
return result;
}
The simpliest way, but when this bean is needed in almost every page, it hurts. 最简单的方法,但是当几乎每个页面都需要此bean时,它很痛苦。
Second way: 第二种方式:
==Code== == ==代码
<div>
${sessionScope.format.doSmth()}
</div>
Bad way too. 也很糟糕。 First, need to inject the bean - the bean is created only when it's injected by Spring Context.
首先,需要注入bean-仅当Spring Context注入bean时才创建。 Otherwise the bean won't be created and put into the session.
否则,将不会创建Bean并将其放入会话中。 Second, my IDE (IDEA) doesn't provide syntax highlighting in that case.
其次,在这种情况下,我的IDE(IDEA)不提供语法突出显示功能。
So the question is: 所以问题是:
Is there any way to use a singleton-scoped bean in a JSP page without putting it in context every time and with IDE support? 有什么方法可以在JSP页面中使用单例作用域的bean,而不必每次都将其置于上下文中并获得IDE支持吗?
I would think about 2 ways of making a (singleton scoped) bean disponible in JSP view. 我会考虑两种在JSP视图中使(单作用域)bean不负责任的方法。
init-method
. init-method
中的bean。 It is then immediately accessible through EL to all JSP But I really have no idea how to get IDE support :-( (don't use IDEA) 但是我真的不知道如何获得IDE支持:-((不要使用IDEA)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.