1 在 Eclipse 中創建新的web項目【ABC】,以後 轉成Maven項目。html
2 建立 class Applicationjava
3 修改POMweb
4 修改web.xmlspring
5 export 成 WAR 到Tomcat 的 workapps裏apache
6 運行tomcat, 並在瀏覽器中查看。bootstrap
Application.class瀏覽器
package demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Configuration @ComponentScan @EnableAutoConfiguration public class Application extends SpringBootServletInitializer { private static Class<Application> applicationClass = Application.class; @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(applicationClass); } public static void main(String[] args) { SpringApplication.run(applicationClass, args); } } @RestController class GreetingController { @RequestMapping("/hello/{name}") String hello(@PathVariable String name) { return "Hello, " + name + "!"; } }
pom.xmltomcat
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-web-thymeleaf</artifactId> <packaging>war</packaging> <name>Spring Boot Web Thymeleaf Example</name> <description>Spring Boot Web Thymeleaf Example</description> <url>https://www.mkyong.com</url> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- hot swapping, disable cache for template, enable live reload --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- Optional, for bootstrap --> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-legacy</artifactId> <version>1.1.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
web.xmlapp
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>ABC</display-name> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>demo.Application</param-value> </context-param> <listener> <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextAttribute</param-name> <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
查看: http://localhost:8080/ABC/hello/s jsp
輸出 : Hello, s!
參考url: https://www.cnblogs.com/weixliu/p/6432342.html
備註:
spring-boot-starter-parent [Maven的用戶能夠經過繼承spring-boot-starter-parent項目來得到一些合理的默認配置]spring-boot-legacy [支持 舊的遺留的 ]spring-boot-starter-thymeleaf [parse html]spring-boot-maven-plugin [Java -jar命令來啓動jar包,打的包裏面纔會有maven依賴的jar包和spring boot的啓動類]maven-surefire-plugin [surefire 插件用來在maven構建生命週期的test phase執行一個應用的單元測試]org.webjars [WebJars能使Maven的依賴管理支持OSS的JavaScript庫/CSS庫,好比jQuery、Bootstrap等]spring-boot-devtools [spring-boot-devtools模塊能夠包含在任何項目中,它能夠節省大量的時間] https://blog.csdn.net/isea533/article/details/70495714 commons-logging [通用日誌]