pringMVC帮我们做了什么?与传统的servlet开发有什么区别?
- 入口控制:springMVC通过DispatcherServlet作为入口控制器。负责接收请求和分发请求。而在servlet中。需要自己编写servlet程序,并在web.xml中配置,才能接收和处理请求。
- 在springMVC中,表单提交时可以自动将表单数据绑定到对应的JavaBean对象中。只需要在控制器方法的参数列表中声明该JavaBean对象即可。无需手动获取和赋值表单数据。而在servlet中需要手动完成。
- IoC容器:springMVC通过IOC容器管理对象。只需要在配置文件中进行相应的配置即可获取实例对象。而在servlet中需要手动创建对象实例。
- 统一处理请求:springMVC提供了拦截器、异常处理器等统一处理请求的机制。并且可以灵活的配置这些处理器。而在servlet中。需要自行编写过滤器、异常处理器等,增加了代码复杂度和开发难度。
- 视图解析:springMVC提供多种视图模板,如jsp、Freemarker等,并且支持国际化、主题等特性。而在servlet中需要手动处理视图,增加了代码复杂度。
第一个springmvc程序
-
引入依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.1.14</version> </dependency> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.1.0</version><scope>provided</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.5.25</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring6</artifactId> <version>3.1.2.RELEASE</version> </dependency> </dependencies> -
在web.xml文件中配置:前端控制器(DispatcherServlet类),所有请求都应应该经过这个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-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>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name></web-app><url-pattern>/</url-pattern> </servlet-mapping>
DispatcherServlet是springmvc最核心的类。他是springmvc的前端控制器,主要职责包括:
-
接收客户端的HTTP请求:DispatcherServlet监听来自web浏览器的HTTP请求。然后根据强求的url将请求数据解析为request对象
-
处理请求的url:DispatcherServlet将请求的url与处理程序进行匹配。确定要调用哪个controller来处理请求
-
调用相应的控制器:DispatcherServlet将强求发送给控制器处理。控制器执行业务逻辑。然后返回一个模型对象(Model)
-
渲染视图:DispatcherServlet调用视图引擎。将模型对象呈现为用户可以查看的html页面
-
返回响应给客户端:DispatcherServlet将响应发送回浏览器。响应包括表单、json、xml、html以及其他类型数据。
-
编写控制器
// @Controller 纳入ioc容器管理, // @Controller 是@Component的别名 @Controller public class FirstController { @RequestMapping("/test") public String say(){ // 返回逻辑视图名称,会通过配置文件设置的前缀和后缀将逻辑视图解析成物理视图进行返回。 return "first"; } } -
编写springmvc配置文件(springmvc-servlet.xml)https://modelscope.cn/learn/396829
https://modelscope.cn/learn/396828
https://modelscope.cn/learn/396827
https://modelscope.cn/learn/396826
https://modelscope.cn/learn/396825
https://modelscope.cn/learn/396824
https://modelscope.cn/learn/396823
https://modelscope.cn/learn/396822
https://modelscope.cn/learn/396821
https://modelscope.cn/learn/396820
https://modelscope.cn/learn/396819
https://modelscope.cn/learn/396817
https://modelscope.cn/learn/396818
https://modelscope.cn/learn/396816
https://modelscope.cn/learn/396815
https://modelscope.cn/learn/396814
https://modelscope.cn/learn/396813
https://modelscope.cn/learn/396812
https://modelscope.cn/learn/396811
https://modelscope.cn/learn/396810
https://modelscope.cn/learn/396809
https://modelscope.cn/learn/396808
https://modelscope.cn/learn/396807
https://modelscope.cn/learn/396806
https://modelscope.cn/learn/396805
https://modelscope.cn/learn/396804
https://modelscope.cn/learn/396803
https://modelscope.cn/learn/396802
https://modelscope.cn/learn/396801
https://modelscope.cn/learn/396800
https://modelscope.cn/learn/396799
https://modelscope.cn/learn/396798
https://modelscope.cn/learn/396797
https://modelscope.cn/learn/396796
https://modelscope.cn/learn/396795
https://modelscope.cn/learn/396794
https://modelscope.cn/learn/396793
https://modelscope.cn/learn/396792
https://modelscope.cn/learn/396791
https://modelscope.cn/learn/396789
https://modelscope.cn/learn/396790
https://modelscope.cn/learn/396788
https://modelscope.cn/learn/396787
https://modelscope.cn/learn/396786
https://modelscope.cn/learn/396785
https://modelscope.cn/learn/396784
https://modelscope.cn/learn/396783
https://modelscope.cn/learn/396782
https://modelscope.cn/learn/396781
https://modelscope.cn/learn/396780
https://modelscope.cn/learn/396779
https://modelscope.cn/learn/396778
https://modelscope.cn/learn/396777
https://modelscope.cn/learn/396776
https://modelscope.cn/learn/396775
https://modelscope.cn/learn/396774
https://modelscope.cn/learn/396773
https://modelscope.cn/learn/396772
https://modelscope.cn/learn/396771
https://modelscope.cn/learn/396770
https://modelscope.cn/learn/396769
https://modelscope.cn/learn/396768
https://modelscope.cn/learn/396767
https://modelscope.cn/learn/396766
https://modelscope.cn/learn/396765
https://modelscope.cn/learn/396764
https://modelscope.cn/learn/396763
https://modelscope.cn/learn/396762
https://modelscope.cn/learn/396761
https://modelscope.cn/learn/396760
https://modelscope.cn/learn/396759
https://modelscope.cn/learn/396758
https://modelscope.cn/learn/396757
https://modelscope.cn/learn/396756
https://modelscope.cn/learn/396755
https://modelscope.cn/learn/396754
https://modelscope.cn/learn/396753
https://modelscope.cn/learn/396751
https://modelscope.cn/learn/396752
https://modelscope.cn/learn/396750
https://modelscope.cn/learn/396749
https://modelscope.cn/learn/396748
https://modelscope.cn/learn/396747
https://modelscope.cn/learn/396746
https://modelscope.cn/learn/396745
https://modelscope.cn/learn/396744
https://modelscope.cn/learn/396743
https://modelscope.cn/learn/396742
https://modelscope.cn/learn/396740
https://modelscope.cn/learn/396741
https://modelscope.cn/learn/396739
https://modelscope.cn/learn/396738
https://modelscope.cn/learn/396737
https://modelscope.cn/learn/396735
https://modelscope.cn/learn/396736
https://modelscope.cn/learn/396734
https://modelscope.cn/learn/396732
https://modelscope.cn/learn/396733
https://modelscope.cn/learn/396731
https://modelscope.cn/learn/396730
https://modelscope.cn/learn/396729
https://modelscope.cn/learn/396728
https://modelscope.cn/learn/396727
https://modelscope.cn/learn/396726
https://modelscope.cn/learn/396725
https://modelscope.cn/learn/396724
https://modelscope.cn/learn/396723
https://modelscope.cn/learn/396722
https://modelscope.cn/learn/396721
https://modelscope.cn/learn/396720
https://modelscope.cn/learn/396719
https://modelscope.cn/learn/396718
https://modelscope.cn/learn/396717
https://modelscope.cn/learn/396716
https://modelscope.cn/learn/396715
https://modelscope.cn/learn/396714
https://modelscope.cn/learn/396713
https://modelscope.cn/learn/396712
https://modelscope.cn/learn/396711
https://modelscope.cn/learn/396710
https://modelscope.cn/learn/396709
https://modelscope.cn/learn/396708
https://modelscope.cn/learn/396707
https://modelscope.cn/learn/396706
https://modelscope.cn/learn/396705
https://modelscope.cn/learn/396704
https://modelscope.cn/learn/396703
https://modelscope.cn/learn/396702
https://modelscope.cn/learn/396701
https://modelscope.cn/learn/396700
https://modelscope.cn/learn/396699
https://modelscope.cn/learn/396698
https://modelscope.cn/learn/396697
https://modelscope.cn/learn/396696
https://modelscope.cn/learn/396695
https://modelscope.cn/learn/396694
https://modelscope.cn/learn/396693
https://modelscope.cn/learn/396692
https://modelscope.cn/learn/396691
https://modelscope.cn/learn/396690
https://modelscope.cn/learn/396689
https://modelscope.cn/learn/396688
https://modelscope.cn/learn/396687
https://modelscope.cn/learn/396685
https://modelscope.cn/learn/396686
https://modelscope.cn/learn/396684
https://modelscope.cn/learn/396683
https://modelscope.cn/learn/396682
https://modelscope.cn/learn/396681
https://modelscope.cn/learn/396680
https://modelscope.cn/learn/396679
https://modelscope.cn/learn/396678
https://modelscope.cn/learn/396677
https://modelscope.cn/learn/396676
https://modelscope.cn/learn/396675
https://modelscope.cn/learn/396674
https://modelscope.cn/learn/396673
https://modelscope.cn/learn/396672
https://modelscope.cn/learn/396671
https://modelscope.cn/learn/396670
https://modelscope.cn/learn/396669
https://modelscope.cn/learn/396668
https://modelscope.cn/learn/396667
https://modelscope.cn/learn/396666
https://modelscope.cn/learn/396665
https://modelscope.cn/learn/396664
https://modelscope.cn/learn/396663
https://modelscope.cn/learn/396662
https://modelscope.cn/learn/396661
https://modelscope.cn/learn/396660
https://modelscope.cn/learn/396659
https://modelscope.cn/learn/396658
https://modelscope.cn/learn/396657
https://modelscope.cn/learn/396656
https://modelscope.cn/learn/396655
https://modelscope.cn/learn/396654
https://modelscope.cn/learn/396653
https://modelscope.cn/learn/396652
https://modelscope.cn/learn/396651
https://modelscope.cn/learn/396650
https://modelscope.cn/learn/396649
https://modelscope.cn/learn/396648
https://modelscope.cn/learn/396647
https://modelscope.cn/learn/396646
https://modelscope.cn/learn/396645
https://modelscope.cn/learn/396644
https://modelscope.cn/learn/396643
https://modelscope.cn/learn/396641
https://modelscope.cn/learn/396642
https://modelscope.cn/learn/396640
https://modelscope.cn/learn/396639
https://modelscope.cn/learn/396638
https://modelscope.cn/learn/396637
https://modelscope.cn/learn/396636
https://modelscope.cn/learn/396635
https://modelscope.cn/learn/396634
https://modelscope.cn/learn/396633
https://modelscope.cn/learn/396632
https://modelscope.cn/learn/396631
https://modelscope.cn/learn/396630
https://modelscope.cn/learn/396629
https://modelscope.cn/learn/396628
https://modelscope.cn/learn/396627
https://modelscope.cn/learn/396626
https://modelscope.cn/learn/396625
https://modelscope.cn/learn/396624
https://modelscope.cn/learn/396623
https://modelscope.cn/learn/396622
https://modelscope.cn/learn/396621
https://modelscope.cn/learn/396620
https://modelscope.cn/learn/396619
https://modelscope.cn/learn/396618
https://modelscope.cn/learn/396617
https://modelscope.cn/learn/396616
https://modelscope.cn/learn/396615
https://modelscope.cn/learn/396614
https://modelscope.cn/learn/396613
https://modelscope.cn/learn/396612
https://modelscope.cn/learn/396611
https://modelscope.cn/learn/396610
https://modelscope.cn/learn/396609
https://modelscope.cn/learn/396608
https://modelscope.cn/learn/396607
https://modelscope.cn/learn/396606
https://modelscope.cn/learn/396605
https://modelscope.cn/learn/396604
https://modelscope.cn/learn/396603
https://modelscope.cn/learn/396602
https://modelscope.cn/learn/396601
https://modelscope.cn/learn/396600
https://modelscope.cn/learn/396598
https://modelscope.cn/learn/396599
https://modelscope.cn/learn/396597
https://modelscope.cn/learn/396596
https://modelscope.cn/learn/396595
https://modelscope.cn/learn/396594
https://modelscope.cn/learn/396593
https://modelscope.cn/learn/396592
https://modelscope.cn/learn/396591
https://modelscope.cn/learn/396590
https://modelscope.cn/learn/396589
https://modelscope.cn/learn/396588
https://modelscope.cn/learn/396587
https://modelscope.cn/learn/396586
https://modelscope.cn/learn/396585
https://modelscope.cn/learn/396584
https://modelscope.cn/learn/396583
https://modelscope.cn/learn/396582
https://modelscope.cn/learn/396581
https://modelscope.cn/learn/396580
https://modelscope.cn/learn/396579
https://modelscope.cn/learn/396578
https://modelscope.cn/learn/396576
https://modelscope.cn/learn/396577
https://modelscope.cn/learn/396575
https://modelscope.cn/learn/396574
https://modelscope.cn/learn/396573
https://modelscope.cn/learn/396571
https://modelscope.cn/learn/396572
https://modelscope.cn/learn/396570
https://modelscope.cn/learn/396569
https://modelscope.cn/learn/396567
https://modelscope.cn/learn/396568
https://modelscope.cn/learn/396566
https://modelscope.cn/learn/396565
https://modelscope.cn/learn/396564
https://modelscope.cn/learn/396563
https://modelscope.cn/learn/396562
https://modelscope.cn/learn/396561
https://modelscope.cn/learn/396560
https://modelscope.cn/learn/396559
https://modelscope.cn/learn/396558
https://modelscope.cn/learn/396557
https://modelscope.cn/learn/396556
https://modelscope.cn/learn/396555
https://modelscope.cn/learn/396554
https://modelscope.cn/learn/396553
https://modelscope.cn/learn/396552
https://modelscope.cn/learn/396551
https://modelscope.cn/learn/396550
https://modelscope.cn/learn/396549
https://modelscope.cn/learn/396548
https://modelscope.cn/learn/396547
https://modelscope.cn/learn/396546
https://modelscope.cn/learn/396545
https://modelscope.cn/learn/396544
https://modelscope.cn/learn/396543
https://modelscope.cn/learn/396542
https://modelscope.cn/learn/396541
https://modelscope.cn/learn/396540
https://modelscope.cn/learn/396539
https://modelscope.cn/learn/396538
https://modelscope.cn/learn/396537
https://modelscope.cn/learn/396536
https://modelscope.cn/learn/396535
https://modelscope.cn/learn/396534
https://modelscope.cn/learn/396533
https://modelscope.cn/learn/396532
https://modelscope.cn/learn/396531
https://modelscope.cn/learn/396530
https://modelscope.cn/learn/396529
https://modelscope.cn/learn/396527
https://modelscope.cn/learn/396528
https://modelscope.cn/learn/396526
https://modelscope.cn/learn/396525
https://modelscope.cn/learn/396524
https://modelscope.cn/learn/396523
https://modelscope.cn/learn/396522
https://modelscope.cn/learn/396521
https://modelscope.cn/learn/396520
https://modelscope.cn/learn/396519
https://modelscope.cn/learn/396518
https://modelscope.cn/learn/396517
https://modelscope.cn/learn/396516
https://modelscope.cn/learn/396515
https://modelscope.cn/learn/396514
https://modelscope.cn/learn/396513
https://modelscope.cn/learn/396512
https://modelscope.cn/learn/396511
https://modelscope.cn/learn/396510
https://modelscope.cn/learn/396509
https://modelscope.cn/learn/396508
https://modelscope.cn/learn/396507
https://modelscope.cn/learn/396506
https://modelscope.cn/learn/396505
https://modelscope.cn/learn/396504
https://modelscope.cn/learn/396502
https://modelscope.cn/learn/396503
https://modelscope.cn/learn/396501
https://modelscope.cn/learn/396500
https://modelscope.cn/learn/396499
https://modelscope.cn/learn/396498
https://modelscope.cn/learn/396497
https://modelscope.cn/learn/396496
https://modelscope.cn/learn/396495
https://modelscope.cn/learn/396494
https://modelscope.cn/learn/396493
https://modelscope.cn/learn/396492
https://modelscope.cn/learn/396491
https://modelscope.cn/learn/396490
https://modelscope.cn/learn/396489
https://modelscope.cn/learn/396488
https://modelscope.cn/learn/396487
https://modelscope.cn/learn/396486
https://modelscope.cn/learn/396485
https://modelscope.cn/learn/396484
https://modelscope.cn/learn/396483
https://modelscope.cn/learn/396482
https://modelscope.cn/learn/396480
https://modelscope.cn/learn/396481
https://modelscope.cn/learn/396479
https://modelscope.cn/learn/396478
https://modelscope.cn/learn/396477
https://modelscope.cn/learn/396475
https://modelscope.cn/learn/396476
https://modelscope.cn/learn/396474
https://modelscope.cn/learn/396473
https://modelscope.cn/learn/396472
https://modelscope.cn/learn/396471
https://modelscope.cn/learn/396470
https://modelscope.cn/learn/396469
https://modelscope.cn/learn/396468
https://modelscope.cn/learn/396467
https://modelscope.cn/learn/396466
https://modelscope.cn/learn/396465
https://modelscope.cn/learn/396464
https://modelscope.cn/learn/396463
https://modelscope.cn/learn/396462
https://modelscope.cn/learn/396461
https://modelscope.cn/learn/396460
https://modelscope.cn/learn/396457
https://modelscope.cn/learn/396459
https://modelscope.cn/learn/396458
https://modelscope.cn/learn/396456
https://modelscope.cn/learn/396454
https://modelscope.cn/learn/396455
https://modelscope.cn/learn/396453
https://modelscope.cn/learn/396452
https://modelscope.cn/learn/396451
https://modelscope.cn/learn/396450
https://modelscope.cn/learn/396449
https://modelscope.cn/learn/396448
https://modelscope.cn/learn/396447
https://modelscope.cn/learn/396446
https://modelscope.cn/learn/396445
https://modelscope.cn/learn/396444
https://modelscope.cn/learn/396443
https://modelscope.cn/learn/396442
https://modelscope.cn/learn/396441
https://modelscope.cn/learn/396440
https://modelscope.cn/learn/396439
https://modelscope.cn/learn/396438
https://modelscope.cn/learn/396437
https://modelscope.cn/learn/396436
https://modelscope.cn/learn/396435
https://modelscope.cn/learn/396434
https://modelscope.cn/learn/396432
https://modelscope.cn/learn/396433
https://modelscope.cn/learn/396431
https://modelscope.cn/learn/396430
https://modelscope.cn/learn/396429
https://modelscope.cn/learn/396428
https://modelscope.cn/learn/396427
https://modelscope.cn/learn/396426
https://modelscope.cn/learn/396425
https://modelscope.cn/learn/396424
https://modelscope.cn/learn/396423
https://modelscope.cn/learn/396422
https://modelscope.cn/learn/396421
https://modelscope.cn/learn/396420
https://modelscope.cn/learn/396419
https://modelscope.cn/learn/396418
https://modelscope.cn/learn/396417
https://modelscope.cn/learn/396416
https://modelscope.cn/learn/396415
https://modelscope.cn/learn/396414
https://modelscope.cn/learn/396413
https://modelscope.cn/learn/396412
https://modelscope.cn/learn/396411
https://modelscope.cn/learn/396410
https://modelscope.cn/learn/396409
https://modelscope.cn/learn/396408
https://modelscope.cn/learn/396407
https://modelscope.cn/learn/396406
https://modelscope.cn/learn/396405
https://modelscope.cn/learn/396404
https://modelscope.cn/learn/396403
https://modelscope.cn/learn/396402
https://modelscope.cn/learn/396400
https://modelscope.cn/learn/396401
https://modelscope.cn/learn/396399
https://modelscope.cn/learn/396398
https://modelscope.cn/learn/396397
https://modelscope.cn/learn/396396
https://modelscope.cn/learn/396395
https://modelscope.cn/learn/396394
https://modelscope.cn/learn/396393
https://modelscope.cn/learn/396392
https://modelscope.cn/learn/396391
https://modelscope.cn/learn/396390
https://modelscope.cn/learn/396389
https://modelscope.cn/learn/396388
https://modelscope.cn/learn/396387
https://modelscope.cn/learn/396386
https://modelscope.cn/learn/396385
https://modelscope.cn/learn/396384
https://modelscope.cn/learn/396383
https://modelscope.cn/learn/396382
https://modelscope.cn/learn/396381
https://modelscope.cn/learn/396380
https://modelscope.cn/learn/396379
https://modelscope.cn/learn/396378
https://modelscope.cn/learn/396377
https://modelscope.cn/learn/396376
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 组件扫描--> <context:component-scan base-package="com.ali.controller" /> <!-- 配置视图解析器--> <bean id="thymeleafViewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver"> <!-- 设置视图编码字符集--> <property name="characterEncoding" value="UTF-8" /> <!-- 如果配置多个视图解析器,它来决定优先使用哪个视图解析器,值越小优先级越高--> <property name="order" value="1" /> <!-- 使用该模板引擎来解析、编译、渲染模板--> <property name="templateEngine"> <bean class="org.thymeleaf.spring6.SpringTemplateEngine"> <!-- 指定thymeleaf使用的模板解析器。模板解析器负责根据模板位置、模板资源名称、文件编码等信息,加载模板并对其进行解析。--> <property name="templateResolver"> <bean class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver" > <!-- 设置模板文件位置(前缀)--> <property name="prefix" value="/WEB-INF/templates/" /> <!-- 设置模板文件后缀,文件扩展名不一定是html。也可以是其他。比如txt。大部分是html--> <property name="suffix" value=".html" /> <!-- 设置模板类型--> <property name="templateMode" value="HTML" /> <!-- 设置编码字符集--> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> </beans>
配置tomcat,启动tomcat后,访问http://localhost:8080/springmvc/test 。
执行流程总结:
-
springmvc前端控制器DispatcherServlet接收到请求
-
DispatcherServlet根据请求路径/test映射到FirstController,调用该方法
-
FirstController处理请求
-
FirstController返回视图逻辑名称first给视图解析器
-
视图解析器找到/WEB-INF/templates/first.html文件,并进行解析。生成视图解析对象返回给前端控制器DispatcherServlet
-
前端控制器DispatcherServlet响应结果到浏览器。
一个控制器里可以编写多个方法。
实现springmvc的首页功能
在controller里添加跳转首页的方法。请求路径设为"/"
@RequestMapping("/")
public String toIndex(){
return "index";
}
这样就会默认跳转到index.html这个首页里面,因为web.xml文件里配置了/属性。
指定springmvc配置文件的名字和路径
在web.xml文件中。可以如下配置
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定springmvc配置文件的名字是springmvc.xml
指定springmvc配置文件存放的路径是类的根路径-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 在web服务器启动的时候,就初始化DispatcherServlet
这样在第一次访问请求的时候,速度就比较快-->
<load-on-startup>0</load-on-startup>
</servlet>
RequestMapping注解
RequestMapping的作用
@RequestMapping用于将请求映射到相应的处理方法上,可以将指定url的请求绑定到一个方法或者类上,从而实现对请求的处理和相应。
@RequestMapping可以标注在类和方法上。
在同一个web应用中,不允许有2个相同的RequestMapping,否则启动报错。
RequestMapping注解的value属性
value属性填写的是请求路径。value属性是一个字符串数组。表示可以提供多个路径。
也就是说,在springmvc中,可以将多个不同的请求路径映射到一个控制器的同一个方法上。
@RequestMapping({"/test01","/test_01","test1"})
public String testRequest(){
return "test_01";
}
ant风格的value(路径支持模糊匹配)
ant风格的value支持路径模糊匹配。关于路径中的通配符包括:
-
?:代表任意一个字符(除/ ? 之外的其他字符,不能空着)
-
*:代表0到n个任意字符(除/ ? 之外的其他字符)
-
** :代表0到n个任意字符,并且路径中可以出现路径分隔符/
注意: 通配符在使用时,左右不能出现字符,只能是/(spring5及以前的版本适用,spring6会报错)**
在spring6中 通配符只能出现在末尾。比如:/testAnt/****
@RequestMapping("/x?y/test")
public String testAntValue(){
return "test_ant";
}
value中的占位符
// restful风格的url
@RequestMapping("/test/restful/{id}/{name}")
public String testRestful(@PathVariable("id") int id,
@PathVariable("name") String name){
return "test_restful";
}
RequestMapping注解的method属性
用来指定请求方式。
method属性是一个数组,所以可以设置多种请求方式。
// 请求路径是/test/login,且请求方式是post
@RequestMapping(value = "/test/login", method = RequestMethod.POST)
public String testLogin(){
return "test_login";
}
衍生的Mapping
@PostMapping 默认采用post方式处理。
@GetMapping 默认采用get方式处理。
web的请求方式
- get(查):获取资源,不影响数据的状态和功能。使用url中传递参数,或者在HTTP请求的头部使用参数,服务器返回请求资源。
- post(增):向服务器提交资源,可能会改变数据的状态和功能。通过表单方式提交请求体。服务器接收请求体后,进行数据处理。
- put(改):更新资源,用于更新指定的资源上所有可编辑内容。通过请求体发送需要被更新的全部内容,服务器接收数据后,将被更新的资源进行修改或替换。
- delete(删):删除资源。用于删除指定的资源。将要被删除的资源标识符放在url中或请求体中。
- head:请求服务器返回资源的头部,与get类似。但是返回的信息是头部信息,不能包含数据体。主要用于资源检测和缓存控制。
- patch:部分更改请求。当请求的资源是可被更改的资源时,请求服务器对该资源进行部分更新,即每次更新一部分。
- options:请求获得服务器支持的请求方法类型。以及支持的请求头标志。"OPTIONS"则返回支持全部方法类型的服务器标志。
- trace:服务器响应客户端HTTP请求,主要用于调试和测试。
- connect:建立网络连接。通常用于加密ssl/TLS连接。
get与post的区别
| get | post |
|---|---|
| 数据会挂在url后面,并且在url后面添加一个?。数据会回显在浏览器地址栏 | 数据在请求体中,不会回显在浏览器地址栏 |
| 只能发送普通字符串,并且有长度限制,不同的浏览器限制不同 | 可以发送任何类型数据,包括字符串、流媒体等信息。理论上没有长度限制 |
| 适合从服务端获取数据 | 适合向服务端传送数据 |
| 请求是安全的,因为是获取数据 | 危险的。因为是修改服务器资源 |
| 支持缓存,二次请求会走浏览器缓存,不去请求真正的服务器 | 不支持缓存 |
RequestMapping注解的params属性
params用来设置请求参数映射。
params属性也是一个数组,要求请求参数必须和params数组中要求的所有参数完全一致后,才能映射成功。

RequestMapping注解的headers属性
headers和params原理相同。用法也相同。
当前端提交的请求头信息和后端要求的请求头信息一致时,才能映射成功。
// 请求路径是/test/headers,且请求头中必须包含Referer和Host
@RequestMapping(value = "/test/headers", headers = {"Referer", "Host"})
public String testHeaders(){
return "test_headers";
}