SpringMVC框架搭建

SpringMVC框架搭建

快速开启SpringMVC🚀🚀

步骤

1、新建模块
2、引入相关依赖
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.1</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>5.0.0</version>
</dependency>
3、配置web.xml
<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">
  <!--注册前端控制器DispatcherServlet-->
  <servlet>
    <servlet-name>dispatchServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置初始化参数-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatchServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
4、创建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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--扫描组件-->
    <context:component-scan base-package="com.louis.controller"></context:component-scan>
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                     </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>
5、创建templates文件夹

在这里插入图片描述

6、创建页面index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
    <title>首页</title>
</head>
<body>
<h2>首页</h2>
</body>
</html>
7、创建控制器
@Controller
public class TestController {
    @RequestMapping("/")
    public String toFirstPage(){
        return "index";
    }
}

在这里插入图片描述

8、测试

在这里插入图片描述

@RequestMapping注解

注解功能

将请求和处理请求的控制器方法关联起来,建立映射关系。

SpringMVC接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。如果有多个控制器,并且@RequsetMapping请求的路径是一样的情况,会报异常。所以在控制器中需要保证请求路径是唯一的。

使用位置

@RequestMapping注解定义时,它的作用范围如下:

@Target({ElementType.TYPE, ElementType.METHOD})
  • 标识一个类:设置映射请求路径的初始信息。
  • 标识一个方法:设置映射请求路径的具体信息
实例

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
    <title>首页</title>
</head>
<body>
<h2>首页</h2>
<a th:href="@{/test/testRequestMapping}">跳转</a>
</body>
</html>

跳转页面的控制器

@Controller
@RequestMapping("/test")
public class TestRequestMapping {

    @RequestMapping("/testRequestMapping")
    public String testTwoRequestMapping(){
        return "seccess";
    }
}
总结

当一个类中既包含了标识类的RequestMapping又包含了标识方法的RequestMapping,首先会匹配到类标识,然后再到类的方法上。所以需要在访问该页面路径下添加标识类的value值。经常用于不同模块的设置控制器。


@RequestMapping注解的属性

在这里插入图片描述

value属性

通过请求的请求地址来匹配,它是一个字符串数组,可以匹配多个请求多个请求地址,只需要符合其中的一个请求地址就表示匹配成功。必须设置。只设置value属性且只有一个值时,"value ="可以省略。

controller

@Controller
public class TestRequestMapping {
    @RequestMapping(value = {"/test", "/testRequestMapping"})
    public String testTwoRequestMapping(){
        return "seccess";
    }
}

index.html

<a th:href="@{/testRequestMapping}">跳转/testRequestMapping</a>
<a th:href="@{/testRequestMapping}">跳转/test</a>

method属性

通过请求方式来匹配请求,(get/post)匹配请求映射。

controller

@Controller
public class TestRequestMapping {
    @RequestMapping(value = {"/test", "/testRequestMapping"}, method = {RequestMethod.GET})
    public String testTwoRequestMapping(){
        return "seccess";
    }

index.html

<form th:action="@{/test}" method="post">
    <input type="submit" value="测试RequestMapping注解的method属性--->POST">
</form>

method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求,若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报如下错:

405:Request method 'GET/POST'not supported

在这里插入图片描述

如果想要既满足POST请求方式又满足GET请求方式,可以修改为:

method = {RequestMethod.GET, RequestMethod.POST}

若不设置method,表示不以method请求方式为条件。任何的请求方式都能够匹配。

常用请求方式

常用的请求方式有GETPOSTPUTDELETE但是目前浏览器只支持get和post,若在form表单提交时,为method设置了其他请求方式的字符串(put或delete),则按照默认的请求方式get处理。

index.html

<form th:action="@{/testPut}" method="put">
    <input type="submit" value="测试form表单是否能够发送put或delete请求--->put">
</form>

controller

  @RequestMapping(value = "/testPut", method = {RequestMethod.PUT})
  public String testPut(){
      return "success";
  }

在这里插入图片描述

在选择form的action的时候,只会有两个选项,get/post,如果我们选择了其他的内容,form会按默认的请求方式(POST)来算。

@RequestMapping的派生注解

对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解。

处理get请求的映射:@GetMapping

index.html

<a th:href="@{/testGetMapping}">测试GetMapping注解</a>

controller

@GetMapping("/testGetMapping")
public String testGetMapping(){
    return "success";
}

在这里插入图片描述

在这里插入图片描述

此外,还有处理post请求的映射:@PostMapping、处理put请求的映射:@PutMapping、处理delete请求的映射:@DeleteMapping

param属性

通过请求参数匹配请求,可以设置多个,但是必须同时满足才能映射。(了解)
@RequestMapping的params属性通过请求的请求参数匹配请求映射,它是一个字符串类型的数组,可以通过四种表达方式请求参数和请求映射的匹配关系:

  • “param”:要求请求映射所匹配的请求必须携带param请求参数
  • “!param”:要求请求映射所匹配的亲求必须不携带param的请求参数
  • “param=value”:要求请求映射所匹配的请求必须携带param请求参数且param=value
  • “param!=value”:要求请求映射所匹配的请求必须携带param请求参数但是param!=value

controller

@RequestMapping(value = "/testParamsAndHeaders",
        //表示当前的请求映射匹配的请求必须携带username请求参数
        params = {"username='user'"})
public String testParams(){
    return "success";
}

index.html

<!--方式一:-->
<!--<a th:href="@{/testParamsAndHeaders(username='user'&pwd=1234)}">测试RequestMapping的Params属性</a>-->
<!--方式二:-->
<a th:href="@{/testParamsAndHeaders?username='user'&pwd=1234}">测试RequestMapping的Params属性</a>

在这里插入图片描述

如果不携带参数

<a th:href="@{/testParamsAndHeaders}">测试RequestMapping的Params属性</a>

在这里插入图片描述

hander属性

通过请求头信息匹配请求(了解)。
@RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射,它是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系:

  • “header”:要求请求有映射所匹配的请求必须携带header请求头信息
  • “!header”:要求请求映射所匹配的请求必须不携带header请求头信息
  • “header=value”:要求请求映射所匹配的请求必须携带header请求头信息且header=value
  • “header!=value”:要求请求映射所匹配的请求必须携带header请求头信息且header!=value
    若当前@RequestMapping注解的value和method属性满足,但是不满足headers属性,此时页面会显示404错误。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值