maven插件spring-boot-starter-tomcat的使用方式
tomcat內嵌到web項目中
將tomcat 內嵌到 web項目中,這樣可以直接運行 webapp項目。
不需要再部署到額外的tomcat,直接就可以運行了。
1.pom.xml 配置
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.3.0.RELEASE</version> </plugin> </plugins>
2.tomcat使用maven內嵌入到web項目需要jdk運行環(huán)境
eclipse默認的運行環(huán)境式jre,我們需要改成jdk
問題解決:
3.springmvc依賴
Spring V4.1以后的版本運行時會報如下錯誤:
NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()
我們使用Spring V4.1以前的版本運行,依賴如下
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.1.RELEASE</version> </dependency>
導入對象轉json的依賴
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency>
在pom.xml中配置jdk版本
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
4.編寫表現層代碼
@RestController public class UserController { @RequestMapping("query") public Map<String, Object> query(){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("code", 0); return map; } }
5.配置web.xml
<?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" id="WebApp_ID" version="3.1"> <display-name>TomcatDemo</display-name> <!-- 編碼級過濾器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC的核心控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>ContextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
6.SpringMVC的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 掃描具有注解的類 --> <context:component-scan base-package="com.wn"></context:component-scan> <!-- 使SpringMVC的注解生效 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置靜態(tài)資源過濾器 --> <mvc:resources location="/" mapping="/**"></mvc:resources> </beans>
7.配置完成后在項目上鼠標右鍵
Run As->Maven build->輸入tomcat啟動命令啟動tomcat
tomcat:run
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mybatis的mapper.xml中if標簽test判斷的用法說明
這篇文章主要介紹了Mybatis的mapper.xml中if標簽test判斷的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06換了最新的idea如何將原來舊版本的idea設置導進新的idea中
這篇文章主要介紹了換了最新的idea如何將原來舊版本的idea設置導進新的idea中,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11