不同方式创建的JavaWeb项目之间差异
IDEA版本:2023.1.5
文章目录
常见的创建JavaWeb的两种方式:
- Maven模板原型(webapp)
- Web Application
Maven模板原型(webapp)方式创建的JavaWeb项目
创建方式
项目结构
web资源的根目录是在/src/main/webapp。
运行后输出目录
Web Application方式创建的JavaWeb项目
创建方式
先创建一个普通Java项目,再添加Web Application框架支持。
项目结构
web资源根目为/web。
运行后输出目录
在WEB-INF目录下的JSP文件在输出目录中仍在WEB-INF下。
JavaWeb项目运行后输出目录中不存在web资源文件问题
Web Application方式创建的JavaWeb项目配置信息存放在iml中,然后在modules.xml中指定iml文件位置。同时还比webapp方式创建的web项目多一个artifacts,里面存放的是ArtifactManager的配置。这些是在第一次运行项目后生成的,刚创建的项目是没有的。这些文件项目运行生成输出目录中的内容是根据这些进行配置的。webapp方式创建的JavaWeb项目不再需要额外配置的项目信息存储在.idea目录下的.xml文件中,而是通过读取pom.xml文件中的信息来进行配置。当两者之间配置方式杂糅就会导致项目运行后输出目录中不存在web资源文件。在IDEA(新版)中,使用Maven创建多模块项目时,当子模块项目使用Web Application方式创建Web项目,运行后会导致运行后输出目录不存在web资源文件,报404。
Web Application
webapplication-test.iml
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/web" relative="/" />
</webroots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
modules.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/webapplication-test.iml" filepath="$PROJECT_DIR$/webapplication-test.iml" />
</modules>
</component>
</project>
webapplication_test_war_exploded.xml
<component name="ArtifactManager">
<artifact type="exploded-war" name="webapplication-test:war exploded">
<output-path>$PROJECT_DIR$/out/artifacts/webapplication_test_war_exploded</output-path>
<root id="root">
<element id="javaee-facet-resources" facet="webapplication-test/web/Web" />
<element id="directory" name="WEB-INF">
<element id="directory" name="classes">
<element id="module-output" name="webapplication-test" />
</element>
</element>
</root>
</artifact>
</component>
webapp
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>webapp-test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>webapp-test Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>webapp-test</finalName>
</build>
</project>