访问Spring MVC DI bean从jsp

编程入门 行业动态 更新时间:2024-10-27 08:34:47
本文介绍了访问Spring MVC DI bean从jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在某些MVC框架中,如果您希望执行一些代码并渲染一些局部视图,您可以从视图调用控制器操作。我不知道什么是在Spring MVC中的正确方法

我想有一组JSP模板。其中一些将是页面布局,其中一些将是小组件,如分页器,登录框,菜单,标签云等等等。每个组件都需要一些bean或控制器动作来设置一些数据到ViewAndModel,以便视图可以使用它。

问题是我不想在每次调用中设置所有这些对象。我的寄存器控制器只关心注册处理。所以现在我怎么做呢?如何从视图调用DI bean或控制器以准备部分视图?还是应该创建一些映射?

$ p

解决方案

Spring-MVC可以将应用程序上下文的bean暴露给视图层,如果这是你想要做的。

例如,可以指示 InternalResourceViewResolver 上下文,或只是指定的。请查看 exposeContextBeansAsAttributes 和 exposedContextBeanNames 属性。

例如,将bean beanA 和 beanB 暴露给您的JSP。您将在上下文中声明视图解析器:

< bean class =org.springframework.web.servlet.view .InternalResourceViewResolver> < property name =exposedContextBeanNames> < list> < value> beanA< / value> < value> beanB< / value> < / list> < / property> < / bean>

或者,为了公开每个bean:

< bean class =org.springframework.web.servlet.view.InternalResourceViewResolver> < property name =exposeContextBeansAsAttributesvalue =true/> < / bean>

这是否是一个好主意是另一个问题,但Spring会给你的选择。 / p>

In some MVC frameworks you can call controller action from the view if you wish to execute some code and render some partial view. I'm not sure what is the correct way to do it in Spring MVC

I want to have set of JSP templates. Some of them will be page layouts some of them will be small components like paginator, login box, menu, tag cloud etc etc etc. Each of these component need some beans or controller action to set some data into ViewAndModel so that view could use it.

The problem is I don't want to set all these objects in each call. My register controller cares only about registration processing. So now how do i do it right? How do I call DI beans or controllers from the view to prepare partial views? Or should I create some mappings? Or am I approaching the problem from totally wrong angle?

解决方案

Spring-MVC can expose the application context's beans to the view layer, if that is what you wish to do.

For example, the InternalResourceViewResolver can be instructed to expose every bean in the context, or just specified ones. Take a look at the exposeContextBeansAsAttributes and exposedContextBeanNames properties.

For example, say you wanted to expose the beans beanA and beanB to your JSPs. You would declare the view resolver in your context thus:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="exposedContextBeanNames"> <list> <value>beanA</value> <value>beanB</value> </list> </property> </bean>

Alternatively, to just expose every bean:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="exposeContextBeansAsAttributes" value="true"/> </bean>

Whether or not this is a good idea is another question, but Spring does give you the option.

更多推荐

访问Spring MVC DI bean从jsp

本文发布于:2023-11-16 21:50:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MVC   Spring   DI   jsp   bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!