How to inject spring beans into a jsp 2.0 SimpleTag?

后端 未结 3 1011
天命终不由人
天命终不由人 2020-12-03 11:36

Currently my jsp 2.0 tags that need spring beans use this code:

ac = WebApplicationContextUtils.getWebApplicationContext( servletContext);
ac.getBeansOfType(         


        
3条回答
  •  盖世英雄少女心
    2020-12-03 11:52

    Another way to achieve this is use a static property to hold the dependency. Just like below:

    public class InjectedTag extends SimpleTagSupport {
    //In order to hold the injected service, we have to declare it as static
        private static AService _service;   
    /***/   
    @Override   
    public void doTag() throws IOException {    
              getJspContext().getOut().
              write("Service injected: " + _service + "
    "); } public void setService(AService service) { _service = service; } }

    In you applicationcontext, you have to register both so that the JSP tag can get one chance to be initiated by Spring. We we Go with the magic...

    
      
    
    
      
    
    

    Cool huh, now aService is visible in our JSP tag :)

提交回复
热议问题