1.打开idea,File->New->Project如下图
2.在弹出window中左侧列表中选择Java->Next->Next->输入工程名->Finish,如下图
3.(创建Module)鼠标选中该项目MyWeb-> 右键选择New,选择Module
4.在弹出window中左侧默认选择Java,右侧选择Java EE/Web Application,如下图,点击Finish后,输入 Module名称,->Finish
5.创建好的web工程
6.在该工程的web/WEB-INF下创建两个文件夹:classes和lib,classes用来存放编译后输出的class文件,lib用于存放第三方jar包。(但是我在项目启动中发现classes文件夹中并没有文件,不知道什么原因)
7.配置一下刚刚创建的两个文件夹路径
File -> Project Structure (快捷键:Ctrl + Shift + Alt + S) -> 选择Module :
选择 Paths -> 选择”Use module compile output path” -> 将Output path和Test output path都选择刚刚创建的classes文件夹。
接着选择Dependencies -> 将Module SDK选择为1.8 -> 点击右边的“+”号 -> 选择1 “Jars or Directories”
-> 选择刚刚创建的lib文件夹
-> 选择“jar directory” -> 一直OK返回,即可在tomcat中运行该项目(此步骤不解释,百度有很多)
8.导入需要的jar文件
(1)将整理好的jar全部copy到WEB-INFO下的lib下
(2)全部拷贝后,File -> Project Structure (快捷键:Ctrl + Shift + Alt + S) ->Libraries->绿色加号->选择java->选择刚才导入到lib下的全部jar
->add给SSM这个Module
->起个名字叫lib->OK
9.配置SSM所需要的文件,web.xml文件,为web/WEB-INF/web.xml添加内容如下,添加spring和mybatis最需要的两个xml.和Spring监听器。
<?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_3_1.xsd"
version="3.1">
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
10.同时在src下创建spring-mvc.xml和spring-mybatis.xml两个文件,分别如下
(1)spring-mvc.xml,因为我的包名比较简单,以com.test开头,如需要可自行修改
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<mvc:annotation-driven />
<!-- 对静态资源文件的访问 不支持访问WEB-INF目录 -->
<!-- <mvc:default-servlet-handler /> -->
<context:component-scan base-package="com.test">
</context:component-scan>
<mvc:resources mapping="/img/**" location="/WEB-INF/img/" />
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
(2)spring-mybatis.xml配置如下,数据连接可自行修改
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.test">
<!--将Controller的注解排除掉 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="dataSourceB" class="com.alibaba.drui