Java Struts - Struts 提供的 .tld 的 JSP 页面中的 Taglib 指令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2392993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Struts - Taglib directive in a JSP page for .tld provided by Struts
提问by Yatendra Goel
I am developing a Struts
based application. I am new to Struts. I want to use html
tags , specified in a taglib directory provided by Struts
, in a JSP
page.
我正在开发一个Struts
基于应用程序。我是 Struts 的新手。我想使用html
的标签,在所提供的标签库目录规定Struts
,在一个JSP
页面。
But don't know how to use it. I know how to use taglib directive but I came to know from sources that the .tld file has been embedded in a .jar file after version 1.2.8. I am using Struts version 2.1.8.
但是不知道怎么用。我知道如何使用 taglib 指令,但我从消息来源得知 .tld 文件已在 1.2.8 版之后嵌入到 .jar 文件中。我正在使用 Struts 版本 2.1.8。
I don't know in which .jar file the struts-html.tld
file is located.
我不知道该struts-html.tld
文件位于哪个 .jar 文件中。
采纳答案by Buhake Sindi
I'm using Struts 1.3.10 for this illustration:
我在这个插图中使用 Struts 1.3.10:
- Download the latest struts library here (http://struts.apache.org/download.cgi#struts1310). Remember, the Full Distributionis that what you have to download as it contains a war file with the Struts TLD's.
- On your web application, copy all the lib in the archive file you downloaded to your
/WEB-INF/lib
folder. - For JSTL libraries (which works well with struts) go here (http://java.sun.com/products/jsp/jstl/)
- Once you have your Struts TLD's and JSTL Tld's, put them under the
/WEB-INF/tld/
folder (it mustbe situated in the /WEB-INF/ folder). On web.xml add the following stuff (under the
<web-app>
element)<jsp-config> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/sslext.tld</taglib-uri> <taglib-location>/WEB-INF/tld/sslext.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-layout.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-layout.tld</taglib-location> </taglib> <!-- Sun's JSTL --> <taglib> <taglib-uri>http://java.sun.com/jstl/fn</taglib-uri> <taglib-location>/WEB-INF/tld/fn.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/tld/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/c-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri> <taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/c-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/tld/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/sql-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri> <taglib-location>/WEB-INF/tld/sql-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/sql-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x</taglib-uri> <taglib-location>/WEB-INF/tld/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/x-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/tld/x-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/x-1_0-rt.tld</taglib-location> </taglib> </jsp-config>
- 在此处下载最新的 struts 库 ( http://struts.apache.org/download.cgi#struts1310)。请记住,完整分发版是您必须下载的内容,因为它包含带有 Struts TLD 的 war 文件。
- 在您的 Web 应用程序上,将您下载的存档文件中的所有库复制到您的
/WEB-INF/lib
文件夹中。 - 对于 JSTL 库(与 struts 配合良好),请访问此处(http://java.sun.com/products/jsp/jstl/)
- 获得 Struts TLD 和 JSTL Tld 后,将它们放在
/WEB-INF/tld/
文件夹下(它必须位于 /WEB-INF/ 文件夹中)。 在 web.xml 上添加以下内容(在
<web-app>
元素下)<jsp-config> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/sslext.tld</taglib-uri> <taglib-location>/WEB-INF/tld/sslext.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-layout.tld</taglib-uri> <taglib-location>/WEB-INF/tld/struts-layout.tld</taglib-location> </taglib> <!-- Sun's JSTL --> <taglib> <taglib-uri>http://java.sun.com/jstl/fn</taglib-uri> <taglib-location>/WEB-INF/tld/fn.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/tld/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/c-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri> <taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/c-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/tld/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/sql-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri> <taglib-location>/WEB-INF/tld/sql-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/sql-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x</taglib-uri> <taglib-location>/WEB-INF/tld/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-1-0</taglib-uri> <taglib-location>/WEB-INF/tld/x-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/tld/x-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-1-0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/x-1_0-rt.tld</taglib-location> </taglib> </jsp-config>
This tells that once you call your TLD from the JSP, your webapp will look for the matching <taglib-uri>
then look for it's location on <taglib-location>
and find relevant class to call.
这表明一旦您从 JSP 调用 TLD,您的 web 应用程序将查找匹配项,<taglib-uri>
然后查找它的位置<taglib-location>
并找到要调用的相关类。
On your JSP, now you can do this:
在您的 JSP 上,现在您可以执行以下操作:
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-layout.tld" prefix="layout"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jstl/fn" prefix="fn" %>
Hope this helps.
希望这可以帮助。
回答by Daniel F. Thornton
BalusC is correct: you just need the JAR files in /WEB-INF/lib
and a proper taglib
in each JSP. One thing that tripped me up initially was the sensitive JSP syntax; spaces between some elements are important. Per the Stripes documentation, each taglib
for the Stripes TLD should look like:
BalusC 是正确的:您只需要 JAR 文件/WEB-INF/lib
和taglib
每个 JSP 中的适当文件。最初让我失望的一件事是敏感的 JSP 语法;某些元素之间的空格很重要。根据 Stripes文档,每个taglib
Stripes TLD 应如下所示:
<%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %>
回答by zb226
Even if this question has an (to me, incomprehensibly) accepted answer, I feel obliged to have a go at it myself. The real answer is contained in BalusC's comment and suggesting stray .tld
files laying around in WEB-INF
is really bad advice. My intention is expand on this using the exact version of Struts2 the OP was asking about (v2.1.8), which I downloaded from Apache's historical archive.
即使这个问题有一个(对我来说,难以理解的)答案,我也觉得有必要自己试一试。真正的答案被包含在BalusC的评论和建议流浪.tld
文件在周围铺设WEB-INF
是非常糟糕的建议。我的意图是使用 OP 询问的确切版本的 Struts2 (v2.1.8) 对此进行扩展,该版本是我从Apache 的历史档案下载的。
I don't know in which .jar file the struts-html.tld file is located.
我不知道 struts-html.tld 文件位于哪个 .jar 文件中。
There is no struts-html.tld
in Struts2 - Instead there are the following:
struts-html.tld
Struts2 中没有- 而是有以下内容:
struts-tags.tld
, which resides in theMETA-INF
directory ofstruts2-core-2.1.8.jar
and contains all standard Struts tags, like the ones you'd expect to find instruts-html.tld
in Struts1.tiles-jsp.tld
, which resides in theMETA-INF
directory oftiles-jsp-2.0.6.jar
and corresponds to what wasstruts-tiles.tld
in Struts1.- some more TLDs, e.g. for SiteMesh which are not directly related to the question.
struts-tags.tld
,它驻留在 的META-INF
目录中struts2-core-2.1.8.jar
并包含所有标准 Struts 标签,就像您希望struts-html.tld
在 Struts1 中找到的那些标签一样。tiles-jsp.tld
,它位于Struts1的META-INF
目录中tiles-jsp-2.0.6.jar
并对应struts-tiles.tld
于 Struts1 中的内容。- 更多的 TLD,例如 SiteMesh,与问题没有直接关系。
I want to use html tags, specified in a taglib directory provided by Struts, in a JSP page. But don't know how to use it. I know how to use taglib directive but I came to know from sources that the .tld file has been embedded in a .jar file after version 1.2.8.
我想在 JSP 页面中使用在 Struts 提供的 taglib 目录中指定的 html 标签。但是不知道怎么用。我知道如何使用 taglib 指令,但我从消息来源得知 .tld 文件已在 1.2.8 版之后嵌入到 .jar 文件中。
That is correct. The way it generally works is the following:
那是正确的。它通常的工作方式如下:
- When the servlet container starts up, it looks through the
WEB-INF/lib
directory and loads any.jar
files it finds there - This is where you need to place the Struts2 library. - Inside these
.jar
files, any TLDs are expected to reside in theMETA-INF
directory. Obviously and as mentioned above, this already is the case forstruts2-core-2.1.8.jar
, so there's nothing that needs to be done. - When the servlet container loads the TLD, it looks for the
<uri>
element inside the root element<taglib>
and stores a mapping between that TLD and its URI. Correspondingly, this URI is used in your.jsp
files to reference the TLD.
- 当 servlet 容器启动时,它会查看
WEB-INF/lib
目录并加载.jar
它在那里找到的所有文件——这是您需要放置 Struts2 库的地方。 - 在这些
.jar
文件中,任何 TLD 都应该驻留在META-INF
目录中。显然,正如上面提到的,这已经是 的情况struts2-core-2.1.8.jar
,所以没有什么需要做的。 - 当 servlet 容器加载 TLD 时,它会
<uri>
在根元素中查找元素<taglib>
并存储该 TLD 与其 URI 之间的映射。相应地,此 URI 在您的.jsp
文件中用于引用 TLD。
In the case of struts2-core-2.1.8.jar
, the URI is /struts-tags
and thus you need to reference it in a .jsp
file like this (of course you can change the prefix
attribute to your liking)...
在 的情况下struts2-core-2.1.8.jar
,URI 是/struts-tags
,因此您需要在这样的.jsp
文件中引用它(当然,您可以prefix
根据自己的喜好更改属性)...
<%@ taglib uri="/struts-tags" prefix="s" %>
...and subsequently put it to use, like e.g. this:
...然后将其投入使用,例如:
<s:form action="HelloWorld">
(...)
</s:form>