当前位置: 首页 > article >正文

Spring整合SpringMVC

目录

【pom.xml】文件;

新建【applicationContext.xml】文件

新建【springmvc.xml】文件;

配置【src/main/webapp/WEB-INF/web.xml】文件;

新建【com.gupaoedu.service.IUserService】;

新建【com.gupaoedu.service.impl.UserServiceImpl】;

新建【com.gupaoedu.controller.UserController】;


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.gupaoedu</groupId>
  <artifactId>springmvc_demo_03</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springmvc_demo_03 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.1.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>springmvc_demo_03</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8080</port> <!-- 访问端口 -->
          <path>/</path>    <!-- 访问路径 -->
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

新建【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.gupao.edu" use-default-filters="true">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

新建【springmvc.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">

    <!-- 添加扫描 -->
    <context:component-scan base-package="com.gupao.edu"
        use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 开启注解-->
    <mvc:annotation-driven/>
</beans>

配置【src/main/webapp/WEB-INF/web.xml】文件;

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- 配置Spring -->
  <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>
  <!-- 配置SpringMVC -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 关联SpringMVC的配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

新建【com.gupaoedu.service.IUserService】;

package com.gupaoedu.service;

public interface IUserService {

    public String hello();

}

新建【com.gupaoedu.service.impl.UserServiceImpl】;


import com.gupaoedu.service.IUserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Override
    public String hello() {
        return "hello Service ... ... ";
    }
}

新建【com.gupaoedu.controller.UserController】;

package com.gupaoedu.controller;

import com.gupaoedu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/user/hello")
    public String hello() {
        return userService.hello();
    }

}


http://www.kler.cn/a/500816.html

相关文章:

  • docker安装windows desktop后打开失败
  • 计算机网络 (33)传输控制协议TCP概述
  • 基于STM32的智能家居蓝牙系统(论文+源码)
  • 笔记本电脑 选购 回收 特权模式使用 指南
  • 【YOLOv5】源码(train.py)
  • iOS - runtime总结
  • 电商项目-基于ElasticSearch实现商品搜索功能(四)
  • Kotlin 协程基础三 —— 结构化并发(二)
  • 国产3D CAD将逐步取代国外软件
  • Excel中身份证号码都变成E+乱码显示如何处理?
  • 2024 Java若依(RuoYi)框架视频教程(课件+示例代码+视频)
  • 【DevOps】Jenkins使用Pipline发布Web项目
  • WEB前端-3.1
  • 抖音矩阵是什么
  • 探索 Cloudflare Workers:高效边缘计算的新选择
  • 浅谈云计算02 | 云计算模式的演进
  • Flutter中Get.snackbar避免重复显示的实现
  • Gitlab-Runner配置
  • ModbusTCP转CCLINKIE在机器人中的革命性应用!
  • ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders论文解读
  • SUTD:偏好优化提升文本到音频效果
  • 理解 SQL 中NULL值对IN操作符的影响
  • 蓝桥杯历届真题 # 封闭图形个数(C++,Java)
  • Win32汇编学习笔记10.OD插件
  • Vue.js组件开发-如何使用day.js、luxon或date-fns处理日期时间
  • 【经管数据】ZF数字采购采购明细数据(2015.3-2024.3)