简体   繁体   English

Struts - Struts提供的.tld JSP页面中的Taglib指令

[英]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:

  1. Download the latest struts library here ( http://struts.apache.org/download.cgi#struts1310 ). 在这里下载最新的struts库( http://struts.apache.org/download.cgi#struts1310 )。 Remember, the Full Distribution is that what you have to download as it contains a war file with the Struts TLD's. 请记住, Full Distribution是您必须下载的内容,因为它包含带有Struts TLD的war文件。
  2. On your web application, copy all the lib in the archive file you downloaded to your /WEB-INF/lib folder. 在Web应用程序中,将下载的存档文件中的所有lib复制到/WEB-INF/lib文件夹中。
  3. For JSTL libraries (which works well with struts) go here ( http://java.sun.com/products/jsp/jstl/ ) 对于JSTL库(适用于struts),请访问此处( http://java.sun.com/products/jsp/jstl/
  4. Once you have your Struts TLD's and JSTL Tld's, put them under the /WEB-INF/tld/ folder (it must be situated in the /WEB-INF/ folder). 获得Struts TLD和JSTL Tld之后,将它们放在/WEB-INF/tld/文件夹下(它必须位于/ WEB-INF /文件夹中)。
  5. 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的历史档案中下载了这个版本。

  1. 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.jarMETA-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.jarMETA-INF目录中,对应于Struts1中的struts-tiles.tld
  • some more TLDs, eg for SiteMesh which are not directly related to the question. 更多TLD,例如SiteMesh,与问题没有直接关系。

  1. 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: 它通常的工作方式如下:

  • 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. 当servlet容器启动时,它会查看WEB-INF/lib目录并加载它在那里找到的任何.jar文件 - 这是你需要放置Struts2库的地方。
  • Inside these .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已经是这种情况,所以没有什么需要做的。
  • 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. 当servlet容器加载TLD时,它会在根元素<taglib>查找<uri>元素,并存储该TLD与其URI之间的映射。 Correspondingly, this URI is used in your .jsp files to reference the TLD. 相应地,此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 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM