[英]Custom tag libraries in JSP with Java EE 7
I am having a difficult time to find the right way to implement my custom tags in JSP with Java EE 7. I could see the web.xml 'taglig' elements have been obsoleted and Java EE 7 documentation talks just about JSF custom tags implementation but not JSPs.Unfortunately, I don't have a chance to move to JSF. 我很难找到使用Java EE 7在JSP中实现自定义标签的正确方法。我可以看到web.xml'taglig'元素已过时,并且Java EE 7文档仅讨论JSF自定义标签的实现,但是不是JSP。不幸的是,我没有机会迁移到JSF。 Any help is greatly appreciated. 任何帮助是极大的赞赏。
You might need to be more specific on your custom tag use case. 您可能需要在自定义标签用例上更加具体。 The easiest way of defining a custom tag is defining .tag file (type of JSP with extension .tag) and place it into {yourapp}/WEB-INF/tags/{dir} 定义定制标记的最简单方法是定义.tag文件(扩展名为.tag的JSP类型)并将其放入{yourapp} / WEB-INF / tags / {dir}
eg 例如
I have a custom.tag in WEB-INF/tags/custom 我在WEB-INF / tags / custom中有一个custom.tag
<%@attribute name="greetName" type="java.lang.String"%>
<div>Hello, ${greetName }</div>
In my index.jsp 在我的index.jsp中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib tagdir="/WEB-INF/tags/custom" prefix="ctm" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP in EE7</title>
</head>
<body>
<div>Before tag</div>
<ctm:custom greetName="John"></ctm:custom>
<div>After tag</div>
</body>
</html>
Output: 输出:
There's also way to define tag using java code: 1. Create a .tld file in WEB-INF 2. Extending BodyTagSupport or SimpleTagSupport class 还有一种使用Java代码定义标签的方法:1.在WEB-INF中创建一个.tld文件。2.扩展BodyTagSupport或SimpleTagSupport类
.tag file is great for reusing some fragment of html and extend the *TagSupport classes if you need more flexibility. 如果需要更大的灵活性,.tag文件非常适合重用html的某些片段并扩展* TagSupport类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.