是否可以在不使用 jsp:include 的情况下将 jspf 文件添加到 jsp 页面?

发布于 2024-12-07 10:05:53 字数 2051 浏览 0 评论 0原文

我想要实现的是类似于asp.net中的母版页的东西。我正在遵循教程,但是我可能错过了一些东西,因为我已将 header.jspf 和 footer.jspf 添加到 WEB-INF/jspf 文件夹中,而 index.jsp 位于 WEB-INF 之外。我在 web.xml 中添加了信息,以便某些 jsp 页面应该自动添加页眉和页脚。问题可能是 index.jsp 无法访问 WEB-INF 文件夹内的任何内容,但我认为我已经在教程的上一步中解决了这个问题。当我运行该项目时,我得到的只是删除所有页眉和页脚内容后 index.jsp 剩下的内容。

我不想使用: <%@include file="header.jspf" %><..jsp:include...>

屏幕截图:

项目屏幕截图

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <jsp-config>
        <jsp-property-group>
            <description>header and footer settings</description>
            <url-pattern>/index.jsp</url-pattern>
            <url-pattern>/WEB-INF/view/*</url-pattern>
            <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
            <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
        </jsp-property-group>
    </jsp-config>
</web-app>

header.jspf:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Webshop</title>
</head>
<body>
     <h1>Webshop</h1>

footer.jspf:

</body>
</html>

What I want to achieve is something similar to a master page in asp.net. I'm following a tutorial, but I may have missed something cause I have added my header.jspf and footer.jspf to the WEB-INF/jspf folder and index.jsp is outside of WEB-INF. I have added info in web.xml so that certain jsp-pages should automatically add the header and footer. The problem might be that index.jsp can't access anything inside the WEB-INF folder, but I thought I had solved that in a previous step in the tutorial. When I run the project, all I get is what's left of index.jsp after I remove all the header and footer stuff.

I don't want to use: <%@include file="header.jspf" %> and <..jsp:include...>.

Screenshot:

Screenshot of the project

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <jsp-config>
        <jsp-property-group>
            <description>header and footer settings</description>
            <url-pattern>/index.jsp</url-pattern>
            <url-pattern>/WEB-INF/view/*</url-pattern>
            <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
            <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
        </jsp-property-group>
    </jsp-config>
</web-app>

header.jspf:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Webshop</title>
</head>
<body>
     <h1>Webshop</h1>

footer.jspf:

</body>
</html>

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

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

发布评论

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

评论(1

喜爱皱眉﹌ 2024-12-14 10:05:53

我也在做那个教程,当我使用Tomcat作为服务器时,它不会包含页眉和页脚,我必须使用glassfish服务器,有什么办法让tomcat包含页眉和页脚吗?

编辑:

用这个标签替换默认标签似乎已经解决了使用 tomcat 作为服务器的问题

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

I'm also doing that tutorial, and when I use Tomcat as the server it won't include the header and the footer, I have to use the glassfish server, is there any way to make tomcat include the header and the footer?

EDIT:

Replacing the default tag with this one seems to have solved the problem using tomcat as the server

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文