Eclipse下Spring6項目環(huán)境配置
1、環(huán)境準(zhǔn)備(OS:win11)
JDK
版本(java --version):
java 17.0.5 2022-10-18 LTS
Java(TM) SE Runtime Environment (build 17.0.5+9-LTS-191)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.5+9-LTS-191, mixed mode, sharing)
安裝:oracle或openjdk官網(wǎng)下載后直接安裝即可
配置:
---JAVA_HOME:設(shè)置成Java安裝目錄
---PATH路徑把java安裝目錄下bin的路徑加進(jìn)去(生效需重啟)
Apache Tomcat
版本:10.11
官網(wǎng):https://tomcat.apache.org/
配置:默認(rèn)即可
Apache Maven
版本:10.11
官網(wǎng):https://maven.apache.org/
配置:參考https://maven.apache.org/configure.html,修改安裝目錄\conf\settings.xml文件
先備份!!!,然后在mirrors標(biāo)簽內(nèi)加入
<mirror>
? ? ? ? <id>alimaven</id>
? ? ? ? <name>aliyun maven</name>
? ? ? ? <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
? ? ? ? <mirrorOf>central</mirrorOf>
? ? </mirror>
? ? <mirror>
? ? ? ? <id>central</id>
? ? ? ? <name>Maven Repository Switchboard</name>
? ? ? ? <url>http://repo1.maven.org/maven2/</url>
? ? ? ? <mirrorOf>central</mirrorOf>
? ? </mirror>
? ? <mirror>
? ? ? ? <id>repo2</id>
? ? ? ? <mirrorOf>central</mirrorOf>
? ? ? ? <name>Human Readable Name for this Mirror.</name>
? ? ? ? <url>http://repo2.maven.org/maven2/</url>
? ? </mirror>
? ? <mirror>
? ? ? ? <id>ibiblio</id>
? ? ? ? <mirrorOf>central</mirrorOf>
? ? ? ? <name>Human Readable Name for this Mirror.</name>
? ? ? ? <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
? ? </mirror>
? ? <mirror>
? ? ? ? <id>jboss-public-repository-group</id>
? ? ? ? <mirrorOf>central</mirrorOf>
? ? ? ? <name>JBoss Public Repository Group</name>
? ? ? ? <url>http://repository.jboss.org/nexus/content/groups/public</url>
? ? </mirror>
? ? <!-- 中央倉庫在中國的鏡像 -->
? ? <mirror>
? ? ? ? <id>maven.net.cn</id>
? ? ? ? <name>oneof the central mirrors in china</name>
? ? ? ? <url>http://maven.net.cn/content/groups/public/</url>
? ? ? ? <mirrorOf>central</mirrorOf>
? ? </mirror>
---path環(huán)境變量中加入maven安裝路徑\bin(生效需重啟)
---驗證生效:mvn -v
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: D:\apache-maven-3.9.4
Java version: 17.0.5, vendor: Oracle Corporation, runtime: D:\Java\jdk-17.0.5
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"
?eclipse
-版本202306
- https://www.eclipse.org/downloads/
- 配置:window->preferences->server->runtime enviroment把server設(shè)置成tomcat對應(yīng)版本;window->preferences->maven->installations,改成本地安裝的版本
2、創(chuàng)建項目
eclipse新建maven項目,catelog選擇internal,archtype artifact ID選擇maven-archtype-webapp。填寫group ID,artifact id,finish完成,注意中間在console選項卡里要敲個Y才能繼續(xù)。
項目創(chuàng)建后默認(rèn)webapp下會有一個jsp文件,此文件會導(dǎo)致eclipse一直報錯。查了半天沒解決,本身也不用JSP,就直接刪掉了。
在web-app下新建一個index.html文件,如果在服務(wù)器上能正常訪問就證明項目本身沒有問題。
3、導(dǎo)入依賴(項目的pom文件)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
? ? <groupId>org.springframework</groupId>
? ? <artifactId>spring-core</artifactId>
? ? <version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
? ? <groupId>org.springframework</groupId>
? ? <artifactId>spring-aop</artifactId>
? ? <version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
? ? <groupId>org.springframework</groupId>
? ? <artifactId>spring-context</artifactId>
? ? <version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
? ? <groupId>org.springframework</groupId>
? ? <artifactId>spring-web</artifactId>
? ? <version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
? ? <groupId>org.springframework</groupId>
? ? <artifactId>spring-beans</artifactId>
? ? <version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
? ? <groupId>org.springframework</groupId>
? ? <artifactId>spring-webmvc</artifactId>
? ? <version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring6 -->
<dependency>
? ? <groupId>org.thymeleaf</groupId>
? ? <artifactId>thymeleaf-spring6</artifactId>
? ? <version>3.1.2.RELEASE</version>
</dependency>
<!--log4j2的依賴-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.20.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web 解決日志里L(fēng)og4j2-TF-2-Scheduled-1警告問題 -->
<dependency>
? ? <groupId>org.apache.logging.log4j</groupId>
? ? <artifactId>log4j-web</artifactId>
? ? <version>2.20.0</version>
</dependency>
4、修改配置文件web.xml將spring作為隨tomcat啟動的servlet注冊
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-basic.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5、創(chuàng)建spring配置文件(/cmspring/src/main/resources/spring.xml)
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
? ? ? ?http://www.springframework.org/schema/beans/spring-beans.xsd
? ? ? ?http://www.springframework.org/schema/context
? ? ? ?https://www.springframework.org/schema/context/spring-context.xsd?
? ? ? ?http://www.springframework.org/schema/aop
? ? ? ?https://www.springframework.org/schema/context/spring-aop.xsd?
? ? ? ?http://www.springframework.org/schema/mvc?
? ? ? ?https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--開啟組件掃描 -->
<context:component-scan
base-package="org.htq.test" />
<!-- 配置 Thymeleaf 視圖解析器 -->
? ?<!-- SpringResourceTemplateResolver automatically integrates with Spring's own -->
? ? <!-- resource resolution infrastructure, which is highly recommended.? ? ? ? ? -->
? ? <bean id="templateResolver"
? ? ? ? ? class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver">
? ? ? ? <property name="prefix" value="/WEB-INF/templates/" />
? ? ? ? <property name="suffix" value=".html" />
? ? ? ? <!-- HTML is the default value, added here for the sake of clarity.? ? ? ? ? -->
? ? ? ? <property name="templateMode" value="HTML" />
? ? ? ? <!-- Template cache is true by default. Set to false if you want? ? ? ? ? ? ?-->
? ? ? ? <!-- templates to be automatically updated when modified.? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <property name="cacheable" value="true" />
?
? ? ? ? <property name="characterEncoding" value="UTF-8" />
? ? </bean>
?
? ? <!-- SpringTemplateEngine automatically applies SpringStandardDialect and? ? ? -->
? ? <!-- enables Spring's own MessageSource message resolution mechanisms.? ? ? ? ?-->
? ? <bean id="templateEngine"
? ? ? ? ? class="org.thymeleaf.spring6.SpringTemplateEngine">
? ? ? ? <property name="templateResolver" ref="templateResolver" />
? ? ? ? <!-- Enabling the SpringEL compiler can speed up execution in most? ? ? ? ? ?-->
? ? ? ? <!-- scenarios, but might be incompatible with specific cases when? ? ? ? ? ?-->
? ? ? ? <!-- expressions in one template are reused across different data types, so? -->
? ? ? ? <!-- this flag is "false" by default for safer backwards compatibility.? ? ? -->
? ? ? ? <property name="enableSpringELCompiler" value="true" />
? ? </bean>
?
? ? <bean class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
? ? ? ? <property name="templateEngine" ref="templateEngine" />
? ? ? ? <!-- NOTE 'order' and 'viewNames' are optional -->
? ? ? ? <property name="order" value="1" />
<!--? ? ? ? <property name="viewNames" value="*.html,*.xhtml" />-->
? ? ? ? <property name="characterEncoding" value="UTF-8"/>
? ? </bean>
</beans>
6.log4j2(/cmspring/src/main/resources/log4j2.xml)便于開發(fā)調(diào)試
在resource下面創(chuàng)建log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
?<!--日志級別以及優(yōu)先級排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
?<!--Configuration后面的status,這個用于設(shè)置log4j2自身內(nèi)部的信息輸出,可以不設(shè)置,當(dāng)設(shè)置成trace時,你會看到log4j2內(nèi)部各種詳細(xì)輸出-->
?<!--monitorInterval:Log4j能夠自動檢測修改配置 文件和重新配置本身,設(shè)置間隔秒數(shù)-->
?<configuration status="WARN" monitorInterval="30">
? ? ?<!--先定義所有的appender-->
? ? ?<appenders>
? ? ?<!--這個輸出控制臺的配置-->
? ? ? ? ?<console name="Console" target="SYSTEM_OUT">
? ? ? ? ?<!--輸出日志的格式-->
? ? ? ? ? ? ?<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
? ? ? ? ?</console>
? ? ?</appenders>
? ? ?<!--然后定義logger,只有定義了logger并引入的appender,appender才會生效-->
? ? ?<loggers>
? ? ? ? ?<!--過濾掉spring和mybatis的一些無用的DEBUG信息-->
? ? ? ? ?<logger name="org.springframework" level="INFO"></logger>
? ? ? ? <root level="all">
? ? ? ? ? ? ?<appender-ref ref="Console"/>
? ? ? ? ? </root>
? ? ?</loggers>
?</configuration>
7.thymeleaf模板文件
(1)模板目錄默認(rèn)是在項目的/cmspring/src/main/webapp/WEB-INF/templates文件夾下
(2)需要在spring配置文件里加上如下配置:
<!-- 配置 Thymeleaf 視圖解析器 -->
? ?<!-- SpringResourceTemplateResolver automatically integrates with Spring's own -->
? ? <!-- resource resolution infrastructure, which is highly recommended.? ? ? ? ? -->
? ? <bean id="templateResolver"
? ? ? ? ? class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver">
? ? ? ? <property name="prefix" value="/WEB-INF/templates/" />
? ? ? ? <property name="suffix" value=".html" />
? ? ? ? <!-- HTML is the default value, added here for the sake of clarity.? ? ? ? ? -->
? ? ? ? <property name="templateMode" value="HTML" />
? ? ? ? <!-- Template cache is true by default. Set to false if you want? ? ? ? ? ? ?-->
? ? ? ? <!-- templates to be automatically updated when modified.? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <property name="cacheable" value="true" />
?
? ? ? ? <property name="characterEncoding" value="UTF-8" />
? ? </bean>
?
? ? <!-- SpringTemplateEngine automatically applies SpringStandardDialect and? ? ? -->
? ? <!-- enables Spring's own MessageSource message resolution mechanisms.? ? ? ? ?-->
? ? <bean id="templateEngine"
? ? ? ? ? class="org.thymeleaf.spring6.SpringTemplateEngine">
? ? ? ? <property name="templateResolver" ref="templateResolver" />
? ? ? ? <!-- Enabling the SpringEL compiler can speed up execution in most? ? ? ? ? ?-->
? ? ? ? <!-- scenarios, but might be incompatible with specific cases when? ? ? ? ? ?-->
? ? ? ? <!-- expressions in one template are reused across different data types, so? -->
? ? ? ? <!-- this flag is "false" by default for safer backwards compatibility.? ? ? -->
? ? ? ? <property name="enableSpringELCompiler" value="true" />
? ? </bean>
?
? ? <bean class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
? ? ? ? <property name="templateEngine" ref="templateEngine" />
? ? ? ? <!-- NOTE 'order' and 'viewNames' are optional -->
? ? ? ? <property name="order" value="1" />
<!--? ? ? ? <property name="viewNames" value="*.html,*.xhtml" />-->
? ? ? ? <property name="characterEncoding" value="UTF-8"/>
? ? </bean>
8.程序(/cmspring/src/main/java/org/htq/controller/Hello.java)
package org.htq.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class Hello {
@RequestMapping("/")
public String hello()
{
return "hello";
}
}
創(chuàng)建/cmspring/src/main/webapp/WEB-INF/templates/hello.html
訪問http://localhost:8080/你的項目名稱/hello/,如果能正常顯示就表示環(huán)境配置成功了。