[英]Struts - Taglib directive in a JSP page for .tld provided by Struts
I am developing a Struts
based application. 我正在开发一个基于
Struts
的应用程序。 I am new to Struts. 我是Struts的新手。 I want to use
html
tags , specified in a taglib directory provided by Struts
, in a JSP
page. 我想在
JSP
页面中使用由Struts
提供的taglib目录中指定的html
标记。
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.
我知道如何使用taglib指令,但我从源代码中了解到.tld文件已经嵌入1.2.8版之后的.jar文件中。 I am using Struts version 2.1.8.
我使用的是Struts 2.1.8版。
I don't know in which .jar file the struts-html.tld
file is located. 我不知道
struts-html.tld
文件位于哪个.jar文件中。
I'm using Struts 1.3.10 for this illustration: 我在这个例子中使用Struts 1.3.10:
/WEB-INF/lib
folder. /WEB-INF/lib
文件夹中。 /WEB-INF/tld/
folder (it must be situated in the /WEB-INF/ folder). /WEB-INF/tld/
文件夹下(它必须位于/ WEB-INF /文件夹中)。 On web.xml add the following stuff (under the <web-app>
element) 在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,您的webapp将查找匹配的
<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. 希望这可以帮助。
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 . 真正的答案包含在BalusC的评论中,并暗示在
WEB-INF
存在的流浪.tld
文件是非常糟糕的建议 。 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 . 我的意图是使用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: Struts2中没有
struts-html.tld
- 而是有以下内容:
struts-tags.tld
, which resides in the META-INF
directory of struts2-core-2.1.8.jar
and contains all standard Struts tags , like the ones you'd expect to find in struts-html.tld
in Struts1. struts-tags.tld
,它位于struts2-core-2.1.8.jar
的META-INF
目录中,包含所有标准的Struts标记 ,就像你期望在Struts1中的struts-html.tld
中找到的那样。 tiles-jsp.tld
, which resides in the META-INF
directory of tiles-jsp-2.0.6.jar
and corresponds to what was struts-tiles.tld
in Struts1. tiles-jsp.tld
,它位于tiles-jsp-2.0.6.jar
的META-INF
目录中,对应于Struts1中的struts-tiles.tld
。 I want to use html tags, specified in a taglib directory provided by Struts, in a JSP page.
我想在JSP页面中使用由Struts提供的taglib目录中指定的html标记。 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.
我知道如何使用taglib指令,但我从源代码中了解到.tld文件已经嵌入1.2.8版之后的.jar文件中。
That is correct. 那是对的。 The way it generally works is the following:
它通常的工作方式如下:
WEB-INF/lib
directory and loads any .jar
files it finds there - This is where you need to place the Struts2 library. WEB-INF/lib
目录并加载它在那里找到的任何.jar
文件 - 这是你需要放置Struts2库的地方。 .jar
files, any TLDs are expected to reside in the META-INF
directory. .jar
文件中,任何TLD都应该驻留在META-INF
目录中。 Obviously and as mentioned above, this already is the case for struts2-core-2.1.8.jar
, so there's nothing that needs to be done. struts2-core-2.1.8.jar
已经是这种情况,所以没有什么需要做的。 <uri>
element inside the root element <taglib>
and stores a mapping between that TLD and its URI. <taglib>
查找<uri>
元素,并存储该TLD与其URI之间的映射。 Correspondingly, this URI is used in your .jsp
files to reference the TLD. .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 eg this: ...然后将其投入使用,例如:
<s:form action="HelloWorld">
(...)
</s:form>
BalusC is correct: you just need the JAR files in /WEB-INF/lib
and a proper taglib
in each JSP. BalusC是正确的:你只需要
/WEB-INF/lib
的JAR文件和每个JSP中的正确taglib
。 One thing that tripped me up initially was the sensitive JSP syntax; 最初让我感到震惊的一件事是敏感的JSP语法; spaces between some elements are important.
一些元素之间的空间很重要。 Per the Stripes documentation , each
taglib
for the Stripes TLD should look like: 根据Stripes 文档 ,Stripes TLD的每个
taglib
应如下所示:
<%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.