在公共标头中声明时,无法识别JSTL标记库 [英] JSTL taglibs not recognized when declared in common header

查看:217
本文介绍了在公共标头中声明时,无法识别JSTL标记库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段时间的想法将我的所有taglib声明(uri等)放在一个公共头文件中,所以我不必手动将它们写入我的所有JSP中。最初,事情看起来很好,虽然我不像简单的EL语法那样使用实际的taglib。但是,我在所有jsp文件中都遇到了问题,除了明确声明了taglibs的文件。所有其他jsp(包括头文件)都将< c:something ... /> 标记视为HTML,并且不对其进行评估。我做了一些谷歌搜索,发现这篇关于O'Reilly的帖子建议我正在努力做的事情,但我显然做错了什么。更重要的是,头文件中的其他内容(常见页眉,页面标题等)都显示正常。头文件和包含的示例如下。

I had the idea a while back to put all of my taglib declarations (uri's, etc) in a common header file so I don't have to manually write them into all of my JSPs. Initially, things seemed fine, although I don't use the actual taglibs as much as just the simple EL syntax. However, I'm having trouble in all jsp files except for the one that explicitly has the taglibs declared. All of the other jsp's (that include the header file) treat the <c:something.../> tag as if it's HTML and don't evaluate it. I did some googling and found this post on O'Reilly suggesting that what I'm trying to do can be done, but I'm clearly doing something wrong. What's more is that the other stuff in the header file (common page headers, page titles, etc.) all show up fine. The header file and a sample of the inclusion are below.

头文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

包含声明:

<jsp:include page="/WEB-INF/jsp/include/header.jsp">
    <jsp:param name="title" value="Home" />
</jsp:include>


推荐答案

这是预期的行为。

当您使用< jsp:include> 时,它在单独的请求中执行目标,然后在包含JSP中包含输出。它不包含所包含目标的,它包含输出。生成目标输出的方法将丢失。

When you use <jsp:include>, it executed the target in a separate request, and then includes the output in the including JSP. It doesn't include the source of the included target, it includes the output. The means by which that target output is generated is lost.

要执行您要执行的操作,您需要使用<%包含%> 指令:

To do what you're trying to do, you need to use <% include %> directives:

<%@ include file="/WEB-INF/jsp/include/header.jsp" %>

这将倾斜 header.jsp 进入你的页面。当然,通过这样做,您不能再向其传递参数,因此您需要将其设置为页面上下文属性(例如,使用< c:set> ...但当然你不能使用< c:set> ,直到你完成了包括......)。

This will incline the literal text of header.jsp into your page. Of course, by doing that, you can no longer pass parameters to it, so you'd need to set that as a page context attribute (e.g. using <c:set>... but of course you can't use <c:set> until you've done your include...).

基本上,这不值得麻烦。 Taglib声明是令人讨厌的样板,但很难摆脱。

Essentially, it's not really worth the hassle. Taglib declarations are annoying boilerplate, but hard to get rid of.

这篇关于在公共标头中声明时,无法识别JSTL标记库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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