java springmvc配置,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标签配置)

注意命名空间的问题

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

3、web.xml配置

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

mvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:ApplicationContext.xml

mvc

*.do

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

cai jin hong

测试

请求和响应流程:

当点击测试超链接时,浏览器向服务器发送userController.do的资源请求

服务器接收到之后,[email protected]("userController.do")注解的对象

找到了之后,[email protected]("xxx")注解的方法

如果只有一个方法,可以不用写名字,直接写RequestMapping

如果有多个方法,需要注明方法名

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值