此前,我一直在用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包。
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Test4</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-version>x.x.x.RELEASE</spring-version><mybatis-version>x.x.x</mybatis-version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- spring核心包 start--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.5</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring-version}</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket --><dependency><groupId>org.springframework</groupId><artifactId>spring-websocket</artifactId><version>4.3.9.RELEASE</version></dependency><!-- spring核心包 end--><!-- mybatis核心包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis-version}</version></dependency><!-- mybatis-spring包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><!-- logback 日志 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.1.7</version></dependency><!-- servlet --><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>x.x.x</version></dependency><!-- druid数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.18</version></dependency><!--json解析包 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.5.4</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.5.4</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.5.4</version></dependency></dependencies></project>
然后再进行Put into Output Root操作,如下:
另外,web.xml文件如下:
<?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"><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/config/servlet-context.xml<!-- ;/WEB-INF/config/spring-mvc-interceptor.xml --></param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>
servlet-context.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:websocket="http://www.springframework.org/schema/websocket"xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/websockethttp://www.springframework.org/schema/websocket/spring-websocket.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"><annotation-driven /> <!-- --><context:component-scan base-package="com.test4.config" /><context:component-scan base-package="com.test4.web" /><mvc:resources mapping="/user/**" location="/"></mvc:resources></beans:beans>
WebConfig.java如下:
package com.test4.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan({"com.test4.web"})
public class WebConfig extends WebMvcConfigurerAdapter{@Beanpublic ViewResolver viewResolver() {System.out.println("WebConfig viewResolver------------");InternalResourceViewResolver resolver=new InternalResourceViewResolver();resolver.setPrefix("/WEB-INF/");resolver.setSuffix(".jsp");return resolver;}
}
那么,如何在不联网的时候引入jar包呢,需要进行如下操作。在library中点+按钮选择本地jar所在的目录。如下图
同时在WEN-INF中新建lib目录,复制粘贴jar到该ib目录,如下图,即完成了引入。