• IDEA搭建ssm项目


    此前,我一直在用eclipse编辑器做java项目,现在初次使用IDEA编辑器,在这里,我记录了使用IDEA环境下搭建ssm项目的过程。

    创建Maven项目,如下

    右击TEST4项目,在弹出的菜单中选择Add Framework Support

    在弹出的菜单中选择Web Application

    此时在Test4项目中新增了web相关元素,具体结构如下:

    接下来,我们在项目中添加tomcat服务器,在IDEA的上方点击Edit Configurations,进入tomcat设置窗口。如下

    在窗口中点击+号,并选择Local,

    在设置窗口中,可配置tomcat服务器

    在TEST4目录下,创建了t1.jsp的文件,然后启动tomcat服务器,即可通过http://localhost:8080/Test4_war_exploded/t1.jsp访问该页面。我的项目名为Test4,我的url路径中却包含Test4_war_exploded。

    右击Test4,在弹出的菜单中选择Open Module Settings,设置如下所示

    同时在服务器配置中,重新添加artifact

    在浏览器输入http://localhost:8080/Test4/t1.jsp,即可打开。

    接下来,引入ssm相关库,我在pom.xml文件中加入以下内容,点击reload后即自动联网下载完毕相关jar包。

    1. "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.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>Test4artifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <maven.compiler.source>8maven.compiler.source>
    11. <maven.compiler.target>8maven.compiler.target>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <spring-version>x.x.x.RELEASEspring-version>
    14. <mybatis-version>x.x.xmybatis-version>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>junitgroupId>
    19. <artifactId>junitartifactId>
    20. <version>3.8.1version>
    21. <scope>testscope>
    22. dependency>
    23. <dependency>
    24. <groupId>org.springframeworkgroupId>
    25. <artifactId>spring-webartifactId>
    26. <version>${spring-version}version>
    27. dependency>
    28. <dependency>
    29. <groupId>org.springframeworkgroupId>
    30. <artifactId>spring-testartifactId>
    31. <version>${spring-version}version>
    32. dependency>
    33. <dependency>
    34. <groupId>org.springframeworkgroupId>
    35. <artifactId>spring-webmvcartifactId>
    36. <version>${spring-version}version>
    37. dependency>
    38. <dependency>
    39. <groupId>org.springframeworkgroupId>
    40. <artifactId>spring-coreartifactId>
    41. <version>${spring-version}version>
    42. dependency>
    43. <dependency>
    44. <groupId>org.springframeworkgroupId>
    45. <artifactId>spring-contextartifactId>
    46. <version>${spring-version}version>
    47. dependency>
    48. <dependency>
    49. <groupId>org.springframeworkgroupId>
    50. <artifactId>spring-context-supportartifactId>
    51. <version>${spring-version}version>
    52. dependency>
    53. <dependency>
    54. <groupId>org.springframeworkgroupId>
    55. <artifactId>spring-aopartifactId>
    56. <version>${spring-version}version>
    57. dependency>
    58. <dependency>
    59. <groupId>org.aspectjgroupId>
    60. <artifactId>aspectjweaverartifactId>
    61. <version>1.8.5version>
    62. dependency>
    63. <dependency>
    64. <groupId>org.springframeworkgroupId>
    65. <artifactId>spring-beansartifactId>
    66. <version>${spring-version}version>
    67. dependency>
    68. <dependency>
    69. <groupId>org.springframeworkgroupId>
    70. <artifactId>spring-txartifactId>
    71. <version>${spring-version}version>
    72. dependency>
    73. <dependency>
    74. <groupId>org.springframeworkgroupId>
    75. <artifactId>spring-jdbcartifactId>
    76. <version>${spring-version}version>
    77. dependency>
    78. <dependency>
    79. <groupId>org.springframeworkgroupId>
    80. <artifactId>spring-ormartifactId>
    81. <version>${spring-version}version>
    82. dependency>
    83. <dependency>
    84. <groupId>org.springframeworkgroupId>
    85. <artifactId>spring-websocketartifactId>
    86. <version>4.3.9.RELEASEversion>
    87. dependency>
    88. <dependency>
    89. <groupId>org.mybatisgroupId>
    90. <artifactId>mybatisartifactId>
    91. <version>${mybatis-version}version>
    92. dependency>
    93. <dependency>
    94. <groupId>org.mybatisgroupId>
    95. <artifactId>mybatis-springartifactId>
    96. <version>1.3.1version>
    97. dependency>
    98. <dependency>
    99. <groupId>ch.qos.logbackgroupId>
    100. <artifactId>logback-classicartifactId>
    101. <version>1.1.7version>
    102. dependency>
    103. <dependency>
    104. <groupId>javax.servletgroupId>
    105. <artifactId>jsp-apiartifactId>
    106. <version>2.0version>
    107. <scope>providedscope>
    108. dependency>
    109. <dependency>
    110. <groupId>javax.servletgroupId>
    111. <artifactId>servlet-apiartifactId>
    112. <version>2.5version>
    113. <scope>providedscope>
    114. dependency>
    115. <dependency>
    116. <groupId>mysqlgroupId>
    117. <artifactId>mysql-connector-javaartifactId>
    118. <version>x.x.xversion>
    119. dependency>
    120. <dependency>
    121. <groupId>com.alibabagroupId>
    122. <artifactId>druidartifactId>
    123. <version>1.0.18version>
    124. dependency>
    125. <dependency>
    126. <groupId>com.alibabagroupId>
    127. <artifactId>fastjsonartifactId>
    128. <version>1.2.3version>
    129. dependency>
    130. <dependency>
    131. <groupId>com.fasterxml.jackson.coregroupId>
    132. <artifactId>jackson-coreartifactId>
    133. <version>2.5.4version>
    134. dependency>
    135. <dependency>
    136. <groupId>com.fasterxml.jackson.coregroupId>
    137. <artifactId>jackson-databindartifactId>
    138. <version>2.5.4version>
    139. dependency>
    140. <dependency>
    141. <groupId>com.fasterxml.jackson.coregroupId>
    142. <artifactId>jackson-annotationsartifactId>
    143. <version>2.5.4version>
    144. dependency>
    145. dependencies>
    146. project>

    然后再进行Put into Output Root操作,如下:

    另外,web.xml文件如下:

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <servlet-name>springmvcservlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>
    12. /WEB-INF/config/servlet-context.xml
    13. param-value>
    14. init-param>
    15. <load-on-startup>1load-on-startup>
    16. servlet>
    17. <servlet-mapping>
    18. <servlet-name>springmvcservlet-name>
    19. <url-pattern>/url-pattern>
    20. servlet-mapping>
    21. <filter>
    22. <filter-name>characterEncodingFilterfilter-name>
    23. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    24. <init-param>
    25. <param-name>encodingparam-name>
    26. <param-value>UTF-8param-value>
    27. init-param>
    28. <init-param>
    29. <param-name>forceEncodingparam-name>
    30. <param-value>trueparam-value>
    31. init-param>
    32. filter>
    33. <filter-mapping>
    34. <filter-name>characterEncodingFilterfilter-name>
    35. <url-pattern>/*url-pattern>
    36. filter-mapping>
    37. web-app>
    servlet-context.xml如下:
    1. "1.0" encoding="UTF-8"?>
    2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:beans="http://www.springframework.org/schema/beans"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xmlns:mvc="http://www.springframework.org/schema/mvc"
    7. xmlns:websocket="http://www.springframework.org/schema/websocket"
    8. xmlns:task="http://www.springframework.org/schema/task"
    9. xsi:schemaLocation="http://www.springframework.org/schema/mvc
    10. http://www.springframework.org/schema/mvc/spring-mvc.xsd
    11. http://www.springframework.org/schema/beans
    12. http://www.springframework.org/schema/beans/spring-beans.xsd
    13. http://www.springframework.org/schema/context
    14. http://www.springframework.org/schema/context/spring-context.xsd
    15. http://www.springframework.org/schema/websocket
    16. http://www.springframework.org/schema/websocket/spring-websocket.xsd
    17. http://www.springframework.org/schema/task
    18. http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    19. <annotation-driven />
    20. <context:component-scan base-package="com.test4.config" />
    21. <context:component-scan base-package="com.test4.web" />
    22. <mvc:resources mapping="/user/**" location="/">mvc:resources>
    23. beans:beans>

    WebConfig.java如下:

    1. package com.test4.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.ComponentScan;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.transaction.annotation.EnableTransactionManagement;
    6. import org.springframework.web.servlet.ViewResolver;
    7. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    8. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    9. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    10. import org.springframework.web.servlet.view.InternalResourceViewResolver;
    11. @Configuration
    12. @EnableWebMvc
    13. @EnableTransactionManagement
    14. @ComponentScan({"com.test4.web"})
    15. public class WebConfig extends WebMvcConfigurerAdapter{
    16. @Bean
    17. public ViewResolver viewResolver() {
    18. System.out.println("WebConfig viewResolver------------");
    19. InternalResourceViewResolver resolver=new InternalResourceViewResolver();
    20. resolver.setPrefix("/WEB-INF/");
    21. resolver.setSuffix(".jsp");
    22. return resolver;
    23. }
    24. }

    那么,如何在不联网的时候引入jar包呢,需要进行如下操作。在library中点+按钮选择本地jar所在的目录。如下图

    同时在WEN-INF中新建lib目录,复制粘贴jar到该ib目录,如下图,即完成了引入。

  • 相关阅读:
    Vue3+Ts+Vite项目(第一篇)——使用Vite创建Vue3项目
    回环设备(/dev/loop*)与losetup命令
    Docker Swarm 更新
    Java&C++题解与拓展——leetcode30.串联所有单词的子串【么的新知识】
    约定编程与Sping AOP
    ESP32 Arduino实战基础篇-使用中断和定时器
    java计算机毕业设计实验中心网站(附源码、数据库)
    ROS(5)PX4仿真安装及运行
    vue2.x和vue3.x 环境相关配置
    SpringBoot自带模板引擎Thymeleaf使用详解②
  • 原文地址:https://blog.csdn.net/wchwdog13/article/details/134320957