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

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 {@Overridepublic 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 {@Autowiredprivate IUserService userService;@GetMapping("/user/hello")public String hello() {return userService.hello();}}

http://www.lryc.cn/news/517750.html

相关文章:

  • 【Rust自学】10.4. trait Pt.2:trait作为参数和返回类型、trait bound
  • 嵌入式系统 (2.嵌入式硬件系统基础)
  • Linux 下 Vim 环境安装踩坑问题汇总及解决方法(重置版)
  • OpenAI 故障复盘 - 阿里云容器服务与可观测产品如何保障大规模 K8s 集群稳定性
  • 安卓触摸对焦
  • jupyter出现“.ipynb appears to have died. It will restart automatically.”解决方法
  • 20250108-实验+神经网络
  • 【权限管理】CAS(Central Authentication Service)
  • Golang笔记:使用net包进行TCP监听回环测试
  • 《浮岛风云》V1.0中文学习版
  • Day10——爬虫
  • 10. C语言 函数详解
  • NRC优先级中比较特殊的—NRC0x13和NRC0x31
  • ref() 和 reactive() 区别
  • 深度学习与计算机视觉 (博士)
  • Sprint Boot教程之五十:Spring Boot JpaRepository 示例
  • NaVILA:用于足式机器人导航的VLA模型
  • 大语言模型提示技巧(七)-扩展
  • 基类指针指向派生类对象,基类指针的首地址永远指向子类从基类继承的基类首地址
  • 25年01月HarmonyOS应用基础认证最新题库
  • wps宏js接入AI功能和接入翻译功能
  • 【Logstash03】企业级日志分析系统ELK之Logstash 过滤 Filter 插件
  • 深度学习:Java DL4J基于RNN构建智能停车管理模型
  • 花生好坏缺陷识别数据集,7262张图片,支持yolo,coco json,pasical voc xml格式的标注,识别准确率在95.7%
  • 2025年:AI化浪潮中的社会变迁与商业革新
  • filebeat、kafka
  • js单例模式
  • 【设计模式】装饰器与代理模式的对比
  • Proteus-8086调试汇编格式的一点心得
  • 什么是Kafka?有什么主要用途?