Java 启用JSP自定义标记库以使用spring服务bean
我正在使用SpringMVC3.2.4(也是SpringCore3.2.4)开发一个web应用程序,后端使用jpa和hibernate。目前正在使用TomcatV6.0进行测试 我创建了一个JSP自定义标记库(使用JSP api 2.1.1和servlet api 2.5),这是一个自定义查找下拉列表,我会给它一个查找类型,它会从DB中获取该类型下的项,以作为列表中的项呈现 自定义taglib类基本上类似于以下内容:Java 启用JSP自定义标记库以使用spring服务bean,java,spring-mvc,jsp-tags,autowired,taglib,Java,Spring Mvc,Jsp Tags,Autowired,Taglib,我正在使用SpringMVC3.2.4(也是SpringCore3.2.4)开发一个web应用程序,后端使用jpa和hibernate。目前正在使用TomcatV6.0进行测试 我创建了一个JSP自定义标记库(使用JSP api 2.1.1和servlet api 2.5),这是一个自定义查找下拉列表,我会给它一个查找类型,它会从DB中获取该类型下的项,以作为列表中的项呈现 自定义taglib类基本上类似于以下内容: public class LookupsTag extends SimpleT
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
}
public类LookupsTag扩展了SimpleTagSupport{
@自动连线
私有静态LookupService LookupService;
私有字符串类型;
public void doTag()抛出JSPEException,IOException{
List items=lookupService.findByType(getType());
StringBuffer buff=新的StringBuffer();
buff.追加(“”);
//…正在添加项目。。。
buff.追加(“”);
getJspContext().getOut().write(buff.toString());
}
//接球手和接球手
}
我已经相应地创建了tld文件
尝试使用此自定义标记查看页面后,会引发NullPointerException,因为在doTag()方法中,lookupService实例为null
Spring似乎不支持自定义jsp标记。有没有办法解决这个问题,使服务实例自动连接?还是我缺少了一些spring配置
谢谢,谢谢Alan Hay的评论,自定义jsp标记库将不受支持。
Spring已经通过他们的taglib提供了我所需要的东西您的
LookupsTag
是否在Spring配置中定义为bean?不,只有j2ee项目中任何普通jsp taglib所需的配置。LookupsTag不是,也永远不会是Spring管理的bean。不管怎样,你在这里干什么?SpringMVC已经有了标签,可以帮你做到这一点:谢谢Alan!我对SpringMVC的产品还没有深入的技术知识。看看文档,这似乎足够了!您应该将此添加为解决方案,而不是注释