Springboot 多模塊 jsp

項目結構

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

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:web

<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熱部署;

關於做者

相關文章
相關標籤/搜索