【问题标题】:Angular 7 - Redirect from angular to jsp pageAngular 7 - 从 Angular 重定向到 jsp 页面
【发布时间】:2020-07-30 00:18:55
【问题描述】:

我正在用 Angular 7 开发一个应用程序。它的构建是为了更新一个也支持 jsp 页面的遗留应用程序。目前,当我尝试调用 index.jsp 页面时,它会自动重定向到 index.html。我无法调用任何 jsp 页面。它们都在同一个基础href下。我需要将这些 jsp 页面用于某些部分。 应用部署在 websphere 上。

context-url 是门户

 <?xml version="1.0" encoding="UTF-8"?>
    <web-ext
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://websphere.ibm.com/xml/ns/javaee"
        xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_1.xsd" version="1.1">
      <reload-interval value="3"/>
      <context-root uri="portal" />
      <enable-directory-browsing value="false"/>
      <enable-file-serving value="true"/>
      <enable-reloading value="true"/>
      <enable-serving-servlets-by-class-name value="false"/>
    </web-ext>

我有 index.jsp 和 index.html。 web.xml 中的欢迎文件列表指向 index.html 在 web.xml 我有多个 url 模式

    <web-resource-collection>
        <web-resource-name>action</web-resource-name>
        <description />
        <url-pattern>/manage.do</url-pattern>
        <url-pattern>/save.do</url-pattern>
        <url-pattern>/legacy.do</url-pattern>
        <url-pattern>/acts.do</url-pattern>
    </web-resource-collection>

对于我拥有的 index.html 页面

我还在 app-routing-module.ts 中配置了应用路由

const routes: Routes = [
  { path: 'manage.do', redirectTo : '/index.jsp', pathMatch : 'full'},
];

每当我调用网址 https://localhost:9443/portal/https://localhost:9443/portal/index.html 时, 它转到 index.html

但是当我打电话给https://localhost:9443/portal/index.jsphttps://localhost:9443/portal/manage.do 时, 它仍然总是重定向到 index.html。

但是我需要它来调用jsp页面并重定向到遗留代码,任何人有任何建议

JSP页面调用manage.do

@RequestMapping(value = "/manage", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView portalMainPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

如您所见,它的返回类型为 modelandview,而不是可以通过 angular 重新生成的列表。

它有自己的jsp页面和自己的视图。有没有办法在同一个ear文件中重定向到jsp页面?

【问题讨论】:

  • redirectTo 用于重定向到 Angular 应用程序中的页面。看看这个stackoverflow.com/questions/40150393/…
  • 问题是我没有连接到外部链接。 jsp 和 angular 在同一个 EAR 文件中。所以问题是为什么 /portal/index.jsp 或 /portal/manage.do 会自动重定向到 index.html。我正在尝试渲染 jsp 页面,但它一直重定向到角度组件。

标签: java angular spring jsp servlets


【解决方案1】:

首先要知道redirectTo : '/index.jsp' 不允许用户导航到index.jsp 页面,但这意味着重定向到'/index.jsp' 路径在'routes'数组中定义。

我的意思是 redirectTo 像这样工作:

const routes: Routes = [
  { path: 'manage.do', redirectTo : 'index', pathMatch : 'full'},
  { path: 'index', component: MyIndexComponent},
];

这意味着,如果 'routes' 数组中有一些路径项,redirectTo 可以正常工作。

现在,至于解决方案,我建议您通过编写JavaScript代码将用户导航到.ts文件中的index.jsp页面,如下:

const routes: Routes = [
  { path: 'manage.do', redirectTo : 'index-jsp', pathMatch : 'full'},
  { path: 'index-jsp', component: MyIndexJspComponent},
];

并像这样创建一个组件作为 MyIndexJspComponent:

import { Component, OnInit } from '@angular/core';

@Component({
  templateUrl: '<div></div>',
  styleUrls: [],
})

export class MyIndexJspComponent implements OnInit {

  constructor() { }
  ngOnInit() {
    window.location.href = "/index.jsp";
  }
}

现在这适用于 Angular 项目架构,并且对您来说都是正确的。现在用户可以在任何地方调用“manage.do”,然后转到 index.jsp。

P.S.:我不确定它在开发模式下是否一切正常。但希望适用于 prod 模式。

【讨论】:

  • 我试过这样做。但是,页面仍然会自动从 index.jsp 转到 index.html,然后加载 angular 组件。我无法获得使用 jsp 在同一基本 href 中使用 angular 的旧代码。当我使用 /portal/mainpage.do 时,在使用开发人员工具的网络数据上,我看到请求方法:POST 状态代码:302 Found
  • 我什至尝试过使用 /portal/index.jsp,它也会自动重定向到 index.html。
猜你喜欢
  • 1970-01-01
  • 2021-01-28
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多