SpringMVC---配置与使用的示例

SpringMVC是Spring的一个组件,所以我们在使用SpringMVC的时候也会使用到Spring

使用环境

  • JDK:1.8
  • Tomcat:9.0.3
  • spring:5.2.8
  • 编译器:IDEA2019

1、导包

需要引入Spring-web和Spring-webmvc两个包,可以到maven仓库里面去下载或者使用maven依赖

2、ApplicationContext.xml配置(Spring的核心配置文件)

  • ApplicationContext.xml文件需要放在WEB-INF下,并且需要把名字改为拦截的serlvet-name+ -Servlet,比如我这边的拦截名字为mvc,所以我需要把配置文件名改为mvc-Servlet.xml
  • 如果不放在WEB-INF下,需要在web.xml文件中进行路径配置(如下web.xml文件中的init-param标签配置)
  • 注意命名空间的问题
<?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
 https://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注解驱动-->
   <context:component-scan base-package="com.cjh"/>
  <!-- 开启mvc注解驱动-->
   <mvc:annotation-driven></mvc:annotation-driven>
</beans>

3、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">
   <servlet> <servlet-name>mvc</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param> <param-name>contextConfigLocation</param-name>
   <!-- 说明Spring核心配置文件的位置-->
   <param-value>classpath:ApplicationContext.xml</param-value>
   </init-param> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name>
   <url-pattern>*.do</url-pattern>
   </servlet-mapping>
</web-app>

4、java的实现

Controller类

@Controller
@RequestMapping("userController.do")
public class UserController {

   public UserController(){
     System.out.println("controller创建了");
   }
   @RequestMapping
   public void test(){
     System.out.println("controller:test方法执行了");
   }

}

index.jsp

<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>cai jin hong</title>
 <style>
 </style></head>
<body>
 <a href="userController.do" rel="external nofollow" >测试</a>
</body>
</html>

请求和响应流程:

  • 当点击测试超链接时,浏览器向服务器发送userController.do的资源请求
  • 服务器接收到之后,找到类上面带有@RequestMapping("userController.do")注解的对象
  • 找到了之后,查找方法上面带有@RequestMapping("xxx")注解的方法

如果只有一个方法,可以不用写名字,直接写RequestMapping
如果有多个方法,需要注明方法名

  • 找到了之后,执行方法,并将处理信息响应回给浏览器(该代码中没有返回值)

本篇文章只讲了一下最基本的时候,下一篇文章会详细的说的SpringMVC请求和响应的处理!!!

以上就是SpringMVC---配置与使用的示例的详细内容,更多关于SpringMVC---配置与使用的资料请关注我们其它相关文章!

(0)

相关推荐

  • springmvc流程图以及配置解析

    springmvc:是完成数据的封装和跳转的功能 流程图如下: springmvc的配置流程 1.导入jar包 2.配置servlet文件 init-param的作用是在启动servlet启动时规定其地地址及名称去搜寻其springmvc配置文件 3.配置springmvc配置文件 进行handlermapping的配置,不进行配置时BeanNameUrlHandlerMapping, handlermapping的三种方式 1.默认方式BeanNameUrlHandlerMapping,根据b

  • 使用注解开发SpringMVC详细配置教程

    1.使用注解开发SpringMVC 1.新建一个普通的maven项目,添加web支持 2.在pom.xml中导入相关依赖 SpringMVC相关 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency&

  • 在IDEA中搭建最小可用SpringMVC项目(纯Java配置)

    1. 新建 SpringMVC 的 Web 项目 File - New - Project.. 勾选 SpringMVC 和 WebApplication ,点击Next 填写 Project name : hello 点击 Finish IDEA 会自动下载所需的 SpringMVC 的 jar 包 2. 代码编写 代码参考 <Spring 实战>(第四版),本文和书中代码略有差异 删除不需要的配置文件 删除 WEB-INF 下的 web.xml 删除 WEB-INF 下的 dispatch

  • SpringMVC拦截器配置及运行流程解析

    1.与过滤器filter的区别 2.springMVC中拦截器的必须实现的三个方法: 3. 拦截器类的编写: package com.imooc.core; import com.imooc.bean.User; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpS

  • SpringMVC Mybatis配置多个数据源并切换代码详解

    这篇文章主要介绍了SpringMVC Mybatis配置多个数据源并切换代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 最近公司一个项目需要连接两个数据库(A和B)操作,有的模块查询A库,有的模块查询B库,因此需要改造下,项目后台用的是SpringMVC+Mybatis+MySQL架构,折腾了两天后终于搞定了,在这里记录过改造过程. 使用场景 多数据源的使用的场景一般有: 主从数据库切换 读写分离 兼容旧库 实现原理 Spring2.x

  • 在IntelliJ IDEA 搭建springmvc项目配置debug的教程详解

    1.添加一个maven 2.选择maven 3.配置好需要debug的父工程 4.配置maven 5.最后就可以打断点debug了 到此这篇关于在IntelliJ IDEA 搭建springmvc项目配置debug的教程详解的文章就介绍到这了,更多相关IDEA 搭建springmvc项目配置debug内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

  • SpringMVC和Spring的配置文件扫描包详解

    在Spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,Spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景就是在一个项目中引入Spring和SpringMVC这两个框架,那么它其实就是两个容器,Spring是父容器,SpringMVC是其子容器,并且在Spring父容器中注册的Bean对于SpringMVC容器中是可见的,而在SpringMVC容器中注册的Bean对于Spring父容器中是不可见的,也就

  • 浅谈SpringMVC的拦截器(Interceptor)和Servlet 的过滤器(Filter)的区别与联系 及SpringMVC 的配置文件

    1.过滤器: 依赖于servlet容器.在实现上基于函数回调,可以对几乎所有请求进行过滤,但是缺点是一个过滤器实例只能在容器初始化时调用一次.使用过滤器的目的是用来做一些过滤操作,获取我们想要获取的数据. 比如:在过滤器中修改字符编码:在过滤器中修改 HttpServletRequest的一些参数,包括:过滤低俗文字.危险字符等 关于过滤器的一些用法可以参考我写过的这些文章: 继承HttpServletRequestWrapper以实现在Filter中修改HttpServletRequest的参

  • 详解SpringMVC的url-pattern配置及原理剖析

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

  • SpringMvc web.xml配置实现原理过程解析

    1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法. 3.部

  • HelloSpringMVC配置版实现步骤解析

    配置版步骤 新建一个module,添加web的支持 确定导入了SpringMVC的依赖 配置web.xml,注册DispatcherServlet <?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-in

  • springmvc注解配置实现解析

    springmvc大大减少了对xml的配置,减少了配置量,以及可以在一个controller类中进行多个请求配置 一.springmvc配置 context:component-scan 开启包扫描,对指定的包进行注解扫描 mvc:annotation-driven开启注解功能 二.controller配置 在类上加上@Controller 在指定的方法上@RequestMapping("/t请求名") 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们.

随机推荐