是否可以在自定义 TagSupport 类中包含 jsp,并指定正文应该放在哪里?

发布于 2024-07-14 04:50:52 字数 1078 浏览 4 评论 0原文

我想使用自定义 jsp 标签来帮助在我的应用程序中构建菜单。 然而,我希望所有实际的 HTML 都位于 JSP 文件中,而不是 Java 类中。

因此,假设我有一个像这样的标签:

<mytags:Menu>
  <mytags:MenuItem name="foo"/>
  <mytags:MenuItem name="bar"/>
  <mytags:MenuItem name="baz"/>
</mytags:Menu>

然后我可能有我的 Menu 标签的类,如下所示:

public class MenuPill extends TagSupport {
    public int doStartTag() throws JspException {
        try {
            pageContext.include("/menu/menu.jsp");
        } catch (ServletException e) {
            throw new JspException(e);
        } catch (IOException e) {
            throw new JspException(e);
        }
        return super.doStartTag();
    }
}

我的 menu.jsp 文件,它是包装器对于菜单本身,则可能如下所示:

<div id="menu>
  <%somehow include the body here%>
</div>

我想要做的是将 mytags:Menu 标记的主体放入 中,该标记将为实际菜单项生成 HTML menu.jsp,位于开始标记和结束标记之间。 我知道我可以将其分成两个不同的 jsp 文件,一个用于开始标记,一个用于结束标记,但这看起来很草率。

是否有可能做到这一点?

谢谢!

I want to use custom jsp tags to help build a menu in my application. However, I want all the actual HTML to live in JSP files, rather than in the Java class.

So, suppose I have a tag like this:

<mytags:Menu>
  <mytags:MenuItem name="foo"/>
  <mytags:MenuItem name="bar"/>
  <mytags:MenuItem name="baz"/>
</mytags:Menu>

I then might have the class for my Menu tag, that looks like this:

public class MenuPill extends TagSupport {
    public int doStartTag() throws JspException {
        try {
            pageContext.include("/menu/menu.jsp");
        } catch (ServletException e) {
            throw new JspException(e);
        } catch (IOException e) {
            throw new JspException(e);
        }
        return super.doStartTag();
    }
}

My menu.jsp file, which is the wrapper for the menu itself, then might look like this:

<div id="menu>
  <%somehow include the body here%>
</div>

What I want to do is put the body of my mytags:Menu tag, which will generate the HTML for the actual menu items, into the menu.jsp, between the opening and closing tags. I know I could break it up into two different jsp files, one for the start tag, and one for the end tag, but that seems sloppy.

Is it possible to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-07-21 04:50:52

我真的建议您使用 jsp 2.0 标记文件 而不是您建议的方法。 创建 Jsp 2.0 标记文件是为了解决您所描述的问题。 结合 jstl(如果真的很复杂的话,可能还可以结合 java 类),您应该能够达到相同的目的,但使用更简洁的方法,更好地分离代码和表示。

I really suggest you use jsp 2.0 tag files instead of the approach you suggest. Jsp 2.0 tag files were created to address problems like the one you describe. Combined with jstl (and possibly java classes if it's really complex), you should be able to achieve the same ends but with much cleaner means, with a much nicer separation of code and presentation.

回忆追雨的时光 2024-07-21 04:50:52

如果您的网络应用程序中的每个页面都需要相同的菜单,那么您可以考虑使用Tiles,用于定义复合视图

If every page in your webapp needs the same menu, then you might consider Tiles, for defining Composite Views

窝囊感情。 2024-07-21 04:50:52

您可以创建自定义标签,例如...

template.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>template</short-name>
  <uri>/template</uri>
  <tag-file>
    <name>block</name>
    <path>/META-INF/tags/template/block.tag</path>
  </tag-file>
  <tag-file>
    <name>stringBlock</name>
    <path>/META-INF/tags/template/stringBlock.tag</path>
  </tag-file>
</taglib>

实现

<%@tag import="org.mentawai.template.*" pageEncoding="UTF-8"%>
<%@attribute name="id" required="true"%>

<%
  Page page = (Page)request.getAttribute(TemplateServlet.PAGE_ATTR);
  Page block = page.getBlock(id);
  String view = "";
  if (block == null) {
      throw new ServletException("Block " + id + " doesn't exists");
  }
  request.setAttribute(TemplateServlet.PAGE_ATTR, block);
  view = block.getView();
  TemplateServlet.executeListener(block, request, response, application);

  String oldView = (String)request.getAttribute("_view");
  request.setAttribute("_view", "/" + view);
%>

<jsp:include page="${_view}" flush="true"/>

<%
  request.setAttribute("_view", oldView);
  request.setAttribute(TemplateServlet.PAGE_ATTR, page);
%>

参考:
http://docs.oracle.com/javaee/1.4/tutorial/doc /JSPTags5.html

You can create custom tag like...

template.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>template</short-name>
  <uri>/template</uri>
  <tag-file>
    <name>block</name>
    <path>/META-INF/tags/template/block.tag</path>
  </tag-file>
  <tag-file>
    <name>stringBlock</name>
    <path>/META-INF/tags/template/stringBlock.tag</path>
  </tag-file>
</taglib>

Implementation

<%@tag import="org.mentawai.template.*" pageEncoding="UTF-8"%>
<%@attribute name="id" required="true"%>

<%
  Page page = (Page)request.getAttribute(TemplateServlet.PAGE_ATTR);
  Page block = page.getBlock(id);
  String view = "";
  if (block == null) {
      throw new ServletException("Block " + id + " doesn't exists");
  }
  request.setAttribute(TemplateServlet.PAGE_ATTR, block);
  view = block.getView();
  TemplateServlet.executeListener(block, request, response, application);

  String oldView = (String)request.getAttribute("_view");
  request.setAttribute("_view", "/" + view);
%>

<jsp:include page="${_view}" flush="true"/>

<%
  request.setAttribute("_view", oldView);
  request.setAttribute(TemplateServlet.PAGE_ATTR, page);
%>

Reference:
http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文