Jsp自定义标签访问struts2+spring+hibernate问题
我想使用Jsp自定义标签来通过struts2+spring+hibernate框架来访问数据库,在struts2+spring+hibernate中通过如下代码访问数据库:
public class TestAction extends ActionSupport{ ...... WebApplicationContextUtils.getRequiredWebApplicationContext().getBean("testDao");}
public class TestTag extends TagSupport{ public int doStartTag() throws JspException{ ...... WebApplicationContextUtils.getRequiredWebApplicationContext().getBean("testDao"); }}
public class XxxTag extends RequestContextAwareTag { //覆盖doStartTagInternal方法 protected int doStartTagInternal() throws Exception { this.getRequestContext().getWebApplicationContext().getBean("beanName"); }}
[解决办法]
在标签中可以通过pageContext得到servletContext
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());XxxService service = (XxxService)ctx.getBean("beanName");