如何为 Angular 应用配置 Weblogic 应用服务器
How to configure Weblogic application Sever for Angular application
我有一个 Angular (6.1) 应用程序,它打包在 WAR(和 EAR)文件中,应该部署到 Weblogic (12c)。
基于 this link 所有对应用程序(上下文根)的请求都应路由到 index.html
应用程序文件。
文档中有一些配置示例,但没有一个适用于像 WebLogic 这样的应用程序服务器。
因为它应该与应用程序相结合,所以它应该在 WAR 中,我想在 web.xml
中使用 servlet 映射。我玩过它但没有让它工作。
(内部服务器错误,未找到默认以外的其他视图...即使我在 tomcat 中使用纯 WAR,WebLogic 拒绝这样做,...)
在投入大量时间之前 - 这是正确的方法吗?
如果是这样,正确的 mapping/pattern 会是什么样子?
如果没有,在 WAR 中配置它的另一种方法是什么?
使用 servlet 过滤器。如果请求是 GET,并且应该转发到 index.html,则将其转发到 index.html。否则,将请求传递到链中。这是此类过滤器的示例。当然,根据您的应用程序的体系结构,条件可能会有所不同:
@WebFilter(value = "/*")
public class IndexFilter implements Filter {
@Override
public void doFilter(ServletRequest req,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
if (mustForward(request)) {
request.getRequestDispatcher("/index.html").forward(request, response);
return;
}
chain.doFilter(request, response);
}
private boolean mustForward(HttpServletRequest request) {
if (!request.getMethod().equals("GET")) {
return false;
}
String uri = request.getRequestURI();
return !(uri.startsWith("/api")
|| uri.endsWith(".js")
|| uri.endsWith(".css")
|| uri.startsWith("/index.html")
|| uri.endsWith(".ico")
|| uri.endsWith(".png")
|| uri.endsWith(".jpg")
|| uri.endsWith(".gif")
|| uri.endsWith(".eot")
|| uri.endsWith(".svg")
|| uri.endsWith(".woff2")
|| uri.endsWith(".ttf")
|| uri.endsWith(".woff");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// nothing to do
}
@Override
public void destroy() {
// nothing to do
}
}
如果它比创建您自己的过滤器更方便,您可以使用:org.tuckey.urlrewritefilter http://tuckey.org/urlrewrite/
简单的 3 个步骤:
- 将依赖项添加到您的 pom.xml
- 将过滤器 UrlRewriteFilter 从 tuckey 添加到 web.xml
- 配置过滤器
urlrewrite.xml
根据我的经验,它非常方便(尤其是与 web.xml 中支持的模式相比)。
示例规则可以是:
<rule>
<from>^/webapp/*</from>
<to>/webapp/index.html</to>
</rule>
将以下代码添加到 web.xml,而不将 index.html 添加到您的 url,http://yourwebsite/ 将成功加载您的站点。
出于某种原因,将此条目添加到现有的欢迎文件列表不起作用。我是 java 的新手,所以我不确定为什么。如下所示添加带有 index.html 的欢迎文件列表的新部分解决了该问题。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
我有一个 Angular (6.1) 应用程序,它打包在 WAR(和 EAR)文件中,应该部署到 Weblogic (12c)。
基于 this link 所有对应用程序(上下文根)的请求都应路由到 index.html
应用程序文件。
文档中有一些配置示例,但没有一个适用于像 WebLogic 这样的应用程序服务器。
因为它应该与应用程序相结合,所以它应该在 WAR 中,我想在 web.xml
中使用 servlet 映射。我玩过它但没有让它工作。
(内部服务器错误,未找到默认以外的其他视图...即使我在 tomcat 中使用纯 WAR,WebLogic 拒绝这样做,...)
在投入大量时间之前 - 这是正确的方法吗?
如果是这样,正确的 mapping/pattern 会是什么样子?
如果没有,在 WAR 中配置它的另一种方法是什么?
使用 servlet 过滤器。如果请求是 GET,并且应该转发到 index.html,则将其转发到 index.html。否则,将请求传递到链中。这是此类过滤器的示例。当然,根据您的应用程序的体系结构,条件可能会有所不同:
@WebFilter(value = "/*")
public class IndexFilter implements Filter {
@Override
public void doFilter(ServletRequest req,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
if (mustForward(request)) {
request.getRequestDispatcher("/index.html").forward(request, response);
return;
}
chain.doFilter(request, response);
}
private boolean mustForward(HttpServletRequest request) {
if (!request.getMethod().equals("GET")) {
return false;
}
String uri = request.getRequestURI();
return !(uri.startsWith("/api")
|| uri.endsWith(".js")
|| uri.endsWith(".css")
|| uri.startsWith("/index.html")
|| uri.endsWith(".ico")
|| uri.endsWith(".png")
|| uri.endsWith(".jpg")
|| uri.endsWith(".gif")
|| uri.endsWith(".eot")
|| uri.endsWith(".svg")
|| uri.endsWith(".woff2")
|| uri.endsWith(".ttf")
|| uri.endsWith(".woff");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// nothing to do
}
@Override
public void destroy() {
// nothing to do
}
}
如果它比创建您自己的过滤器更方便,您可以使用:org.tuckey.urlrewritefilter http://tuckey.org/urlrewrite/
简单的 3 个步骤:
- 将依赖项添加到您的 pom.xml
- 将过滤器 UrlRewriteFilter 从 tuckey 添加到 web.xml
- 配置过滤器 urlrewrite.xml
根据我的经验,它非常方便(尤其是与 web.xml 中支持的模式相比)。
示例规则可以是:
<rule>
<from>^/webapp/*</from>
<to>/webapp/index.html</to>
</rule>
将以下代码添加到 web.xml,而不将 index.html 添加到您的 url,http://yourwebsite/ 将成功加载您的站点。 出于某种原因,将此条目添加到现有的欢迎文件列表不起作用。我是 java 的新手,所以我不确定为什么。如下所示添加带有 index.html 的欢迎文件列表的新部分解决了该问题。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>