从 Servlet 重定向到 Angular2 页面导致 404

Redirection to Angular2 page from Servlet results in 404

我有一个旧应用程序 Java 1.6,Servlet 2.5,并在 Websphere Application Server 7.x 版本上部署为 WAR。现在,我正在尝试使用 Angular2 框架和打字稿添加一个新编写的页面。

我的 web.xml 具有以下 servlet 映射。

<servlet>
        <servlet-name>MyControllerServlet</servlet-name>
        <servlet-class>com.company.path.MyControllerServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>MyControllerServlet</servlet-name>
    <url-pattern>/MyControllerServlet</url-pattern>
</servlet-mapping>

在我们现有的应用程序中,我们将带有查询参数的 GET 请求传递给我们的 servlet 以解决重定向问题。

http://localhost:9081/rootContext/MyControllerServlet?brand=brand1 http://localhost:9081/rootContext/MyControllerServlet?brand=brand2 等等。为此,我们必须在品牌为 brand3 时添加新创建的 Angular2 页面。 因此,http://localhost:9081/rootContext/MyControllerServlet?brand=brand3 应该重定向到 Angular2 页面。

MyControllerServlet.com

if(brand.equalsIgnoreCase(BRAND1)){
  response.sendRedirect(BRAND1_PAGE);
}else if(brand.equalsIgnoreCase(BRAND2)){
  response.sendRedirect(BRAND2_PAGE);
}

这对于所有都是 JSP 的现有页面来说效果很好。新添加的 Angular2 页面名为 brand3.jsp,配置如下。

webpack-dist-conf.js

new HtmlWebpackPlugin({
   template: conf.path.src('index.html'),                   
   filename: 'brand3.jsp'
})

输出块看起来像这样。

 output: {
    path: path.join(process.cwd(), conf.paths.dist),
    filename: '[name]-[hash].js',
    publicPath: '${pageContext.request.contextPath}/'
 }

The publicPath was added as the main*.js and vendor*.js were giving 404 without the root context.

routes.ts有如下root配置

{
 path: '',
 redirectTo: 'landingpage',
 pathMatch: 'full'
},
{
 path: 'landingpage',
 component: LandingPageComponent
}

挑战是当我们重定向到 brand3.jsp 时,我们得到一个 404 并出现以下错误。

ERROR 错误:未捕获(承诺):错误:无法匹配任何路由。 URL 细分:'rootContext/MyControllerServlet' 错误:无法匹配任何路由。 URL 段:'rootContext/MyControllerServlet'

当页面是使用 Angular 1.6 版本开发时,此重定向在早期工作得很好。我们甚至不需要 publicPath 来呈现位于 capmf.jsp.

同一文件夹中的 main*.js 和 vendor*.js

仅在升级到 TypeScript 和 Angular2.

后问题才开始出现

我读到 Angular2 路由需要一些服务器端配置。为此,我尝试使用具有以下配置的 tuckey 的 UrlRewriteFilter。

web.xml

<filter>
 <filter-name>UrlRewriteFilter</filter-name>
 <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
 <init-param>
  <param-name>logLevel</param-name>
  <param-value>DEBUG</param-value>
 </init-param>
</filter>
<filter-mapping>
 <filter-name>UrlRewriteFilter</filter-name>
 <url-pattern>/*</url-pattern>
 <dispatcher>REQUEST</dispatcher>
 <dispatcher>FORWARD</dispatcher>
</filter-mapping>

urlrewrite.xml

<rule enabled="true">
    <note>Do not process URL ending at index.html</note>
    <from>contextRoot/brand3.jsp$</from>
    <to last="true">-</to>
</rule>

<rule>
    <from>contextRoot/landingpage</from>
    <to>contextRoot/brand3.jsp</to>
</rule>

我知道 URL 重写工作正常,因为我可以看到调试语句。但是,我以前没有使用过它,不确定是否正确配置了它。

附加说明 - 我们使用 Maven3 构建应用程序,对于 Angular 个页面,我们使用

<groupId>com.github.eirslett</groupId>
  <artifactId>frontend-maven-plugin</artifactId>
<version>1.4</version>

我的问题是 -

在基本 href 中添加上下文根是正确的解决方案。

new BaseHrefWebpackPlugin({ baseHref: '${pageContext.request.contextPath}/contextRoot' })

添加基础 href 更正后,输出块中不需要 publicPath 以及 JS 文件也找到了,但没有给出 404。

output: {
 path: path.join(process.cwd(), conf.paths.dist),
 filename: '[name]-[hash].js'
} 

URL如果刷新页面,系统仍然需要重写才能正常工作。它与我们在缺少基本 href 标签时得到的 404 无关