SpringMVC—RequestMapping注解

news2024/10/6 18:32:48

一、RequestMapping注解

@RequestMapping注解:是Spring MVC框架中的一个控制器映射注解,用于将请求映射到相应的处理方法上,具体来说,他可以将指定URL的请求绑定到一个特定的方法或类上,从而实现对请求的处理和响应。

1.RequestMapping的value属性

package com.pon;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Order {
    @RequestMapping(value = {"/e","/b"})
    public String o(){
        return "index";
    }
}

多个vlue属性在同一RequestMapping上,可以作为同一地址·。

控制类:

package com.pon;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Order {
    @RequestMapping(value = {"/e","/b"})
    public String o(){
        return "index";
    }
}

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>你好</h1>
</body>
</html>

 

1)Ant风格的value

3)value使用占位符

URL使用RESTFul风格:

package com.pon;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Order {
    @RequestMapping(value = {"/e","/b"})
    public String o(){
        return "index";
    }
    @RequestMapping(value = "/login/{username}/{password}")//value的占位符
    public String log(
            @PathVariable("username")
            String username,
            @PathVariable("password")
            String password){
        System.out.println("用户名"+username+"密码"+password);
        return "index";
    }
}

处理器端:

2.RequestMapping的method属性

超链接发送的请求方式为get请求。

在controller包下的Hello类:

package com.pon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Hello {
    @RequestMapping("/")
    public String first(){
        return "first";
    }
     //请求映射只支持post请求
    @RequestMapping(value = {"/ljx"},method = {RequestMethod.POST})
    public String hello(){
        return "hi";
    }
}

first.xml:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/ljx}">Hello界面</a>
</body>
</html>

hi.xml:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>你好 MVC</h1>
</body>
</html>

@RequestMapping的派生注解

验证GetMapping()

在controller包下的Hello类:

@Controller
public class Hello {
    @RequestMapping("/")
    public String first(){
        return "first";
    }
    @RequestMapping(value = {"/ljx"},method = {RequestMethod.POST})
    public String hello(){
        return "hi";
    }
    @GetMapping("/get")
    public String getMapping(){
        return "hi";
    }
}

first.xml:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/lix}">Hello界面</a>
<a th:href="@{/get}">Get界面</a>
</body>
</html>

hi.xml:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>你好 MVC</h1>
</body>
</html>

3.RequestMapping的params属性

params在requestmapping中赋值和method在同一位置。

4.RequestMapping的headers属性

二、SpringMvc获取请求参数 

1通过ServletAPI获取

testContorller类下:

package com.pon.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
   @RequestMapping("/parama")
    public String test(HttpServletRequest request){
      String s= request.getParameter("username");
      String r= request.getParameter("password");
      System.out.println("name="+s+",password="+r);
      return "test";
   }
}

 先访问首页的first.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<form th:action="@{/format}">
    <input type="submit" value="表单"><br>
</form>
<a th:href="@{/parama(username='admin',password=123456)}">获取请求参数</a>
</body>
</html>

test.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>获取到请求参数</h1>
</body>
</html>

 

2通过控制器方法的形参获取请求参数

 testContorller类下:

@Controller
public class TestController {
@RequestMapping("/testparam")
   public String testparam( String username,  String password){
       System.out.println("username"+username+"password"+password);
       return "hi";
   }
}

first.html页面中调用/testparam路径:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<form th:action="@{/format}">
    <input type="submit" value="表单"><br>
</form>
<a th:href="@{/parama(username='admin',password=123456)}">获取请求参数</a>
<a th:href="@{/testparam(username='adminmm',password=1234)}">控制器方法获取请求参数</a>
</body>
</html>

hi.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>你好 MVC</h1>
</body>
</html>

3@RequestParam

当HTML中的属性名与获取请求参数中的名字不一样,需要使用@RequestParam。

例如:

在控制器类中:

@Controller
public class TestController {
@RequestMapping("/testparam")
   public String testparam( String username,  String password){
       System.out.println("username:"+username+",password:"+password);
       return "hi";
   }
}

首页first.html界面:

<a th:href="@{/testparam(usern='adminmm',password=1234)}">控制器方法获取请求参数</a><br>

当参数不一样时,页面可以正常访问,但控制台获取不到参数。

解决方案,在控制类的构造函数上,添加@RequestParam注解,其他参数不变。

@Controller
public class TestController {
@RequestMapping("/testparam")
   public String testparam(@RequestParam("usern")String username, String password){
       System.out.println("username:"+username+",password:"+password);
       return "hi";
   }
}

