1

项目结构

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

关于作者


花田土著
190 声望34 粉丝

希望加入有意思的开发团队!