首先,來看下MAVENx項目標準的目錄結構:html
通常狀況下,咱們用到的資源文件(各類xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包時,maven能把這些資源文件打包到相應的jar或者war裏。java
有時候,好比mybatis的mapper.xml文件,咱們習慣把它和Mapper.java放一塊兒,都在src/main/java下面,這樣利用maven打包時,就須要修改pom.xml文件,來把mapper.xml文件一塊兒打包進jar或者war裏了,不然,這些文件不會被打包的。(maven認爲src/main/java只是java的源代碼路徑)。網絡上有不少方法,我大概試了下,幾種方法均可以,能夠任選一種便可。web
方法1,其中**/*這樣的寫法,是爲了保證各級子目錄下的資源文件被打包。spring
Xml代碼 apache
<build> <finalName>test</finalName> <!-- 這樣也能夠把全部的xml文件,打包到相應位置。 <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
方法2,利用build-helper-maven-plugin插件api
Xml代碼 tomcat
<build> ... </plugins> ... <!-- 此plugin能夠用 利用此plugin,把源代碼中的xml文件, 打包到相應位置,這裏主要是爲了打包Mybatis的mapper.xml文件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.8</version> <executions> <execution> <id>add-resource</id> <phase>generate-resources</phase> <goals> <goal>add-resource</goal> </goals> <configuration> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> ... </plugins> ... </build>
方法3,利用maven-resources-plugin插件springboot
Xml代碼 網絡
<build> ... </plugins> ... <!-- 此plugin能夠用 利用此plugin,把源代碼中的xml文件,打包到相應位置, 這裏主要是爲了打包Mybatis的mapper.xml文件 --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-xmls</id> <phase>process-sources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/classes</outputDirectory> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> ... </plugins> ... </build>
例如:使用IDEA前臺使用JSP頁面展示視圖,須要進行以下配置,不然會提示404,找不到頁面mybatis
POM文件:
<!-- 引入springboot 內嵌的Tomcat對JSP的解析包 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- servlet依賴的jar包start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- servlet依賴的jar包end --> <!-- jsp依賴的jar包start --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <!-- jsp依賴的jar包end --> <!-- jstl標籤依賴的jar包start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- jstl標籤依賴的jar包end -->
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!-- 使用IDEA開發JSP,爲了防止找不到頁面,出現404錯誤,必須加入下面配置 --> <!-- maven打包配置resources: --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> <!-- 這個表示把src/main/webapp下面全部文件(**/*.*)都編譯到 META-INF/resources目錄下--> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build>
application.properties:
#前段視圖採用JSP,配置jsp先後綴 spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp
Controller:
@Controller public class JspController { @RequestMapping("/boot/index") public String index(Model model){ model.addAttribute("msg","spring boot 集成 JSP"); return "index"; } }
JSP頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>INDEX</title> </head> <body> <div> ${msg} </div> </body> </html>
效果: