SpringMVC(二)之注解以及参数封装

SpringMVC 注解实现参数映射 SpringMVC 注解实现参数映射1. 请求参数映射1.1 RequestMapping1.2 请求参数的映射1.2.1 基本类型/字符串属性封装1.2.2 实体类属性封装1.2.3 集合属性封装1.2.4 Date类型封装1.3 常用注解1.4 其他注解2. 响应数据和结果视图2.1 返回值分类3. 配置不拦截静态资源 参考资料:https://spring-mvc.linesh.tw/ 1. 请求参数映射 @RequestMapping 注解的作用是建立请求 URL 和处理方法之间数据的对应关系。 阅读详情
通过示例项目,学习SpringMVC的注解以及参数封装

创建web项目以及导入jar包

项目结构如图
这里写图片描述
这里写图片描述

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Springmvc02</display-name>

  <filter>
    <filter-name>characterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>characterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 默认加载方式
       默认加载必须规范:
       * 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
       * 路径规范:必须在WEB-INF目录下面
    -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- 扫描注解 -->
    <context:component-scan base-package="com.zyj"></context:component-scan>

    <!-- 配置注解处理器映射器 
        负责寻找Controller
    -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
    <!-- 配置注解处理器处理器 
        负责执行Controller
    -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

    <!-- 配置视图解析器 
        后台返回逻辑试图:index
        视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

User.java

public class User {
    private int id;
    private String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
    //省略getter和setter方法
}

UserCustom.java

// 用户包装类
public class UserCustom {
    // 用户
    private User user;
    // List集合
    private List<User> userList;
    // Map集合
    private Map<String, Object> map = new HashMap<String, Object>();
    //省略getter和setter方法
}

UserController.java

@Controller
@RequestMapping("/user")//配置根路径,目的是解决多个Controller中出现相同的RequestMapping的冲突
public class UserController {

    // 跳转页面
    @RequestMapping("toAddUser")
    public String toAddUser() {
        return "addUser";
    }

    // 接收int类型和string类型参数
    @RequestMapping("receiveIntAndString")
    public String receiveIntAndString(int id, String username) {
        System.out.println(id+","+username);
        return "success";
    }

    // 接收po类型参数
    @RequestMapping("receiveUser")
    public String receiveUser(User user) {
        System.out.println(user);
        return "success";
    }

    // 接收包装类型参数
    @RequestMapping("receiveUserCustom")
    public String receiveUserCustom(UserCustom userCustom) {
        System.out.println(userCustom.getUser());
        return "success";
    }

    // 接收数组类型参数
    @RequestMapping("receiveArray")
    public String receiveArray(Integer[] ids) {
        for (Integer integer : ids) {
            System.out.println(integer);
        }
        return "success";
    }

    // 接收list集合类型参数
    @RequestMapping("receiveUserCustomList")
    public String receiveUserCustomList(UserCustom userCustom) {
        System.out.println(userCustom.getUserList());
        return "success";
    }

    // 接收map集合类型参数
    @RequestMapping("receiveUserCustomMap")
    public String receiveUserCustomMap(UserCustom userCustom) {
        System.out.println(userCustom.getMap());
        return "success";
    }
}

addUser.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/user/receiveIntAndString.do" method="POST">
        ID:<input type="text" name="id" />
        用户名:<input type="text" name="username" />
        <input type="submit" value="提交多个普通类型参数"/>
    </form>
    <hr color="grey" size="10" />
    <form action="${pageContext.request.contextPath}/user/receiveUser.do" method="POST">
        ID:<input type="text" name="id" />
        用户名:<input type="text" name="username" />
        性别:<input type="text" name="sex" />
        生日:<input type="text" name="birthday" />
        地址:<input type="text" name="address" />
        <input type="submit" value="提交po类型参数"/>
    </form>
    <hr color="grey" size="10" />
    <form action="${pageContext.request.contextPath}/user/receiveUserCustom.do" method="POST">
        ID:<input type="text" name="user.id" />
        用户名:<input type="text" name="user.username" />
        性别:<input type="text" name="user.sex" />
        生日:<input type="text" name="user.birthday" />
        地址:<input type="text" name="user.address" />
        <input type="submit" value="提交包装类型参数"/>
    </form>
    <hr color="grey" size="10" />
    <form action="${pageContext.request.contextPath}/user/receiveArray.do" method="POST">
        ID:<input type="checkbox" name="ids" value="1"/>
        ID:<input type="checkbox" name="ids" value="2"/>
        ID:<input type="checkbox" name="ids" value="3"/>
        ID:<input type="checkbox" name="ids" value="4"/>
        ID:<input type="checkbox" name="ids" value="5"/>
        <input type="submit" value="提交数组类型参数"/>
    </form>
    <hr color="grey" size="10" />
    <form action="${pageContext.request.contextPath}/user/receiveUserCustomList.do" method="POST">
        ID:<input type="text" name="userList[0].id" />
        用户名:<input type="text" name="userList[0].username" />
        ID:<input type="text" name="userList[1].id" />
        用户名:<input type="text" name="userList[1].username" />
        <input type="submit" value="提交List类型参数"/>
    </form>
    <hr color="grey" size="10" />
    <form action="${pageContext.request.contextPath}/user/receiveUserCustomMap.do" method="POST">
        ID:<input type="text" name="map['id']" />
        用户名:<input type="text" name="map['username']" />
        <input type="submit" value="提交Map类型参数"/>
    </form>
