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

时间:2017-05-01 14:17:32

标签: url-rewriting angular2-routing websphere-7 html-webpack-plugin servlet-2.5

我有一个旧的应用程序,它位于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。

错误错误:未捕获(在承诺中):错误:无法匹配任何路由。 URL段:'rootContext / MyControllerServlet' 错误:无法匹配任何路由。网址段:'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-plugin webpack-base-href插件 < 不是构造函数错误 / LI>
  • 当没有为src javascript文件添加rootContext时,publicPath是正确的处理方式吗?
  • 我还能做些什么来避免brand3.jsp的错误?

1 个答案:

答案 0 :(得分: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无关