Idea 工具 创建SSM框架的web工程(非Maven)

本文介绍了如何在IntelliJ IDEA中创建一个非Maven的SSM(Spring、SpringMVC、MyBatis)框架的Web工程。详细步骤包括:新建Java项目和Module,配置Web应用,创建classes和lib文件夹,设置编译输出路径,导入和管理依赖库,编写web.xml、spring和mybatis配置文件,创建包和对应的Java类。完成上述步骤后,通过访问特定URL可获取数据库数据。
摘要由CSDN通过智能技术生成

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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值