简单的JSP网站

发布于 2024-08-15 14:16:58 字数 124 浏览 15 评论 0原文

我正在寻找使用相当于 PHP include+switch 功能创建简单 JSP 网站的帮助/建议。

目标是我希望能够在一个主页中的多个 JSP 包含页面之间进行切换。

上述“函数”最简单的形式是什么?

I'm looking for help/advice with creating simple JSP website using equivalent of PHP include+switch function.

The goal is that I want to be able to switch between multiple JSP include pages in one main page.

What would be the simplest possible form of above 'function'?

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

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

发布评论

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

评论(2

埋情葬爱 2024-08-22 14:16:58

在那里你有 < /code>为。您可以使用 EL 来指定 页面 属性。

创建一个 /WEB-INF/main.jsp 文件,如下所示:

<!doctype html>
<html lang="en">
    <head>
        <title>Title</title>
    </head>
    <body>
        <jsp:include page="${page}" />
    </body>
</html>

您可以借助页面控制器 servlet 来控制 ${page} 值。类似于:

public class PageController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("page", "/WEB-INF" + request.getPathInfo());
        request.getRequestDispatcher("/WEB-INF/main.jsp").forward(request, response);
    }

}

web.xml 中映射此 servlet,如下所示:

<servlet>
    <servlet-name>pageController</servlet-name>
    <servlet-class>com.example.PageController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>pageController</servlet-name>
    <url-pattern>/page/*</url-pattern>
</servlet-mapping>

这样可以通过 http://example.com/context/page/foo.jsp 访问该 servlet,并且在此 URL 示例中,它将从路径信息中获取 /foo.jsp ,从而将 page 属性设置为值 /WEB-INF/foo.jsp< /code> 以便它在 EL 中可用 ${page} 以便 jsp:include 知道它应该包含什么。不需要讨厌的 scriptlet 或 switch 语句。

/WEB-INF/foo.jsp 中,您只需写下 HTML,就像将其放置在 HTML 标记内一样。

请注意,JSP 文件放置在 /WEB-INF 中,这样做是为了防止通过 URL 直接访问,这样用户就无法在不通过页面控制器的情况下请求它们,例如 http://example.com/context/foo.jsp 只会返回部分内容(要包含的页面)。

希望这有帮助。

There you have the <jsp:include> for. You can use EL to specify the page attribute.

Create a /WEB-INF/main.jsp file which look like:

<!doctype html>
<html lang="en">
    <head>
        <title>Title</title>
    </head>
    <body>
        <jsp:include page="${page}" />
    </body>
</html>

You can control the ${page} value with help of a page controller servlet. Something like:

public class PageController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("page", "/WEB-INF" + request.getPathInfo());
        request.getRequestDispatcher("/WEB-INF/main.jsp").forward(request, response);
    }

}

Map this servlet in web.xml as follows:

<servlet>
    <servlet-name>pageController</servlet-name>
    <servlet-class>com.example.PageController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>pageController</servlet-name>
    <url-pattern>/page/*</url-pattern>
</servlet-mapping>

This way the servlet is accessible through http://example.com/context/page/foo.jsp and in this URL example it will then get /foo.jsp from the pathinfo and thus set the page attribute with the value /WEB-INF/foo.jsp so that it is available in EL as ${page} so that the jsp:include knows what it should include. No need for nasty scriptlets or switch statements.

In the /WEB-INF/foo.jsp you can just write down HTML as if it is placed inside the HTML <body> tag.

Note that the JSP files are placed in /WEB-INF, this is done so to prevent direct access by URL so that the users cannot request them without going through the page controller, such as for example http://example.com/context/foo.jsp which would only return the partial content (the to-be-included page).

Hope this helps.

夏末 2024-08-22 14:16:58

尝试

<%  if ( expression1 ) { %>
    <%@ include file="file1.jspf" %>
<% } else if(expression2) { %>
    <%@ include file="file2.jspf" %>
<% } %>

或者,如果可以的话,请查看 JSF2 和/或 Facelets。它具有更强大的模板功能。

Try

<%  if ( expression1 ) { %>
    <%@ include file="file1.jspf" %>
<% } else if(expression2) { %>
    <%@ include file="file2.jspf" %>
<% } %>

Or, if you have the option, check out JSF2 and/or Facelets. It has much more powerful templating capabilities.

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