Jsp自定义标签访问struts2 spring hibernate问题

时间:2008-07-18 09:11:15   来源:论坛整理  作者:  编辑:chinaitzhe
我想使用Jsp自定义标签来通过struts2 spring hibernate框架来访问数据库,在struts2 spring hibernate中通过如下代码访问数据库:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



public class TestAction extends ActionSupport{

    ......

    WebApplicationContextUtils.getRequiredWebApplicationContext().getBean("testDao");

}


上面的代码在struts2 spring hibernate中运行很正常。

现在我想通过Jsp自定义标签来访问struts2 spring hibernate框架,从而获得数据,代码如下:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



public class TestTag extends TagSupport{

   public int doStartTag() throws JspException{

       ......

       WebApplicationContextUtils.getRequiredWebApplicationContext().getBean("testDao");

   }

}



当我运行这个JSP自定义标签时,系统出错:
Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException

WebApplicationContextUtils.getRequiredWebApplicationContext().getBean("testDao");//引起空指针异常

有什么办法能使用JSP自定义标签来访问struts2 spring hibernate框架来获得数据?
网友回复:此回复为自动发出,仅用于显示而已,并无任何其他非凡作用
楼主【zhangsc】截止到2008-07-16 11:29:20的历史汇总数据(不包括此帖):
发帖的总数量:41 发帖的总分数:1840 每贴平均分数:44
回帖的总数量:21 得分贴总数量:1 回帖的得分率:4%
结贴的总数量:37 结贴的总分数:1760
无满足结贴数:4 无满足结贴分:190
未结的帖子数:4 未结的总分数:80
结贴的百分比:90.24 % 结分的百分比:95.65 %
无满足结贴率:10.81 % 无满足结分率:10.80 %
值得尊敬
网友回复:可以从org.springframework.web.servlet.tags.RequestContextAwareTag继续,
getRequestContext().getWebApplicationContext()可以得到spring的context
网友回复:要有一块全局的context,通过它来得,就不会有问题,
在启动的时候把它写进上下文
网友回复:>>可以从org.springframework.web.servlet.tags.RequestContextAwareTag继续

是指标签类可以这样写吗?
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



public class TestTag extends org.springframework.web.servlet.tags.RequestContextAwareTag{

       ......

       WebApplicationContextUtils.getRequiredWebApplicationContext().getBean("testDao");

   }

}




能说得具体些吗?谢谢!
网友回复:给你个例子
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





public class XxxTag extends RequestContextAwareTag {

  //覆盖doStartTagInternal方法

  protected int doStartTagInternal() throws Exception {

      this.getRequestContext().getWebApplicationContext().getBean("beanName");

      

  }

}




网友回复:在标签中可以通过pageContext得到servletContext
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());

XxxService service = (XxxService)ctx.getBean("beanName");

关键字:Jsp,自定义,标签,访问,struts,spring,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面