java - Angular 7 - 从 Angular 重定向到 jsp 页面
问题描述
我正在开发一个 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'},
];
每当我调用 URL https://localhost:9443/portal/或https://localhost:9443/portal/index.html时,它都会转到 index.html
但是当我调用https://localhost:9443/portal/index.jsp或https://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 : '/index.jsp'
不会让用户导航到index.jsp
page,但这意味着重定向到 ' routes' 数组中定义的'/index.jsp'路径。
我的意思是redirectTo 像这样工作:
const routes: Routes = [
{ path: 'manage.do', redirectTo : 'index', pathMatch : 'full'},
{ path: 'index', component: MyIndexComponent},
];
这意味着,如果 'routes' 数组中有一些路径项,redirectTo 可以正常工作。
现在,至于解决方案,我建议您.ts
通过编写 JavaScript 代码将用户导航到文件中的 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。
PS:我不确定在开发模式下一切正常。但希望适用于 prod 模式。
推荐阅读
- sql-server - 使用正则表达式检索 html 标签之间的字符串
- angular - Angular 6 - 无法检索静态数据
- excel - 根据 2 个列表创建一个 excel 报告
- apache-kafka - Apache Kafka 将平面 JSON 数组连接到对象
- ruby-on-rails - 缺少必需的选项:名称
- macos - 如何在 macOS 中更新 lldb
- wordpress - 在前端切换缺货商品 - woocommerce
- powershell - 将多个命令结果输出到单个 TXT 文件
- javascript - 子视图忽略父视图填充,因此它适合全宽
- ruby-on-rails - Rails - 需要应用程序中每个页面的通用反向链接