?目錄
原始方式
使用Maven
一、原始方式
- 正常創(chuàng)建Java項(xiàng)目即可
- 創(chuàng)建完成之后的目錄結(jié)構(gòu)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
- 右擊鼠標(biāo)項(xiàng)目名稱,點(diǎn)擊“Add Framework Support”
- 勾選上Web Application 以及對應(yīng)的Create web.xml,點(diǎn)擊OK
- 新的目錄結(jié)構(gòu)多了web這個(gè)文件夾? ? ? ? ? ? ? ? ? ??
- 右擊WEB-INF,新建兩個(gè)Directory,名稱分別為:classes和lib
- 點(diǎn)擊導(dǎo)航欄的File,打開"Project Structure"
- Modules->項(xiàng)目“TestJavaWeb”->Paths->Use module compile output path->文件夾圖標(biāo)點(diǎn)開,選擇剛剛創(chuàng)建的classes文件夾->點(diǎn)擊Apply
- classes就會變橙色? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
-
再點(diǎn)擊Dependencies -> 點(diǎn)擊+號 -> 選擇第一個(gè)“JARs or directories..." -> 選擇剛剛創(chuàng)建的lib文件夾,點(diǎn)擊OK
- 選擇 "Jar Directory" -> 點(diǎn)擊OK? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
- 勾選上剛剛的文件夾 -> 點(diǎn)擊OK? ? ? ? ? ?
- 點(diǎn)擊右上角的"Add Configuration..." 進(jìn)行Tomcat部署
- ?點(diǎn)擊+號 -> 點(diǎn)擊添加 Tomcat Server Local? ? ? ? ? ? ? ? ? ? ? ? ?
- Configure... -> 點(diǎn)擊文件夾圖標(biāo),選擇自己下載的Tomcat文件即可
- Deployment -> 點(diǎn)擊+號 -> 點(diǎn)擊Artifact
- ? 刪除多余的后綴“_war_exploded"? ? ? ?
- ?點(diǎn)擊綠色三角進(jìn)行測試,瀏覽器中顯示jsp文件中body標(biāo)簽中的內(nèi)容就表示配置成功
二、Maven(前提已經(jīng)下載并配置好了Maven)
- Maven -> 勾選"Create from archetype" -> 點(diǎn)擊"........webapp" -> Next
- 配置好自己的Maven -> 點(diǎn)擊 Finish
- 會開始下載大量文件,出現(xiàn)"BUILD SUCCESS"即可
- 此時(shí)的目錄結(jié)構(gòu),main中并沒有resource和lib文件夾
- 鼠標(biāo)右點(diǎn)main,新建Directory,會出現(xiàn)紅色框中的內(nèi)容,依次將其新建出即可(如果目錄中已經(jīng)有這兩個(gè)文件夾就跳過這一步)
- 配置Tomcat的步驟跟原始方法一樣,只不過在Deployment中點(diǎn)擊加號后出現(xiàn)以上彈窗時(shí),選擇有"exploded"后綴的那一項(xiàng),點(diǎn)擊OK? ? ? ? ? ? ??
-
同樣,還要去掉“_war_exploded"后綴,點(diǎn)擊OK
-
在pom.xml文件中添加紅框中的代碼?(代碼附在最下面
-
點(diǎn)擊藍(lán)色加載圖標(biāo),如果沒有這個(gè)圖標(biāo),就右擊pom.xml文件,reload一下就行? ?? ? ? ? ? ? ?
-
使用spring框架,記得導(dǎo)包,然后編輯spring xml文件還有web.xml文件(代碼附下面
-
結(jié)構(gòu)目錄? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?
-
點(diǎn)擊右上角運(yùn)行,出現(xiàn)jsp中的內(nèi)容即可? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
附:
pom.xml
第一行放在properties標(biāo)簽中:
<spring.version>5.2.15.RELEASE</spring.version>
這些放dependencies標(biāo)簽中:?文章來源:http://www.zghlxwxcb.cn/news/detail-808894.html
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
spring-mvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
如果運(yùn)行不出來,改一下web.xml文件頭:文章來源地址http://www.zghlxwxcb.cn/news/detail-808894.html
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
到了這里,關(guān)于IDEA的JavaWeb項(xiàng)目配置+Tomcat的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!