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

Java 在JSF中,我可以在URL中抑制XHTML文件的部分目录层次结构吗?,java,jsf,web-applications,servlets,url-mapping,Java,Jsf,Web Applications,Servlets,Url Mapping,在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-pat

在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>
我们希望通过删除
/xhtml
部分来简化URL,即。

我找不到任何方法来实现这一点。在
中是否有这样做的方法?我需要一些额外的框架吗?

您可以使用
过滤器来完成。国产或类似的第三方产品。只需将其映射到
*.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
子文件夹中,以便您能够以适当的方式利用
的功能

另见:

感谢您的详细回复。很高兴知道我可以使用过滤器,但是如果推荐的方法是将XHTML直接放到
webapp/
中,我想这就是我们要做的。没有必要把事情复杂化。。。
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);
}