在 JSF 中,我可以在 URL 中隐藏 XHTML 文件的部分目录层次结构吗?

发布于 2024-12-22 10:22:14 字数 791 浏览 1 评论 0原文

在 JSF 应用程序中,我们有目录层次结构:

webapp
  xhtml
    login.xhtml
    main.xhtml
    search.xhtml
  css
    main.css
    extra.css
  js
    jquery.js

等等。servlet 映射是:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

这工作正常,但我们的 Web 应用程序的 URL 如下所示:

http://localhost/myapp/xhtml/login.xhtml
http://localhost/myapp/xhtml/search.xhtml

我们希望通过删除 /xhtml部分,即 http://localhost/myapp/login.xhtml

我找不到任何方法来完成此操作。有没有办法在 中做到这一点?我需要一些额外的框架吗?

In a JSF application, we have the directory hierarchy:

webapp
  xhtml
    login.xhtml
    main.xhtml
    search.xhtml
  css
    main.css
    extra.css
  js
    jquery.js

etc. The servlet mapping is:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

This works fine, but the URLs of our web app look like this:

http://localhost/myapp/xhtml/login.xhtml
http://localhost/myapp/xhtml/search.xhtml

We would like to have simpler URLs by dropping the /xhtml part, i.e.
http://localhost/myapp/login.xhtml

I could not find any way to accomplish this. Is there some way to do this in the <servlet-mapping>? Do I need some additional framework?

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

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

发布评论

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

评论(1

烟火散人牵绊 2024-12-29 10:22:14

可以使用过滤器来做到这一点。自行开发的过滤器或第三方过滤器,例如 URLRewriteFilter。只需将其映射到 *.xhtml 上,然后转发到 /xhtml/* 即可。

类似:

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

String ctx = request.getContextPath();
String uri = request.getRequestURI();
String viewId = uri.substring(ctx.length(), uri.length());

if (viewId.startsWith("/xhtml")) {
    // Redirect to URL without /xhtml (changes URL in browser address bar).
    response.setStatus(301);
    response.setHeader("Location", ctx + viewId.substring("/xhtml".length());
    // Don't use response.sendRedirect() as it does a temporary redirect (302).
} else {
    // Forward to the real location (doesn't change URL in browser address bar).
    request.getRequestDispatcher("/xhtml" + viewId).forward(request, response);
}

但更简单的方法是更改​​目录层次结构以删除 /xhtml 子文件夹。这些 CSS/JS(和图像)文件最好放置在 /resources 子文件夹中,以便您可以利用 的功能以正确的方式

另请参阅:

You could do it with a Filter. Either homegrown or a 3rd party one like URLRewriteFilter. Just map it on *.xhtml and then forward to /xhtml/*.

Something like:

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

String ctx = request.getContextPath();
String uri = request.getRequestURI();
String viewId = uri.substring(ctx.length(), uri.length());

if (viewId.startsWith("/xhtml")) {
    // Redirect to URL without /xhtml (changes URL in browser address bar).
    response.setStatus(301);
    response.setHeader("Location", ctx + viewId.substring("/xhtml".length());
    // Don't use response.sendRedirect() as it does a temporary redirect (302).
} else {
    // Forward to the real location (doesn't change URL in browser address bar).
    request.getRequestDispatcher("/xhtml" + viewId).forward(request, response);
}

But simpler would be to just change the directory hierarchy to get rid of /xhtml subfolder. Those CSS/JS (and image) files should preferably be placed in a /resources subfolder so that you can utilize the powers of <h:outputStylesheet>, <h:outputScript> and <h:graphicImage> in a proper way.

See also:

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