我想要一个名为_contextPath
的变量,它是JSP中的Javascript评估变量,当前可用于systemjs.config.js
- 我试图让此_contextPath
变量可用在打字稿服务中。将Web应用程序上下文传递给Angular2服务
我想知道,如果_contextPath
变量可以被传递到打字稿服务称为job.service.ts
这里是我的文件夹结构:
├── scripts
│ └── mec-irs
│ ├── app
| | ├── app.component.ts
| | ├── app.module.ts
| | ├── app.routes.ts
| | └── jobs
| | ├── job.ts
| | ├── job.routes.ts
| | └── job.service.ts
│ ├── db.json
│ ├── GruntFile.js
│ ├── index.html
│ ├── node
│ ├── node_modules
│ ├── package.json
│ ├── style.css
│ ├── systemjs.config.js
│ ├── tsconfig.json
│ ├── typings
│ └── typings.json
├── views
│ ├── mec.jsp
的mec.jsp
获得通过JavaScript调用和存储上下文它在一个变量被称为_contextPath:
<html>
<head>
<title>MEC IRS</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script>var _contextPath = "${pageContext.request.contextPath}";</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="..<%=request.getContextPath()%>/scripts/mec-irs/style.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="..<%=request.getContextPath()%>/scripts/mec-irs/node_modules/core-js/client/shim.min.js"></script>
<script src="..<%=request.getContextPath()%>/scripts/mec-irs/node_modules/zone.js/dist/zone.js"></script>
<script src="..<%=request.getContextPath()%>/scripts/mec-irs/node_modules/reflect-metadata/Reflect.js"></script>
<script src="..<%=request.getContextPath()%>/scripts/mec-irs/node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="..<%=request.getContextPath()%>/scripts/mec-irs/systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
的_contextPath
用于在systemjs.config.js
:
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': _contextPath + '/scripts/mec-irs/node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: _contextPath + '/scripts/mec-irs/app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
的Java Web应用程序上下文被硬编码虽然job.service.ts
为mec
。我想知道如何通过_contextPath
这个打字稿服务job.service.ts
请。由于上下文mec
下面看到的是硬编码为/mec/admin/irs/jobs/
,它是一个网络服务端点:
import {Injectable, Inject} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
// Decorator to tell Angular that this class can be injected as a service to another class
@Injectable()
export class JobService {
// Class constructor with Jsonp injected
constructor(@Inject (Http)private http:Http) { }
// Base URI for Spring Batch Admin
private jobsUrl = '/batch/';
//TODO wish not to have hardcoded context, like below
//private mecUrl = '/' + _contextPath + '/admin/irs/jobs/';
private mecUrl = '/mec/admin/irs/jobs/';
// Stop Spring Batch Job by its name
stopJobByName(name: string) {
const endPoint = name + '/stopIrsJobPoller';
return this.http.get(this.mecUrl + endPoint)
.map(res => res.json());
}
.
.
.
为什么你需要这个变量? –
我需要这个变量,因为在服务中,我希望通过http:// hostname/_contextPath/someRestfulWS –