1、背景
根據官網一步步地生成項目,他喵的啟動后居然是一個空白頁面,這怎么玩啊?還有這種操作的嗎?跟說好的不一樣啊!關於JHipster資料,國內少的可憐,幾乎都是同一樣的東西,純介紹的文章,只好上stackoverflow上查了。
2、相似的情況
在stackoverflow找到了一個相似的提問,不過沒有具體的解決方法,倒是里面的某個評論給了我很大的提示。
By default, yarn listens on port 9000 (webpack + browser sync) to hot reload frontend code and contents, maven or gradle listens on port 8080 to hot reload server code. You must run both in dev.
在開發階段,通常使用yarn start來啟動angular2,可以在開發過程中熱加載修改后的代碼,然后通過./mvnw來啟動Spring Boot后端的api服務。
那么問題來了,我以后要上線的時候也要這樣啟動啊?(╯‵□′)╯︵┻━┻
3、解決過程
由於前端是angular2,雖然可以熱加載方便開發者開發,但是上線也不會這樣做的。
通過了解angular2,可以通過ng build打包好,然后可以直接訪問主頁。
我在項目的目錄下執行ng build后,打包好的代碼自動生成到target的www上面。
再次啟動項目,還是空白頁面,回到原點。
打開瀏覽器的F12的network,居然只有一個localhost???
不對啊!為什么只會有一個localhost呢?其他資源沒有加載嗎?有貓膩!
打開生成項目的代碼,找到配置web資源的WebConfigurer,通過查看代碼,我們可以看到:
public void customize(ConfigurableEmbeddedServletContainer container) {
MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
// IE issue, see https://github.com/jhipster/generator-jhipster/pull/711
mappings.add("html", "text/html;charset=utf-8");
// CloudFoundry issue, see https://github.com/cloudfoundry/gorouter/issues/64
mappings.add("json", "text/html;charset=utf-8");
container.setMimeMappings(mappings);
// When running in an IDE or with ./mvnw spring-boot:run, set location of the static web assets.
setLocationForStaticAssets(container);
/*
* Enable HTTP/2 for Undertow - https://twitter.com/ankinson/status/829256167700492288
* HTTP/2 requires HTTPS, so HTTP requests will fallback to HTTP/1.1.
* See the JHipsterProperties class and your application-*.yml configuration files
* for more information.
*/
if (jHipsterProperties.getHttp().getVersion().equals(JHipsterProperties.Http.Version.V_2_0) &&
container instanceof UndertowEmbeddedServletContainerFactory) {
((UndertowEmbeddedServletContainerFactory) container)
.addBuilderCustomizers(builder ->
builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true));
}
}
其中,setLocationForStaticAssets(container);就是設置網站的靜態資源的位置。眼看代碼是沒問題的,唯有debug救我。debug下面的代碼:
private void setLocationForStaticAssets(ConfigurableEmbeddedServletContainer container) {
File root;
String prefixPath = resolvePathPrefix();
root = new File(prefixPath + "target/www/");
if (root.exists() && root.isDirectory()) {
container.setDocumentRoot(root);
}
}
通過debug發現,root的路徑多了%20的字符導致找不到路徑,%20就是空格,將空格去掉再試一試~
果然是這個問題,去掉空格之后就有內容了。
4、結案
看看目錄的路徑是否帶有空格或者中文字符,有的話去掉試試看。