</body>
</html>

页面

这里写图片描述

RequestMapping

  1. RequestMapping(“hello”)(推荐,不需要设置后缀名)
  2. RequestMapping(“/hello.do”)
  3. RequestMapping(value=”/hello.do”)
  4. RequestMapping(value=”/hello.do”,method=RequestMethod.GET)
  5. RequestMapping(value=”/hello.do”,method=RequestMethod.POST)
    浏览器直接访问,a标签都是get请求
    表单提交(指定post),ajax指定post提交,post提交。
  6. RequestMapping(value=”/hello.do”,method={RequestMethod.POST,
    RequestMethod.GET})

RequestMaping根路径

解决多个Controller中出现相同的RequestMapping时的冲突
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("save")
    public String save() {
        return "success";
    }
}
@Controller
@RequestMapping("/orders")
public class OrdersController {
    @RequestMapping("save")
    public String save() {
        return "success";
    }
}
访问:
http://localhost:8080/项目名/user/save.do
http://localhost:8080/项目名/orders/save.do

上一篇:《SpringMVC(一)之入门》
下一篇:《SpringMVC(三)之数据回显、url模板映射、转发和重定向》

Spring MVC--6.封装参数获得与传递 立即下载

相关推荐

2024最新Typora配置指南:从安装到打造学术写作环境(含LaTeX/参考文献解决方案)

本文提供2024年最新Typora配置指南,详细讲解如何从安装开始,结合Pandoc、Zotero和LaTeX,打造高效的学术写作环境。指南涵盖Typora深度定制、学术主题配置、参考文献自动管理以及一键生成出版级PDF的完整解决方案,旨在帮助科研人员和学生实现内容与格式分离,大幅提升论文写作效率。

weixin_29258297的博客 210

SpringMVC工作原理之参数解析

前面分析到 SpringMVC工作原理之处理映射[HandlerMapping] ,由映射处理器(HandlerMapping) 解析出对应的 handler。接着 SpringMVC工作原理之适配器[HandlerAdapter] 描述了 handler 是怎么匹配到合适的适配器,进行 handler 对应方法的执行。其他几种适配器还好,但是 RequestMappingHandlerAdapter 适配器对应接下来的参数解析及绑定并执行并不是那么简单,因此本篇笔记主要分析 RequestMapping.

AlbenXie的博客 1338

快速上手CH340N电路设计(CH340N USB转串口模块 USB Type-C接口 CH340系列芯片讲解)

一、上模块 二、功能分析 l 芯片:CH340N l 输入接口:USB、TYPE-C l 输出接口:TTL(5V\3.3V\GND\TX\RX) l 指示灯:电源、TX、RX 三、CH340x芯片 l CH340 是一个 USB 总线的转接芯片,实现 USB 转串口 l 全速 USB 设备接口,兼容 USB V2.0 l 硬件全双工串口,内置收发缓冲区,支持通讯波特率 50bps~2Mbps l 支持常用的 MODEM 联络信号 RTS、DTR、DCD、RI、DS...

东小东博客 5万+

Spring MVC各种参数进行封装

Spring MVC各种参数获取

qq_53317005的博客 7187

45个经典Linux面试题(非常详细)零基础入门到精通,收藏这一篇就够了

