Springboot 多模組 jsp

花田土著發表於2018-01-30

專案結構

module1(如:封裝好的一套組織機構、許可權、角色、使用者管理模組)包含jsp頁面,module2(具體業務應用場景開發)依賴module1,結構圖如下:

parent
│       
└── module1
│   │── ...
│   └── src/main/resources
│       │── mapper
│       │── public
│       │   ...
│       src/main/webapp
│       └── WEB-INF
│            └── jsp
│
└── module2
    │   ...

解決方案

  • module1和module2的packaging均為jar。
  • src/main/resources下靜態資源,及src/main/webapp/WEB-INF/jsp通過==resources==打包到META-INF目錄下。這裡的META-INF指打成jar後,jar裡的META-INF。值得注意的是,如果您的mapper位於src/main/resources下,也需要打包出來哦。

module1`s pom.xml的build標籤內中新增resources:

<resources>
    <!-- 打包時將jsp檔案拷貝到META-INF目錄下-->
    <resource>
        <!-- 指定resources外掛處理哪個目錄下的資原始檔 -->
        <directory>src/main/webapp</directory>
        <!-- 注意必須要放在此目錄下才能被訪問到-->
        <targetPath>META-INF/resources</targetPath>
        <includes>
            <include>**/**</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources/public</directory>
        <targetPath>META-INF/resources</targetPath>
        <includes>
            <include>**/**</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/**</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
  • 普通專案直接在application.yml中配置即可,多模組專案無效。採用WebConfig.java:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
    }
    /**
     * 多模組的jsp訪問,預設是src/main/webapp,但是多模組的目錄只設定yml檔案不行
     * @return
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        // jsp目錄
        resolver.setPrefix("/WEB-INF/jsp/");
        // 字尾
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

展望

  • 是否支援module1熱部署;

關於作者

相關文章