回文集目錄:JHipster一知半解
docs目錄
這個目錄是最簡單的一個組件,內(nèi)嵌了一個iframe,顯示swagger。
<iframe src="swagger-ui/index.html" width="100%" height="900" seamless
target="_top" title="Swagger UI" class="border-0"></iframe>
health目錄
health.service.ts
與spring原生的management/health進行通訊,獲取系統(tǒng)的健康信息。
值得注意的是addHealthObject(),內(nèi)部還有transformHealthData()對返還的數(shù)據(jù)進行解析。
JhiHealthModalComponent組件
顯示健康信息詳情的模態(tài)框,對硬盤容量數(shù)據(jù)有特殊處理。
JhiHealthCheckComponent組件
根據(jù)后端數(shù)據(jù)返還值,顯示當前系統(tǒng)狀態(tài)。有通過refresh(),重新獲取和showHealth()跳出詳細模態(tài)框的兩個方法。
refresh() {
//先修改自己的狀態(tài)
this.updatingHealth = true;
//調(diào)用通訊接口獲取健康信息
this.healthService.checkHealth().subscribe((health) => {
this.healthData = this.healthService.transformHealthData(health);
this.updatingHealth = false;
}, (error) => {
//如果報錯,就顯示錯誤信息
if (error.status === 503) {
this.healthData = this.healthService.transformHealthData(error.json());
this.updatingHealth = false;
}
});
}
//通過modalService彈出模態(tài)框
showHealth(health: any) {
const modalRef = this.modalService.open(JhiHealthModalComponent);
//向模態(tài)框賦值的方法要通過componentInstance
modalRef.componentInstance.currentHealth = health;
//由于只是顯示,并沒有額外操作
modalRef.result.then((result) => {
// Left blank intentionally, nothing to do here
}, (reason) => {
// Left blank intentionally, nothing to do here
});
}
logs目錄
與web.rest.LogsResource對應,內(nèi)部使用了logback的LoggerContext修改日志等級。
顯示所有l(wèi)og,和修改的html如下:
<tr *ngFor="let logger of (loggers | pureFilter:filter:'name' | orderBy:orderProp:reverse)">
<!--利用slice管道截斷日志名-->
<td><small>{{logger.name | slice:0:140}}</small></td>
<td>
<!--利用ngClass顯示當前等級,默認用btn-light的樣式-->
<!--每個等級都是一個按鈕,點擊時候觸發(fā)changeLevel()方法-->
<button (click)="changeLevel(logger.name, 'TRACE')" [ngClass]="(logger.level=='TRACE') ? 'btn-danger' : 'btn-light'" class="btn btn-sm">TRACE</button>
<button (click)="changeLevel(logger.name, 'DEBUG')" [ngClass]="(logger.level=='DEBUG') ? 'btn-warning' : 'btn-light'" class="btn btn-sm">DEBUG</button>
<button (click)="changeLevel(logger.name, 'INFO')" [ngClass]="(logger.level=='INFO') ? 'btn-info' : 'btn-light'" class="btn btn-sm">INFO</button>
<button (click)="changeLevel(logger.name, 'WARN')" [ngClass]="(logger.level=='WARN') ? 'btn-success' : 'btn-light'" class="btn btn-sm">WARN</button>
<button (click)="changeLevel(logger.name, 'ERROR')" [ngClass]="(logger.level=='ERROR') ? 'btn-primary' : 'btn-light'" class="btn btn-sm">ERROR</button>
</td>
</tr>
metrics目錄
顯示spring提供的統(tǒng)計信息,與spring原生的management/metrics和management/dump交互,其中現(xiàn)場狀態(tài)是在模態(tài)框顯示的。這個還是推薦jvm自帶的jvisual。
user-management目錄
用戶管理是該模塊最復雜的一個頁面,包含用戶增刪改查、激活/反激活、權限編輯。
user-modal.service.ts
由于用戶管理邏輯在UserService中,UserModalService的作用是彈出交互的模態(tài)框
export class UserModalService {
private ngbModalRef: NgbModalRef;
//注入modalService,路由,用戶服務
constructor(
private modalService: NgbModal,
private router: Router,
private userService: UserService
) {
this.ngbModalRef = null;
}
open(component: Component, id?: number | any): Promise<NgbModalRef> {
return new Promise<NgbModalRef>((resolve, reject) => {
const isOpen = this.ngbModalRef !== null;
if (isOpen) {
resolve(this.ngbModalRef);
}
//根據(jù)是否有傳入Id信息,顯示不同的內(nèi)容,
if (id) {
this.userService.find(id).subscribe((user) => {
this.ngbModalRef = this.userModalRef(component, user);
resolve(this.ngbModalRef);
});
} else {
// setTimeout used as a workaround for getting ExpressionChangedAfterItHasBeenCheckedError
//注冊頁面需要異步打開。
setTimeout(() => {
this.ngbModalRef = this.userModalRef(component, new User());
resolve(this.ngbModalRef);
}, 0);
}
});
}
//內(nèi)部方法,應該加個private
userModalRef(component: Component, user: User): NgbModalRef {
const modalRef = this.modalService.open(component, { size: 'lg', backdrop: 'static'});
modalRef.componentInstance.user = user;
modalRef.result.then((result) => {
//這里的模態(tài)返回就有操作了,調(diào)用路由的navigate(),進行用戶信息的跳轉,并且清空ngbModalRef。
this.router.navigate([{ outlets: { popup: null }}], { replaceUrl: true, queryParamsHandling: 'merge' });
this.ngbModalRef = null;
}, (reason) => {
this.router.navigate([{ outlets: { popup: null }}], { replaceUrl: true, queryParamsHandling: 'merge' });
this.ngbModalRef = null;
});
return modalRef;
}
user-management.component.ts
export class UserMgmtComponent implements OnInit, OnDestroy {
//當前賬號
currentAccount: any;
//記錄查詢回來的所有用戶
users: User[];
error: any;
success: any;
routeData: any;
links: any;
totalItems: any;
queryCount: any;
itemsPerPage: any;
page: any;
predicate: any;
previousPage: any;
reverse: any;
constructor(
private userService: UserService,
private alertService: JhiAlertService,
private principal: Principal,
private parseLinks: JhiParseLinks,
private activatedRoute: ActivatedRoute,
private router: Router,
private eventManager: JhiEventManager
) {
this.itemsPerPage = ITEMS_PER_PAGE;
//從當前的URL信息中獲取路由信息
this.routeData = this.activatedRoute.data.subscribe((data) => {
this.page = data['pagingParams'].page;
this.previousPage = data['pagingParams'].page;
this.reverse = data['pagingParams'].ascending;
this.predicate = data['pagingParams'].predicate;
});
}
//初始化時候,記錄當前用戶,加載第一頁用戶列表,注冊用戶變動通知
ngOnInit() {
this.principal.identity().then((account) => {
this.currentAccount = account;
this.loadAll();
this.registerChangeInUsers();
});
}
//銷毀時候要反注冊
ngOnDestroy() {
this.routeData.unsubscribe();
}
registerChangeInUsers() {
this.eventManager.subscribe('userListModification', (response) => this.loadAll());
}
//設置激活,如果成功,重新加載頁面數(shù)據(jù)
setActive(user, isActivated) {
user.activated = isActivated;
this.userService.update(user).subscribe(
(response) => {
if (response.status === 200) {
this.error = null;
this.success = 'OK';
this.loadAll();
} else {
this.success = null;
this.error = 'ERROR';
}
});
}
//調(diào)用userService查詢
loadAll() {
this.userService.query({
page: this.page - 1,
size: this.itemsPerPage,
sort: this.sort()}).subscribe(
(res: ResponseWrapper) => this.onSuccess(res.json, res.headers),
(res: ResponseWrapper) => this.onError(res.json)
);
}
trackIdentity(index, item: User) {
return item.id;
}
//排序回調(diào)方法,默認根據(jù)ID排序,與sort指令對應
sort() {
const result = [this.predicate + ',' + (this.reverse ? 'asc' : 'desc')];
if (this.predicate !== 'id') {
result.push('id');
}
return result;
}
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.transition();
}
}
//重新定向到用戶管理頁面
transition() {
this.router.navigate(['/user-management'], {
queryParams: {
page: this.page,
sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
}
});
this.loadAll();
}
//查詢成功時候的回調(diào)
private onSuccess(data, headers) {
this.links = this.parseLinks.parse(headers.get('link'));
this.totalItems = headers.get('X-Total-Count');
this.queryCount = this.totalItems;
this.users = data;
}
//查詢失敗時候的回調(diào)
private onError(error) {
this.alertService.error(error.error, error.message, null);
}
}
user-management.component.html
<div>
<h2>
<span jhiTranslate="userManagement.home.title">Users</span>
<!--這里制定新建用戶的彈出框的outlet為‘popup’,直接在html定義router,這樣也行-->
<button class="btn btn-primary float-right jh-create-entity" [routerLink]="['/', { outlets: { popup: ['user-management-new'] } }]">
<span class="fa fa-plus"></span> <span jhiTranslate="userManagement.home.createLabel">Create a new User</span>
</button>
</h2>
<jhi-alert></jhi-alert>
<div class="table-responsive" *ngIf="users">
<table class="table table-striped">
<thead>
<!--表頭,與自定義的jhisort和jhiSortBy指令配合,完成排序工作-->
<tr jhiSort [(predicate)]="predicate" [(ascending)]="reverse" [callback]="transition.bind(this)">
<th jhiSortBy="id"><span jhiTranslate="global.field.id">ID</span> <span class="fa fa-sort"></span></th>
<th jhiSortBy="login"><span jhiTranslate="userManagement.login">Login</span> <span class="fa fa-sort"></span></th>
<th jhiSortBy="email"><span jhiTranslate="userManagement.email">Email</span> <span class="fa fa-sort"></span></th>
<th></th>
<th jhiSortBy="langKey"> <span jhiTranslate="userManagement.langKey">Lang Key</span> <span class="fa fa-sort"></span></th>
<th><span jhiTranslate="userManagement.profiles">Profiles</span></th>
<th jhiSortBy="createdDate"><span jhiTranslate="userManagement.createdDate">Created Date</span> <span class="fa fa-sort"></span></th>
<th jhiSortBy="lastModifiedBy"><span jhiTranslate="userManagement.lastModifiedBy">Last Modified By</span> <span class="fa fa-sort"></span></th>
<th jhiSortBy="lastModifiedDate"><span jhiTranslate="userManagement.lastModifiedDate">Last Modified Date</span> <span class="fa fa-sort"></span></th>
<th></th>
</tr>
</thead>
<!--循環(huán)讀取用戶信息,顯示在表格里面-->
<tbody *ngIf ="users">
<tr *ngFor="let user of users; trackBy: trackIdentity">
<td><a [routerLink]="['../user-management', user.login]">{{user.id}}</a></td>
<td>{{user.login}}</td>
<td>{{user.email}}</td>
<td>
<button class="btn btn-danger btn-sm" (click)="setActive(user, true)" *ngIf="!user.activated"
jhiTranslate="userManagement.deactivated">Deactivated</button>
<button class="btn btn-success btn-sm" (click)="setActive(user, false)" *ngIf="user.activated"
[disabled]="currentAccount.login === user.login" jhiTranslate="userManagement.activated">Activated</button>
</td>
<td>{{user.langKey}}</td>
<td>
<div *ngFor="let authority of user.authorities">
<span class="badge badge-info">{{ authority }}</span>
</div>
</td>
<td>{{user.createdDate | date:'dd/MM/yy HH:mm'}}</td>
<td>{{user.lastModifiedBy}}</td>
<td>{{user.lastModifiedDate | date:'dd/MM/yy HH:mm'}}</td>
<td class="text-right">
<div class="btn-group flex-btn-group-container">
<button type="submit"
[routerLink]="['../user-management', user.login]"
class="btn btn-info btn-sm">
<span class="fa fa-eye"></span>
<span class="d-none d-md-inline" jhiTranslate="entity.action.view">View</span>
</button>
<!--直接定義route,彈出編輯框-->
<button type="submit"
[routerLink]="['/', { outlets: { popup: 'user-management/'+ user.login + '/edit'} }]"
replaceUrl="true"
queryParamsHandling="merge"
class="btn btn-primary btn-sm">
<span class="fa fa-pencil"></span>
<span class="d-none d-md-inline" jhiTranslate="entity.action.edit">Edit</span>
</button>
<!--直接定義route,彈出刪除確認框-->
<button type="submit"
[routerLink]="['/', { outlets: { popup: 'user-management/'+ user.login + '/delete'} }]"
replaceUrl="true"
queryParamsHandling="merge"
class="btn btn-danger btn-sm" [disabled]="currentAccount.login === user.login">
<span class="fa fa-remove"></span>
<span class="d-none d-md-inline" jhiTranslate="entity.action.delete">Delete</span>
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div *ngIf="users">
<div class="row justify-content-center">
<jhi-item-count [page]="page" [total]="queryCount" [itemsPerPage]="itemsPerPage"></jhi-item-count>
</div>
<div class="row justify-content-center">
<ngb-pagination [collectionSize]="totalItems" [(page)]="page" [pageSize]="itemsPerPage" [maxSize]="5" [rotate]="true" [boundaryLinks]="true" (pageChange)="loadPage(page)"></ngb-pagination>
</div>
</div>
</div>