【问题标题】:Including html template in servlet在 servlet 中包含 html 模板
【发布时间】:2015-10-12 06:52:21
【问题描述】:

我想在我的 servlet 中包含页眉和页脚模板,就像 PHP 控制器中的 PHP 一样:

public doSomething()
{
    include "header.html";
    //generate doSomething content in HTML and echo it
    include "footer.html";
}

PS:这是一个例子,我不直接在 PHP 中这样做;)

这样,我想在所有 JSP 文件(包含)中避免这样:

<jsp:include page="header.html" />
<%-- Display doSomething informations -->
<jsp:include page="footer.html" />

没错,我想要:

public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
    //Include the header.html
    this.getServletContext().getRequestDispatcher( "/WEB-INF/Example.jsp" ).forward( request, response );
    //Include the footer.html
}

在这里,我想在 JSP 中包含不这样做的页脚和页眉。

【问题讨论】:

  • 你到底想要什么?
  • 转发到Example.jsp后,页眉和页脚html都会包含在这个jsp中。
  • 是的,但我问我是否可以在 Servlet 中包含页眉和页脚,而不是 JSP 文件。
  • 一种选择是将它们读入字符串并将它们设置为请求属性并在jsp中使用这些属性或将这些字符串写入响应并调用请求调度程序的include方法。
  • 谢谢@RP-,是我想要的。

标签: java html jakarta-ee servlets


【解决方案1】:

所以 RP 在 cmets 中回答了这个问题,但是为了给这个问题一个实际的回答,我把他复制到这里:

"一个选项是将它们读入字符串并将它们设置为请求属性并在 jsp 中使用这些属性或将这些字符串写入响应并调用请求调度程序的 include 方法。" - RP

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2017-10-07
    • 1970-01-01
    • 2015-08-31
    • 2018-10-26
    • 2014-02-04
    相关资源
    最近更新 更多