spring boot_1簡單的ssm搭建

在重新學習一下springboot,在此先來回顧一下ssm用xml的方式搭建

工具就用 IntelliJ IDEA

1 先用IDEA搭建一個J2EE項目

目錄結構
在這裏插入圖片描述

2 在pom.xml文件下添加依賴
<?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.smile</groupId>
    <artifactId>xmlssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.1.RELEASE</version>
        </dependency>

    </dependencies>

</project>

只用添加 spring-webmvc 一個 其他的依賴maven都會幫你添加進來 如下圖
在這裏插入圖片描述

3 然後創建spring與springmvc的xml配置文件

在resource目錄下創建spring的xml配置文件以及springmvc.xml配置文件

在這裏插入圖片描述
application.xml(spring的配置)

<?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 http://www.springframework.org/schema/context/spring-context.xsd">

                             <!--使用默認過濾器爲true 就是把org.smile下面所有東西都掃描-->
    <context:component-scan base-package="org.smile" use-default-filters="true">
        <!--除去controller包,所有包都掃,就是不掃描controller-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
</beans>

spring-mvc.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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <!--只掃描controller包-->
        <context:component-scan base-package="org.smile" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>

    <mvc:annotation-driven />
</beans>

其實可以不用配置這麼麻煩的掃描,可以直接掃描全部就可以了,這樣是爲了更好的理解

4 配置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">
    <!--spring 相關-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!--spring mvc相關-->
    <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>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>
5 測試

創建controller , service 包 以及測試類
在這裏插入圖片描述
(1)TestController.class

package org.smile.controller;

import org.smile.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Smile    博客  https://blog.csdn.net/Smile__1
 * @version 1.0
 * @date 2019/12/3 21:38
 * @description
 */
@RestController
public class TestController {
    @Autowired
    TestService testService;

    @GetMapping(value = "/hello",produces = "text/html;charset=utf-8")//中文亂碼
    public String test(){

        return testService.hello();
    }
}

(2)TestService

package org.smile.service;

/**
 * @author Smile    博客  https://blog.csdn.net/Smile__1
 * @version 1.0
 * @date 2019/12/3 21:54
 * @description
 */

public interface TestService {

    public String hello();
}

(3)TestServiceImpl

package org.smile.service.impl;

import org.smile.service.TestService;
import org.springframework.stereotype.Service;

/**
 * @author Smile    博客  https://blog.csdn.net/Smile__1
 * @version 1.0
 * @date 2019/12/3 21:55
 * @description
 */
@Service
public class TestServiceImpl implements TestService {
    public String hello() {
        return "hello  smile  ssm 哈嘍";
    }
}

然後用Tomcat啓動項目
有些剛接觸IDEA的不太熟悉, 下面我們來配置一下tomcat, 點擊右上角
配置tomcat
來配置tomcat,看到以下界面
在這裏插入圖片描述
點擊左上角的加號
在這裏插入圖片描述
往下拉按圖中的步驟選擇

在這裏插入圖片描述然後點擊Deployment
在這裏插入圖片描述
點擊左面加號
選擇第一個
在這裏插入圖片描述

最後點擊ok完事
在這裏插入圖片描述
啓動tomcat

在這裏插入圖片描述
測試成功!!!

發佈了59 篇原創文章 · 獲贊 248 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章