idea第一个SSM项目

本文档详细介绍了如何在IntelliJ IDEA中创建第一个SSM(Spring、Struts、MyBatis)项目,包括手动配置web.xml、编写pom.xml、applicationContext.xml、spring-service.xml等关键步骤,并提供了测试和项目打包的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

idea第一个SSM项目

  • 第一个idea web xml-ssm项目
  • maven项目,不使用模板。需要手动生成web.xml

pom.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--输入com,提示标签-->
    <!--配置扫描-->
    <!--配置基准扫描路径-->
    <!--使用默认拦截器-->
        <!--排除拦截器:排除Controller类-->
             <!--不要选择这个类 expression="org.springframework.web.servlet.mvc.Controller"-->
    <context:component-scan base-package="com.cjw" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

applicationContext.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--输入com,提示标签-->
    <!--配置扫描-->
    <!--配置基准扫描路径-->
    <!--使用默认拦截器-->
        <!--排除拦截器:排除Controller类-->
             <!--不要选择这个类 expression="org.springframework.web.servlet.mvc.Controller"-->
    <context:component-scan base-package="com.cjw" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

spring-service.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--spring-mvc的配置文件-->
    
    <!--只扫描Controller类-->
    <!--包含拦截器:扫描Controller类-->
    <context:component-scan base-package="com.cjw" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--???-->
    <mvc:annotation-driven />

    <!--???-->
    <!--静态资源文件路径相关-->
    <mvc:resources mapping="/**" location="/"/>
    <!--<mvc:resources mapping="/css/*" location="/css/"/>-->
    <!--<mvc:resources mapping="/img/*" location="/img/"/>-->
    
</beans>

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">

    <!--tomcat启动时就加载spring的配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--深究原理-->
    <!--ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--配置springmvc调度器-->
    <!--加载springmvc的配置文件-->
    <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-service.xml</param-value>
        </init-param>
    </servlet>

    <!--servlet映射路径-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

测试

目录结构

图片.png

package com.cjw.controller;

import com.cjw.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String sayHello() {
        return helloService.hello();
    }
}

package com.cjw.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {

    public String hello(){
        return  "hello";
    }

}

图片.png

项目打包

项目打包 提取码:n3n1 环境:idea,ssm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值