IDEA创建基于Maven的web项目

柔光的暖阳◎ 2023-10-08 11:27 152阅读 0赞

1、先创建一个基本的Maven项目。基础的maven项目结构如下:
在这里插入图片描述
2、File-》Project Structure
在这里插入图片描述
3、新增Web模块
在这里插入图片描述
4、create Artifact
在这里插入图片描述
5、OK
在这里插入图片描述
6、自动生成web目录,新项目结构
在这里插入图片描述
7、Run -》Edit Configurations
在这里插入图片描述
8、Add -》Tomcat Server -》Local
在这里插入图片描述
9、设置Tomcat路径
在这里插入图片描述
10、添加部署项目
在这里插入图片描述
11、配置项目根路径
在这里插入图片描述
12、添加spring依赖,配置log4j,最终项目结构
在这里插入图片描述
12、运行项目,访问项目url
在这里插入图片描述


发生异常:ClassNotFoundException…
解决:手动“Put into Output Root”(貌似每次添加maven依赖都要操作一下)
在这里插入图片描述


相关配置文件

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.markix</groupId>
  7. <artifactId>web</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <spring.version>4.3.25.RELEASE</spring.version>
  11. <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
  12. <logback.version>1.2.3</logback.version>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>log4j</groupId>
  17. <artifactId>log4j</artifactId>
  18. <version>1.2.17</version>
  19. </dependency>
  20. <!-- Spring -->
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-core</artifactId>
  24. <version>${spring.version}</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-beans</artifactId>
  29. <version>${spring.version}</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-context</artifactId>
  34. <version>${spring.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-context-support</artifactId>
  39. <version>${spring.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-expression</artifactId>
  44. <version>${spring.version}</version>
  45. </dependency>
  46. <!-- Spring AOP -->
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-aop</artifactId>
  50. <version>${spring.version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-aspects</artifactId>
  55. <version>${spring.version}</version>
  56. </dependency>
  57. <!-- Spring Test -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-test</artifactId>
  61. <version>${spring.version}</version>
  62. <scope>test</scope>
  63. </dependency>
  64. <!-- Spring Web -->
  65. <dependency>
  66. <groupId>org.springframework</groupId>
  67. <artifactId>spring-web</artifactId>
  68. <version>${spring.version}</version>
  69. </dependency>
  70. <dependency>
  71. <groupId>org.springframework</groupId>
  72. <artifactId>spring-webmvc</artifactId>
  73. <version>${spring.version}</version>
  74. </dependency>
  75. <!-- Spring ORM -->
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-jdbc</artifactId>
  79. <version>${spring.version}</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.springframework</groupId>
  83. <artifactId>spring-orm</artifactId>
  84. <version>${spring.version}</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.springframework</groupId>
  88. <artifactId>spring-tx</artifactId>
  89. <version>${spring.version}</version>
  90. </dependency>
  91. <!-- servlet api -->
  92. <dependency>
  93. <groupId>javax.servlet</groupId>
  94. <artifactId>javax.servlet-api</artifactId>
  95. <version>${javax.servlet-api.version}</version>
  96. <scope>provided</scope>
  97. </dependency>
  98. </dependencies>
  99. <build>
  100. <plugins>
  101. <!-- 资源文件拷贝插件 -->
  102. <plugin>
  103. <groupId>org.apache.maven.plugins</groupId>
  104. <artifactId>maven-resources-plugin</artifactId>
  105. <version>2.7</version>
  106. <configuration>
  107. <encoding>UTF-8</encoding>
  108. </configuration>
  109. </plugin>
  110. <!-- java编译插件 -->
  111. <plugin>
  112. <groupId>org.apache.maven.plugins</groupId>
  113. <artifactId>maven-compiler-plugin</artifactId>
  114. <version>3.5.1</version>
  115. <configuration>
  116. <encoding>UTF-8</encoding>
  117. </configuration>
  118. </plugin>
  119. </plugins>
  120. </build>
  121. </project>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--<web-app version="4.0"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  6. >-->
  7. <web-app version="3.0"
  8. xmlns="http://java.sun.com/xml/ns/javaee"
  9. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  11. >
  12. <!--<web-app version="2.5"
  13. xmlns="http://java.sun.com/xml/ns/javaee"
  14. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  16. >-->
  17. <display-name>web-project-demo</display-name>
  18. <description>Demo项目</description>
  19. <!-- 配置监听器:加载spring的ioc容器(加载spring配置文件) -->
  20. <listener>
  21. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  22. </listener>
  23. <context-param>
  24. <param-name>contextConfigLocation</param-name>
  25. <param-value>
  26. classpath:spring/spring.xml
  27. </param-value>
  28. </context-param>
  29. <!-- 配置spring前端控制器 -->
  30. <servlet>
  31. <servlet-name>dispatcherServlet</servlet-name>
  32. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  33. <init-param>
  34. <param-name>contextConfigLocation</param-name>
  35. <param-value>classpath:spring/springmvc.xml</param-value>
  36. </init-param>
  37. <load-on-startup>1</load-on-startup>
  38. </servlet>
  39. <servlet-mapping>
  40. <servlet-name>dispatcherServlet</servlet-name>
  41. <url-pattern>/</url-pattern>
  42. </servlet-mapping>
  43. <!-- 配置拦截器,解决中文编码问题 -->
  44. <filter>
  45. <filter-name>characterEncodingFilter</filter-name>
  46. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  47. <init-param>
  48. <param-name>encoding</param-name>
  49. <param-value>UTF-8</param-value>
  50. </init-param>
  51. </filter>
  52. <filter-mapping>
  53. <filter-name>characterEncodingFilter</filter-name>
  54. <url-pattern>/*</url-pattern>
  55. </filter-mapping>
  56. <welcome-file-list>
  57. <welcome-file>index.html</welcome-file>
  58. </welcome-file-list>
  59. </web-app>

springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  9. <!-- SpringMVC 仅扫描 Controller组件 -->
  10. <context:component-scan base-package="com.markix.controller" use-default-filters="false">
  11. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  12. </context:component-scan>
  13. <!-- 自动配置请求处理映射器和请求处理适配器 -->
  14. <!-- RequestMappingHandlerMapping / RequestMappingHandlerAdapter -->
  15. <mvc:annotation-driven/>
  16. <!-- 视图解析器 -->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <!-- 配置返回页面路径前缀prefix、后缀suffix
  19. Controller类中返回的视图路径前缀/后缀可省略-->
  20. <property name="prefix" value="/"></property>
  21. <property name="suffix" value=""></property>
  22. </bean>
  23. </beans>

spring.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  7. <context:component-scan base-package="com.markix">
  8. <!-- Spring 容器中不扫描 Controller组件 -->
  9. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  10. </context:component-scan>
  11. </beans>

log4j.xml

  1. <?xml version='1.0' encoding='utf-8' ?>
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3. <log4j:configuration >
  4. <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
  5. <layout class="org.apache.log4j.PatternLayout">
  6. <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] [%c:%L] - %m%n" />
  7. <!--
  8. %p: 输出日志信息优先级,即DEBUG,INFO,WARN,ERROR,FATAL,
  9. %d: 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似:2002年10月18日 22:10:28,921
  10. %r: 输出自应用启动到输出该log信息耗费的毫秒数
  11. %c: 输出日志信息所属的类目,通常就是所在类的全名
  12. %t: 输出产生该日志事件的线程名
  13. %l: 输出日志事件的发生位置,相当于%C.%M(%F:%L)的组合,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java:10)
  14. %x: 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。
  15. %%: 输出一个”%”字符
  16. %F: 输出日志消息产生时所在的文件名称
  17. %L: 输出代码中的行号
  18. %m: 输出代码中指定的消息,产生的日志具体信息
  19. %n: 输出一个回车换行符,Windows平台为”\r\n”,Unix平台为”\n”输出日志信息换行
  20. -->
  21. </layout>
  22. </appender>
  23. <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
  24. <!-- 设置日志输出文件名 -->
  25. <param name="File" value="/logs/console.log" />
  26. <param name="Append" value="true" />
  27. <!--<param name="encoding" value="UTF-8" />-->
  28. <param name="MaxFileSize" value="10MB" />
  29. <param name="MaxBackupIndex" value="10" />
  30. <layout class="org.apache.log4j.PatternLayout">
  31. <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] [%c:%L] - %m%n" />
  32. </layout>
  33. </appender>
  34. <root >
  35. <level value="debug"/>
  36. <appender-ref ref="STDOUT" />
  37. <!--<appender-ref ref="FILE" />-->
  38. </root>
  39. </log4j:configuration>

发表评论

表情:
评论列表 (有 0 条评论,152人围观)

还没有评论,来说两句吧...

相关阅读