此前,我一直在用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.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.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>Test4artifactId>
- <version>1.0-SNAPSHOTversion>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <spring-version>x.x.x.RELEASEspring-version>
- <mybatis-version>x.x.xmybatis-version>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>3.8.1version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-coreartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-context-supportartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aopartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.aspectjgroupId>
- <artifactId>aspectjweaverartifactId>
- <version>1.8.5version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-beansartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-ormartifactId>
- <version>${spring-version}version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-websocketartifactId>
- <version>4.3.9.RELEASEversion>
- dependency>
-
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis-version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>1.3.1version>
- dependency>
-
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.1.7version>
- dependency>
-
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>jsp-apiartifactId>
- <version>2.0version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>servlet-apiartifactId>
- <version>2.5version>
- <scope>providedscope>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>x.x.xversion>
- dependency>
-
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.0.18version>
- dependency>
-
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.3version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-coreartifactId>
- <version>2.5.4version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.5.4version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-annotationsartifactId>
- <version>2.5.4version>
- dependency>
-
- dependencies>
-
- project>
然后再进行Put into Output Root操作,如下:
另外,web.xml文件如下:
- "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>springmvcservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>
- /WEB-INF/config/servlet-context.xml
-
- param-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>springmvcservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
- <filter>
- <filter-name>characterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- <init-param>
- <param-name>forceEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>characterEncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>
servlet-context.xml如下:
- "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.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/websocket
- http://www.springframework.org/schema/websocket/spring-websocket.xsd
- http://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{
- @Bean
- public 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目录,如下图,即完成了引入。