SpringBootは、エンタープライズアプリケーション開発のためにSpringDataJpa、Shiro、Redisを統合します(2)

プロジェクトの前提:

前回の記事では、データソースとアプリケーションの起動ページを正常に接続しました。プロジェクトにページを追加して試してみましょう。

1:Web側のpomファイル(zh-web)にページレンダリングエンジンの依存関係を追加します。現在、freemarkerを使用しています。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

その後、web(zh-web)のapplication.ymlでfreemarkerの関連する構成ファイルを構成します。具体的な内容は次のとおりです。

spring:
  freemarker:
    suffix: .html
    templateEncoding: UTF-8
    templateLoaderPath: classpath:/templates/  #需要在web端(zh-web)的resource下建立templates文件夹
    content-type: text/html

次に、ルーティング構成ファイルをWeb側(zh-web)側に書き込みます。

ファイルの内容を図に示します。

package com.zh.controller.common;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * created with IntelliJ IDEA
 *
 * @author: create by limu
 * Date: 2020/11/13
 * Time:15:27
 */
@Controller
public class MappingController {

    /**
     * 首页测试页面
     *
     * @return 解析地址
     */
    @GetMapping("index")
    public String index() {
        return "index";
    }

}

次に、開始して、開始後に127.0.0.1:8080 / indexにアクセスすると、結果は次のようになります。

アクセスに成功しました。ブラウザコンソールを確認すると、jsファイルのアドレスを参照しても問題ないことがわかりますが、ブラウザをロードするとステータスが404になりますが見つかりません。

このとき、SpringBootの静的ファイルが静的フォルダーを無視することを知っておく必要があります。静的リソースアドレスを次のように変更できます。

<script src="/js/jquery.js"></script>

ただし、このアイデアは参照できず、常に黄色の警告が報告され、jsファイルを表示するのは不便です。フロントエンドの静的ページを送信するのは一般的に不便です。このように、静的リソースアドレスを次のように構成できます。

共通モジュール(zh-common)に構成クラスを追加します。ファイルの内容は次のとおりです。

package com.zh.config.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * created with IntelliJ IDEA
 *
 * @author: create by limu
 * Date: 2020/11/13
 * Time:16:02
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

この後、アドレスをフロントエンドから指定されたアドレスに変更し、再起動して再試行します。ファイルが正常にロードされていることがわかります。

次の記事では、マルチターミナルログイン機能を紹介します

 

おすすめ

転載: blog.csdn.net/qq_38821574/article/details/109675219