似乎有两种使用 JSP 进行模板化的方法。包含具有这些语句之一的文件
<%@ include file="foo.html" %>
<jsp:include page="foo.html" />
或者使用JSP标签文件
// Save this as mytag.tag
<%@ tag description="Description" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<jsp:doBody/>
</body>
</html>
并在另一个 JSP 页面中调用它
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:mytag>
<h1>Hello World</h1>
</t:mytag>
那么我应该使用哪种方法呢?一种现在被认为已弃用,还是它们既有效又涵盖不同的用例?
编辑
使用这个标签文件不是和使用包含一样吗?
// Save this as product.tag
<%@ tag description="Product templage" pageEncoding="UTF-8"%>
<%@ tag import="com.myapp.Product" %>
<%@ attribute name="product" required="true" type="com.myapp.Product"%>
Product name: ${product.name} <br/>
Quantity: ${product.quantity} <br/>
然后在另一个 JSP 上调用它
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:product>
<c:forEach items="${cart.products}" var="product">
<t:product product="${product}"/>
</c:forEach>
</t:product>
在我看来,这与使用包含并向其传递参数非常相似。那么标记文件与包含相同吗?
最佳答案
首先,为了让事情更清楚,这里是JSP syntax elements 的简短概述。 :
有多种机制可以重用 JSP 文件中的内容。
以下在JSP中包含内容的4种机制可以归类为直接重用:
(前 3 种机制引用自 "Head First Servlets and JSP")
1) The include directive:
<%@ include file="header.html" %>Static: adds the content from the value of the file attribute to the current page at translation time. The directive was originally intended for static layout templates, like HTML headers.
2) The
<jsp:include>standard action:<jsp:include page="header.jsp" />Dynamic: adds the content from the value of the page attribute to the current page at request time. Was intended more for dynamic content coming from JSPs.
3) The
<c:import>JSTL tag:<c:import url=”http://www.example.com/foo/bar.html” />Dynamic: adds the content from the value of the URL attribute to the current page, at request time. It works a lot like
<jsp:include>, but it’s more powerful and flexible: unlike the other two includes, the<c:import>URL can be from outside the web Container!4) Preludes and codas:
Static: preludes and codas can be applied only to the beginnings and ends of pages.
You can implicitly include preludes (also called headers) and codas (also called footers) for a group of JSP pages by adding<include-prelude>and<include-coda>elements respectively within a<jsp-property-group>element in the Web application web.xml deployment descriptor.Read more here:
• Configuring Implicit Includes at the Beginning and End of JSPs
• Defining implicit includes
标签文件是一种间接方法的内容重用,封装可重用内容的方式。 标记文件是一个源文件,其中包含可作为自定义标记重用的 JSP 代码片段。
包含文件和标记文件的目的不同。
Tag file (JSP 2.0 引入的一个概念)是用于创建自定义标签 的选项之一。这是构建自定义标签的一种更快、更简单的方法。 Custom tags ,也称为标记扩展,是 JSP 元素,允许将其他 Java 组件提供的自定义逻辑和输出插入到 JSP 页面中。通过自定义标记提供的逻辑由称为 标记处理程序的 Java 对象实现。
可以由自定义标签执行的一些任务示例包括对隐式对象进行操作、处理表单、访问数据库和其他企业服务(如电子邮件和目录)以及实现流控制。
也许在您的示例中(在您的 “编辑” 段落中),使用直接包含和标记文件之间没有区别。但是自定义标签具有丰富的功能。他们可以
通过调用页面传递的属性进行自定义。
将变量传回调用页面。
访问所有可用于 JSP 页面的对象。
互相交流。您可以创建和初始化一个 JavaBeans 组件,在一个标签中创建一个引用该 bean 的公共(public) EL 变量,然后在另一个标签中使用该 bean。
相互嵌套并通过私有(private)变量进行通信。
还可以从“Pro JSP 2”中阅读:Understanding JSP Custom Tags .
Difference between include directive and include action in JSP
来自 coreservlet.com 的内容丰富且易于理解的教程,内容精美
包括 <jsp:include> VS. <%@ include %> 的解释
对照表:
Including Files and Applets in JSP
Pages
来自 coreservlets.com 的另一个不错的教程,与标签库和
标记文件:
Creating Custom JSP Tag Libraries: The
Basics
官方 Java EE 5 教程及示例:
Encapsulating Reusable Content
Using Tag
Files .
来自官方 Java EE 5 教程的这个页面应该给你甚至
更多了解:
Reusing Content in JSP
Pages .
这本书摘自“Pro JSP 2”还讨论了为什么需要
一个标签文件,而不是使用静态包含:
Reusing Content with Tag
Files
Oracle 文档中非常有用的指南:
Static Includes Versus Dynamic Includes
Use the right tools for each task.
使用标签文件作为创建自定义标签的一种快速简便的方法,可以帮助您封装可重复使用的内容.
关于 JSP 中包含的内容(引自 here ):
- Use the include directive if the file changes rarely. It’s the fastest mechanism. If your container doesn’t automatically detect changes, you can force the changes to take effect by deleting the main page class file.
- Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.
关于java - 使用 JSP 包含指令、JSP 包含操作和使用 JSP 标记文件包含文件有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580120/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题