4@RequsetHeader

请求头信息其实就是:localhost:8080

5@CookieValue

6通过实体类形参获取请求参数 

在首页first.html中定义一个表单:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<form th:action="@{/pojo}" method="post">
    用户名:<input type="text" name="username"><br>
    密码  :<input type="password" name="password"><br>
    性别:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
    年龄:<input type="text" name="age"><br>
    <input type="submit" value="实体类请求参数">
</form>
</body>
</html>

根据表单的属性创建一个实体类对象User:

package com.pon.pojo;
public class User {
    private String username;
    private String password;
    private String sex;
    private Integer age;
    public User() {
    }
    public User(String username, String password, String sex, Integer age) {
        this.username = username;
        this.password = password;
        this.sex = sex;
        this.age = age;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

在控制类中写访问页面的路径:

@Controller
public class TestController {
 @RequestMapping("/pojo")
    public String Pojo(User user){
       System.out.println(user);
       return "hi";
   }
}

hi.xml:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success</h1>
</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1836620.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

充电学习—7、BC1.2 PD协议

BC1.2&#xff08;battery charging&#xff09;充电端口识别机制&#xff1a; SDP、CDP、DCP 1、VBUS detect&#xff1a;vbus检测 PD&#xff08;portable device&#xff0c;便携式设备&#xff09;中有个检测VBUS是否有效的电路&#xff0c;电路有个参考值&#xff0c;高…

高级算法复习指南

自用 如果帮到了你就点个关注和赞吧&#xff01; 1.算法知识回顾 求两个不全为0的非负整数m和n的最大公约数 数据结构回顾 数组 连续内存分配、静态大小、类型一致、索引访问、访问时间相同、边界检查 多维数组&#xff1a;数组的数组&#xff0c;可以是连续的&#xff0c…

不重新安装Anaconda找回不见的Anaconda Prompt

找回Anaconda Prompt只需三步 系统&#xff1a;win11 x641.cd Anaconda的安装目录2. Anaconda Prompt又回来了 系统&#xff1a;win11 x64 1.cd Anaconda的安装目录 winR 输入cmd 进入命令行&#xff0c;进入到Anaconda的安装目录 eg&#xff1a;我的Anaconda安装在&#xff…

Ptrade和QMT的区别,怎么获取合适的量化交易软件?

​Ptrade和QMT的适用人群 交易活跃用户 量化爱好者已经专业量化投资者 高净值个人或机构 Ptrade和QMT的区别 回测和交易频率 Ptrade回测和交易只支持分钟级和日线级别的频率&#xff0c;而QMT支持tick级、分钟级、5分钟级、10分钟级、日线、周线、月线等。 使用QMT进行回…

GDB调试相关教程

GDB调试相关教程 相关参考链接 https://wizardforcel.gitbooks.io/100-gdb-tips/content/set-step-mode-on.htmlhttps://wizardforcel.gitbooks.io/100-gdb-tips/content/set-step-mode-on.html 设置程序运行参数 命令set args 10 20 30 40 使用show args显示设置好的运行参…

线性稳压器LDO的基础知识

一、什么是线性稳压器? 线性稳压器的工作原理是&#xff1a;采用一个压控电流源以强制在稳压器输出端上产生一个固定电压。控制电路连续监视&#xff08;检测&#xff09;输出电压&#xff0c;并调节电流源&#xff08;根据负载的需求&#xff09;以把输出电压保持在期望的数值…

(微服务实战)聚合支付系统商户线上聚合收银台接口设计

1 概述 聚合支付收款分为线上和线下业务场景&#xff0c;本文中的商户收银台接口设计主要是指线上业务场景&#xff0c;线下业务场景聚合收款方式后续会进行单独设计和分析。 主流的线上支付渠道有微信支付&#xff0c;支付宝支付&#xff0c;云闪付。这三种支付渠道都有各自…

JupyterLab使用指南(五):JupyterLab的 扩展

1. 什么是JupyterLab的扩展 JupyterLab 扩展&#xff08;Extension&#xff09;是一种插件机制&#xff0c;用于增强 JupyterLab 的功能。通过安装扩展&#xff0c;用户可以添加新的功能、改进现有功能&#xff0c;甚至自定义界面和工作流。扩展可以覆盖各种用途&#xff0c;从…

零售门店收银系统源码

智慧新零售系统是一套线下线上一体化的收银系统。致力于给零售门店提供『多样化线下收银』、『ERP进销存』、『o2o小程序商城』、『精细化会员管理』、『丰富营销插件』等一体化行业解决方案&#xff01; 一、多样化线下收银 1.聚合收款码 ①适用商户&#xff1a;小微门店&am…

QT day4(对话框 事件机制)

1&#xff1a;思维导图 2&#xff1a; #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);ui->setupUi(this);//去除头部this->setWindowFlag(Qt::Frameles…

【docker 如何自定义镜像】

查看容器列表 首先是查看容器&#xff1a;在命令台中键入 docker ps -a 命令&#xff0c;得到如下界面。 从容器创建一个新镜像 接着&#xff0c;dockers commit 容器名 要保存成的镜像名&#xff1a;版本名&#xff08;若没有 &#xff1a;版本名 则直接默认为latest&#x…

openwrt如何安装python

首先配置opkg源。 # 备份初始conf mv /etc/opkg.conf /etc/opkg.conf.bak # 新建配置 vim /etc/opkg.conf # 添加如下内容&#xff1a; dest root / dest ram /tmp lists_dir ext /var/opkg-lists option overlay_root /overlay # notice dest usb /mnt/sdb1/opkg arch all 100…

LeetCode739每日温度

题目描述 给定一个整数数组 temperatures &#xff0c;表示每天的温度&#xff0c;返回一个数组 answer &#xff0c;其中 answer[i] 是指对于第 i 天&#xff0c;下一个更高温度出现在几天后。如果气温在这之后都不会升高&#xff0c;请在该位置用 0 来代替。 解析 每次往栈中…

简易计算器需求报告

1. &#xff08;简易计算器&#xff09; 需求说明书 文件编号&#xff1a;2022[1] [木柚2] 06[3] [木柚4] 01[5] [木柚6] 完成日期&#xff1a;2024年 06月18日 编制&#xff1a; 易正阳 日期&#xff1a;2024年6月18日 审核&#xff1a;张正 日期&#xff1a;2024年6月18…

【免费API推荐】: 解锁创意无限,享受免费开发之旅

幂简网站上免费的 API 分类内汇集了各种各样的免费 API&#xff0c;涵盖了多个领域和功能。无论你是在构建网站、开发应用还是进行数据分析&#xff0c;这个项目都能为你提供丰富的选择。 幂简集成搜集了网络上免费的 API 资源&#xff0c;为广大开发者和创业者提供便捷的访问渠…

浏览器渲染机制:重排(Reflow)与重绘(Repaint)以及Vue优化策略

浏览器渲染机制是一个复杂但有序的过程&#xff0c;其目的是将HTML、CSS和JavaScript代码转化为用户可以看到和交互的视觉界面。重排&#xff08;Reflow&#xff09;与重绘&#xff08;Repaint&#xff09;是浏览器渲染过程中对页面元素进行更新的两个重要步骤&#xff0c;理解…

文献学习——PWM - PFM模式无缝转换的PFC变换器统一控制

PWM - PFM模式无缝转换的PFC变换器统一控制 摘要&#xff1a;断续导通模式通常应用在升压功率因数校正转换器中。这篇文章提出了一种基于虚拟阻抗原理的实现脉冲宽度调制控制和脉冲频率调制控制的统一控制方法。控制模式可以简单的通过只调整一个控制参数来改变。因此&#xf…

基于Spring Boot+VUE旧物置换网站

1前台首页功能模块 旧物置换网站&#xff0c;在系统首页可以查看首页、旧物信息、网站公告、个人中心、后台管理等内容&#xff0c;如图1所示。 图1系统功能界面图 用户注册&#xff0c;在用户注册页面通过填写用户名、密码、姓名、性别、头像、手机、邮箱等内容进行用户注册&…

Airbind - hackmyvm

简介 靶机名称&#xff1a;Airbind 难度&#xff1a;中等 靶场地址&#xff1a;https://hackmyvm.eu/machines/machine.php?vmAirbind 本地环境 虚拟机&#xff1a;vitual box 靶场IP&#xff08;Airbind&#xff09;&#xff1a;192.168.56.121 跳板机IP(windows 11)&…

threejs教程:绘制3D地图(广东省区划图)

一、效果展示&#xff1a; 二、开发准备 Three.js中文文档&#xff1a;Three.js中文网 Three.js文本渲染插件&#xff1a;Troika 3D Text - Troika JS 行政区划边界数据查询&#xff08;阿里云数据可视化平台&#xff09;&#xff1a;DataV.GeoAtlas地理小工具系列 1. 在项目…