Accessing Spring MVC DI beans from jsp

前端 未结 5 648
逝去的感伤
逝去的感伤 2020-12-04 18:43

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 d

5条回答
  •  时光取名叫无心
    2020-12-04 19:23

    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:

    
       
          
             beanA
             beanB
          
       
    
    

    Alternatively, to just expose every bean:

    
       
    
    

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

提交回复
热议问题