启用JSP Custom taglib以使用spring服务bean [英] Enable JSP Custom taglib to use spring service beans

查看:203
本文介绍了启用JSP Custom taglib以使用spring服务bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Spring MVC 3.2.4(Spring Core 3.2.4)和jpa& hibernate用于后端。目前正在使用Tomcat v6.0进行测试。

I'm developing a web application using Spring MVC 3.2.4 (Spring Core 3.2.4 too) with jpa & hibernate for the back-end. And currently using Tomcat v6.0 for testing.

我有一个案例,我创建了一个JSP自定义标记库lib(使用jsp-api 2.1.1& servlet-api 2.5),这是一个自定义查找下拉列表列表,我会给它查找类型,它将从DB获取此类型下的项目呈现为列表中的项目。

I had a case when i created a JSP custom tag lib (using jsp-api 2.1.1 & servlet-api 2.5), which is a custom lookups drop down list, i would give it the lookup type, and it would get from the DB the items under this type to render as items in the list.

自定义taglib类基本上看起来像这样:

The custom taglib class basically looks like something similar to this:

public class LookupsTag extends SimpleTagSupport {
  @Autowired
  private static LookupService lookupService;
  private String type;

    public void doTag() throws JspException, IOException {
        List<Lookup> items = lookupService.findByType(getType());

        StringBuffer buff = new StringBuffer();
        buff.append("<select>");
        //...adding items...
        buff.append("</select>");
        getJspContext().getOut().write(buff.toString());
    }
  //getters and setters
}

和我已经相应地创建了tld文件。

And i have created the tld file accordingly and all.

一旦我尝试使用此自定义标记查看页面,就会抛出 NullPointerException ,因为在 doTag()方法, lookupService 实例是 null

Once i attempt to view a page using this custom tag, a NullPointerException is thrown because inside the doTag() method, the lookupService instance is null.

Spring似乎不支持自定义jsp标记。有没有办法解决这个问题,使服务实例自动自动装配?或者我错过了一些弹簧配置吗?

Spring doesn't seem to support custom jsp tags. Is there a way to work around this to make the service instance autowired automatically? Or am i missing some spring configurations to do?

谢谢,

推荐答案

<感谢Alan Hay的评论,不支持自定义jsp taglib。
Spring已经通过他们的taglib提供了我需要的东西 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/spring-form.tld.html#spring-form .tld.select

这篇关于启用JSP Custom taglib以使用spring服务bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