(文件系统分配其中的一些磁盘块用来记录它自身的一些数据,如 i 节点,磁盘分布图,间接块,超级块等。$ chmod 751 file 给 file 的属主分配读、写、执行(7)的权限,给 file 的所在组分配读、执行(5)的权限,给其他用户分配执行(1)的权限。linux命令’hash’管理着一个内置的哈希表,记录了已执行过的命令的完整路径, 用该命令可以打印出你所使用过的命令以及执行的次数。创建文件:典型的如 touch,vi 也可以创建文件,其实只要向一个不存在的文件输出,都会创建文件。

小陈的博客 2069

AWS-解析mysql binlog同步数据方案

虽然是公有云的鼻祖,AWS在某些产品的实现却太不给力;可能是习惯了阿里云喂到嘴边的感觉,AWS很多方案需要自己折腾,蛋疼!比如这里要讲的mysql数据同步方案。 阿里云产品DTS,点几下就OK了,AWS,只能通过插件安装、配置、调试、蹚坑……,具体就是用msk的debezium插件连接器。没见识过的一脸懵逼,下面说下配置步骤和一些注意的坑。

sdmei 1406

利用JAVA的AOP编译时注解实现领域实体对象的通用封装

在JAVA开发中,针对某一些场景,AOP(面向切面编程)往往可以带来另外一种巧妙的解决方式。这里主要是摘取了在DDD(领域驱动设计)实践中的领域对象基础公共实现,来分享一些预编译时注解的开发经验。

2301_78901807的博客 489

搭建自己的MQTT服务器,实现设备上云(Ubuntu+EMQX)

这篇文章教大家在ECS云服务器上部署EMQX,搭建自己私有的MQTT服务器,配置EMQX实现设备上云,设备数据转发,存储;服务器我采用的华为云的ECS服务器,系统选择Ubuntu系统。

2万+

springmvc工作原理(源码解析)

一spring-web工作原理 1.1 tomcat & springmvc配置 SpringMVC在Java后端中,是一个很重要也很常用的框架,本篇来分析一下SpringMVC的源码。web.xml配置,虽然使用SpringMVC不需要我们写Servlet,但SpringMVC是封装了Servlet,提供 DispatcherServlet 来帮我们处理的。所以需要在 web.xml 配置 DispatcherServlet。可以看出 DispatcherServlet,映射的ur...

worn_xiao的博客 1442

SAP UI5 百科全书的目录

笔者 12 年 SAP UI5 开发岁月中写下的 595 篇 SAP UI5 学习笔记,全部收录到《SAP UI5 百科全书》专栏中去,目录如下。请大家按照自己的学习需要,根据关键字搜索到对应的文章,然后进行学习。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3826

网络流量异常信息分析方法研究

本课题针对网络安全中网络流量异常的分析方法进行研究,提出一种基于机器学习和深度学习的异常检测算法。该算法采用卷积神经网络(CNN)和长短时记忆神经网络(LSTM)结合的方式,通过训练得到二分类模型,从而实现对网络流量异常的检测。随着互联网的飞速发展,网络攻击事件日益增多,网络安全问题成为了亟待解决的问题之一。常见的网络攻击手段包括 DDoS 攻击、SQL 注入、XSS 攻击等,这些攻击手段可能导致网络系统发生异常,给网络运维、数据安全带来极大的挑战。

肥晨开发的学习之路 1035

springmvc 响应封装_springMVC的参数绑定和返回值类型封装处理原理解析

1.进行方法参数值的解析的接口是HandlerMethodArgumentResolver下面是一些具体的实现类,它们针对不同的注解实现不同的解析绑定功能:1.RequestParamMethodArgumentResolver支持带有@RequestParam注解的参数或带有MultipartFile类型的参数2.RequestParamMapMethodArgumentResolver支持...

weixin_39801165的博客 551

【Windows Server 2019】活动目录 (Active Directory) ——将计算机加入域和脱离域

博文介绍了如何将计算机加入AD域和脱离AD域,包括加入域,验证客户机加入到域后的信息,使用域账户用户在客户机上登录,使用本地账户用户在计算机上登录,脱离域。

VMware, Windows, Linux, Cisco, Container, Kubernetes, Kali 6169

二、@RequestMapping注解和参数的获取

1、@RequestMapping @RequestMapping(value="/testRequestMapping", method={RequestMethod.POST}, params = {"username=jack"}, headers = {"Accept"}) value和path的作用都是指定请求路径,当有注解后面只有一个参数的时候可以直接写路...

a2231476020的博客 7496

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

SpringMVC--ModelAttribute注解的使用

Spring MVC中@ModelAttribute注解的使用 通过 org.springframework.web.bind.annotation.ModelAttribute 注解类型可经常实现以下两个功能: 1)绑定请求参数到实体对象(表单的命令对象) @RequestMapping("/register") public String register(@ModelAttribute("u...

吴声子夜歌的博客 751

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

SpringMVC两种接收GET参数的方法

接收Date类型数据当客户端发起这样的请求时,服务端如何接收其中的日期参数settleDate呢:http://localhost:8080/calculate/1?settleDate=2016-10-30以前我们基本上是用一个字符串来接收这个参数,然后在代码中手动将其转换为Date类型。如:publicvoidtest(@PathViriablelendId,@...

weixin_34341229的博客 1258

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

SpringMVC常用注解及其介绍

SpringMVC常用注解及其介绍 前言:在介绍SpringMVC常用注解之前,有兴趣的可以先了解一下SpringMVC的工作流程。SpringMVC工作流程详解 OK,接下来进入本次主题。 在实际项目中,我们最常用的几个注解,包括 @Controller、@RestController、 @RequestMapping、@PathVariable、@RequestParam 以及 @Reques...

weixin_45139929的博客 1万+

Springmvc的注解开发与封装参数

注解开发 创建一个web工程,并导入jar 配置web.xml &amp;lt;filter&amp;gt; &amp;lt;filter-name&amp;gt;characterEncoding&amp;lt;/filter-name&amp;gt; &amp;lt;filter-class&amp;gt;org.springframework.web.filter.CharacterEncodingFilter&am

weixin_41653442的博客 751

SpringMVC常用注解超详解

文章目录SpringMVC常用注解超详解✨✨✨1.@RequestMapping注解1.1@RequestMapping注解的功能1.2@RequestMapping注解的位置1.3@RequestMapping注解的value属性1.4@RequestMapping注解的method属性1.5@RequestMapping注解的params属性(了解)1.6@RequestMapping注解的headers属性(了解)1.7SpringMVC支持路径中的占位符(@PathVariable)(重点)2.Sp

小皮皮LL的博客 6705

SpringMVC 通过注解进行参数注入

使用SpringMVC已经有好长一段时间,SpringMVC最独特的方法莫过于对参数的封装进行注入(在方法上面直接写参数进行自动注入),但是一直没深入到里面了解其细节,最近在一个项目中看到java注解结合SpringMVC对参数进行注入,感觉特别不错。         众所周知,在方法中能对基本数据类型,Http对象,对象类型进行封装。这个过程是如何实现的呢?SpringMVC3.0提供Hand

qq383264679的专栏 8305

Spring、SpringMVC常用注解总结

一、Spring常用注解 @Component:标注一个普通的Spring Bean类。 @Controller:标注一个控制器组件类。 @Service:标注一个业务层组件类。 @Repository:标注一个dao持久层组件类。 Bean实例的名字默认是Bean类的首字母小写,其他部分不变。 尽量考虑使用@Controller,@Service,@Repository代替通用的@Component。 二、SpringMVC常用注解 @Controller @RequestMappin

qq_42552857的博客 1万+

SpringMVC中的@ModelAttribute注解【详解】

SpringMVC中@ModelAttribute是一个重要的属性,理解好它是用好自动封装等功能的基础。可是@ModelAttribute和@RequestMapping组合后的状况较多,很容易让人糊涂。 1、如果想向Model中绑定对象属性: 例:一个没有使用@ModelAttribute注解的Controller方法: @RequestMapping("/hello") public String hello(User user) { user.setId(520); user.s.

单枪匹码 9197

springMVC获取参数的几种注解

一: @RequestBody 接受json方式发送的请求不可用于from表单提交方式 实例: @RequestMapping(value= "/save",method=RequestMethod.POST) @ResponseBody public PageData saveLeave(@RequestBody Map&lt;String,String&gt; map){} 以什...

qq_37453085的博客 3348

【SpringMVC】Controller注解及请求参数详解

本文目录Controller注解Controller 注解用法Controller 接收请求参数通过形参接收请求参数通过实体Bean 接收参数通过HttpServletRequst接收请求参数通过RequestParam注解接收请求参数PathVariable注解接收请求参数 Controller注解 在Spring中 Controller注解用于指示Spring类的实例是一个控制器,相对于实现Controller接口变得更加简单。而且实现Controller接口只能处理一个单一的请求,而是用@Contro

lucashu的博客 2万+

一、springMVC环境、参数封装、常用注解

一、spring入门案例 1. springMVC概述 SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring FrameWork 的后续产品,已经融合在 Spring Web Flow 里面。Spring 框架提供了构建 Web 应用程序的全功 能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring 的 Spring MVC 框架或集成其他 MVC 开发

zhoujian43的博客 397

SpringMVC参数封装使用总结

一、springMVC自动封装 1、传递参数 (1)、GET请求方式传递 参数 后端代码:test(String paramName,String paramName1) 前端代码:$.get(basePath + "/api/params/test?paramName=111&paramName1=222"(2)、POST请求方式传递 参数 后台代码:test(String paramName,String paramName1) 前端代码:$.post(bas...

AlbenXie的博客 1592
上一篇: 解决请求参数的中文乱码问题
下一篇: SpringMVC(三)之数据回显、url模板映射、转发和重定向
章 鱼
博客等级 码龄10年 203粉丝 105原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值