首页 文章

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

提问于
浏览
0

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

我的 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}/'
 }

添加了publicPath作为主要* .js和供应商* .js在没有根上下文的情况下提供404 .

routes.ts 具有以下根配置

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

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

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'rootContext/MyControllerServlet' Error: Cannot match any routes. URL Segment: 'rootContext/MyControllerServlet'

当使用Angular 1.6版本开发页面时,此重定向在此之前工作得非常好 . 我们甚至不需要使用publicPath来呈现与capmf.jsp位于同一文件夹中的主* .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>

我的问题是 -

  • 这是因为 **** 标签而发生的吗?我无法使用npm插件更改基本href标记 . 我用 base-href-webpack-pluginwebpack-base-href plugin 获得 not a constructor error

  • 当没有为src javascript文件添加rootContext时,publicPath是正确的处理方式吗?

  • 我还能做些什么来避免brand3.jsp的404错误?

1 回答

  • 0

    在基本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无关

相关问题