0、Idea创建xmlssm项目并tomcat启动

1、新建maven工程

0、Idea创建xmlssm项目并tomcat启动_第1张图片
0、Idea创建xmlssm项目并tomcat启动_第2张图片
0、Idea创建xmlssm项目并tomcat启动_第3张图片

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>com.cs</groupId>
    <artifactId>xmlssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--war包部署-->
    <packaging>war</packaging>

    <dependencies>
        <!--添加springmvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>

            <!--tomcat插件启动-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <!--控制tomcat端口号 -->
                <configuration>
                    <port>8080</port>
                    <!-- 发布到tomcat后的名称 -->
                    <!--/ 相当于把项目发布成ROOT -->
                    <path>/abc</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <!--  <finalName>love</finalName>
                    <server>tomcat7</server> -->
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3、设置web工程目录

0、Idea创建xmlssm项目并tomcat启动_第4张图片
0、Idea创建xmlssm项目并tomcat启动_第5张图片
双击打开,点击ok,yes
0、Idea创建xmlssm项目并tomcat启动_第6张图片
点击+号,选择web.xml
0、Idea创建xmlssm项目并tomcat启动_第7张图片
剪切\WEB-INF\web.xml,打开右面…,放到项目webapp目录下
0、Idea创建xmlssm项目并tomcat启动_第8张图片
0、Idea创建xmlssm项目并tomcat启动_第9张图片

0、Idea创建xmlssm项目并tomcat启动_第10张图片
0、Idea创建xmlssm项目并tomcat启动_第11张图片
完成后,目录结构变成这样:
0、Idea创建xmlssm项目并tomcat启动_第12张图片

4、resources添加配置文件

注意,先添加springmvc依赖再添加配置文件,添加applicationContext.xm和spring-servlet.xml配置文件
0、Idea创建xmlssm项目并tomcat启动_第13张图片
0、Idea创建xmlssm项目并tomcat启动_第14张图片

5、新建controller目录和service目录

0、Idea创建xmlssm项目并tomcat启动_第15张图片
右键把controller和service目录分开,放在同一级目录下
0、Idea创建xmlssm项目并tomcat启动_第16张图片

0、Idea创建xmlssm项目并tomcat启动_第17张图片

0、Idea创建xmlssm项目并tomcat启动_第18张图片

0、Idea创建xmlssm项目并tomcat启动_第19张图片

0、Idea创建xmlssm项目并tomcat启动_第20张图片

最后目录结构变成这样
0、Idea创建xmlssm项目并tomcat启动_第21张图片

6、配置applicationContext.xml和spring-servlet.xml

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

    <context:component-scan base-package="com.cs" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

spring-servlet.xml:
注意:mvc:annotation选择mvc那个
0、Idea创建xmlssm项目并tomcat启动_第22张图片

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

    <context:component-scan base-package="com.cs" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <mvc:annotation-driven/>
</beans>

7、配置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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

8、编写HelloController和HelloService

HelloController.java:

package com.cs.controller;

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

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author chengshan
 * @Date 2020/6/13 14:00
 * @Version 1.0
 **/
@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    //如果中文乱码,则加上,produces = "text/html;charset=utf-8"
    @GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
    public String sayHello(){
        return helloService.sayHello();
    }
}


HelloService.java:

package com.cs.service;

import org.springframework.stereotype.Service;

/**
 * @ClassName HelloService
 * @Description TODO
 * @Author chengshan
 * @Date 2020/6/13 14:00
 * @Version 1.0
 **/
@Service
public class HelloService {
    public String sayHello() {
        return "Hello world! 你好!";
    }
}

9、插件tomcat启动项目

tomcat插件启动项目:
0、Idea创建xmlssm项目并tomcat启动_第23张图片
浏览器访问http://localhost:8080/abc/hello
0、Idea创建xmlssm项目并tomcat启动_第24张图片

10、本地tomcat启动项目

除了上面的插件tomcat启动,还可以本地tomcat启动项目
0、Idea创建xmlssm项目并tomcat启动_第25张图片

0、Idea创建xmlssm项目并tomcat启动_第26张图片
配置tomcat
0、Idea创建xmlssm项目并tomcat启动_第27张图片

0、Idea创建xmlssm项目并tomcat启动_第28张图片

0、Idea创建xmlssm项目并tomcat启动_第29张图片
选war包
0、Idea创建xmlssm项目并tomcat启动_第30张图片
0、Idea创建xmlssm项目并tomcat启动_第31张图片
启动tomcat
0、Idea创建xmlssm项目并tomcat启动_第32张图片

浏览器访问http://localhost:8080/xmlssm_war/hello
0、Idea创建xmlssm项目并tomcat启动_第33张图片

你可能感兴趣的:(SpringBoot学习)