`

spring_mvc(三)Obtaining Request Data

 
阅读更多
Obtained 'foo' query parameter value 'bar'

http://localhost:8080/spring_mvc_test/data/param?foo=test
@RequestMapping(value="param")
public @ResponseBody String withParam(@RequestParam String foo) {
      String message = "Obtained 'foo' query parameter value '" + foo + "'";
      return message;
}

结果:Obtained 'foo' query parameter value 'test'

Obtain parameter group Param1:foo, Param2:bar, Params:baz

http://localhost:8080/spring_mvc_test/data/group?param1=foo&param2=bar&param3=baz
@RequestMapping(value="group")
public @ResponseBody String withParamGroup(JavaBean bean) {
         String message = "Obtained parameter group: " 
	+ bean.getParam1() + "," + bean.getParam2() + "," +  bean.getParam3();
         return message;
}

JavaBean:
private String param1;	
private String param2;
private String param3;


Obtain 'var' path variable value 'foo'

http://localhost:8080/spring_mvc_test/data/path/foo
@RequestMapping(value="path/{var}")
public @ResponseBody String withPathVariable(@PathVariable String var) {
	String message = "path variable:" + var;
	return message;
}


Obtained 'Accept'header 'text/plain, */*'
http://localhost:8080/spring_mvc_test/data/header
@RequestMapping(value="header")
public @ResponseBody String withHeader(@RequestHeader String Accept) {
	String message = "Obtained 'Accept' header '" + Accept + "'";
	return message;
}

Obtained 'Accept' header 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*'

posted request body 'foo'
<form action="http://localhost:8080/spring_mvc_test/data/body" method="post">
	<input type="text" name="aaa"/>
	<input type="submit" value="提交" />
</form>

@RequestMapping(value="body", method=RequestMethod.POST)
public @ResponseBody String withBody(@RequestBody String body) {
	String message = "body:" + body;
	return message;
}

结果:body:aaa=aaasdfasf

Request Body and Headers
@RequestMapping(value="entity", method=RequestMethod.POST)
public @ResponseBody String withEntity(HttpEntity<String> entity) {
	String message = "Posted request body '" + entity.getBody() "'; header = " + entity.getHeaders();
	return message;
}


read a form
<form action="http://localhost:8080/spring_mvc_test/data/form" method="post">
	<input type="text" id="aaa" name="aaa"/>
	<input type="text" id="bbb" name="bbb"/>
	<input type="submit" value="提交" />
</form>

@RequestMapping(value="form", method=RequestMethod.POST)
public @ResponseBody String withEntity(@RequestBody MultiValueMap<String, String> form) {
String message = "read from map" + form;
return message;
}
read from map{aaa=[qqq], bbb=[www]}

write a form

http://localhost:8080/spring_mvc_test/data/form
@RequestMapping(value="/form", method=RequestMethod.GET)
public @ResponseBody MultiValueMap<String, String> writeForm() {
   MultiValueMap<String, String> map = new LinkedMultiValueMap<String , String>();
   map.add("foo", "bar");
   map.add("fruit", "apple");
   return map;
}
分享到:
评论

相关推荐

    Usermanagement2:SpringMVC

    Obtaining Request Data Generating Responses Message Converters Rendering Views Type Conversion Validation Forms AngularJS MongoDB 跑步: 从命令行使用 Maven: $ mvn tomcat7:run [-Dmaven.tomcat.port=]...

    spring-mvc-sample-parcial2:伊曼纽尔·马隆(Emanuel Marlon)Fabricio Amperez Tahuico 201741942 Parcial 2

    介绍Spring MVC应用程序示例。 只需以mvn clean tomcat7:run身份运行嵌入式Tomcat服务器,即可在上测试该应用程序。执照该示例应用程序及其关联的源代码全部按照以下许可条款提供。 Copyright (C) 2015Permission is...

    Data.Lake.Architecture.1634621174

    Organizations invest incredible amounts of time and money obtaining and then storing big data in data stores called data lakes. But how many of these organizations can actually get the data back out ...

    Python Data Analytics 无水印pdf 0分

    This book examines how to go about obtaining, processing, storing, managing and analyzing data using the Python programming language. You will use Python and other open source tools to wrangle data ...

    Python Data Analytics(Apress,2015)

    This book examines how to go about obtaining, processing, storing, managing and analyzing data using the Python programming language. You will use Python and other open source tools to wrangle data ...

    Data Science with Java: Practical Methods for Scientists and Engineers

    Examine methods for obtaining, cleaning, and arranging data into its purest form Understand the matrix structure that your data should take Learn basic concepts for testing the origin and validity of ...

    Practical Web Scraping for Data Science - 2018

    The authors recommend web scraping as a powerful tool for any data scientist’s arsenal, as many data science projects start by obtaining an appropriate data set. Starting with a brief overview on ...

    数据分析视频--Getting and Cleaning Data.zip

    1-1-Obtaining Data Motivation (5-38) .mp4 1 - 2 - Raw and Processed Data (7-07).mp4 1 - 3 - Components of Tidy Data (9-25).mp4 1 - 4 - Downloading Files (7-09).mp4 1 - 5 - Reading Local Files (4-55)....

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    2015年高考英语二轮专题限时训练(15)定语从句

    2. 句子"Living in the mountain area has its problems, ________ obtaining water is a big problem."中的"________ obtaining water"也是定语从句,修饰"problems",表示“其中获取水是个大问题”,因此用"of ...

    domain user password obtaining

    domain user password obtaining

    Obtaining a stack trace in C upon SIGSEGV

    ### 获取 C 语言程序在 SIGSEGV 时的堆栈轨迹 #### 代码实现 本文档介绍了一种在不使用调试器的情况下获取 C 或 C++ 程序崩溃时堆栈轨迹的方法。此方法尤其适用于那些希望在程序崩溃时能够追踪到错误发生位置的...

    Obtaining Absolute Encoder Position on a TMS320C240.pdf

    在IT领域,尤其是在嵌入式系统与数字信号处理(DSP)技术中,了解如何获取绝对编码器位置是一项关键技能。本文将深入探讨标题为“在TMS320C240上获取绝对编码器位置”的应用简报,以及其在工业自动化、机器人技术和...

    [MIT公开课]MITOpenCourseWare: Advanced Data Structures 高级数据结构

    In addition, data structures are essential building blocks in obtaining efficient algorithms. This course will cover major results and current directions of research in data structures.

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-jpa-envers:一个示例项目,用于检查Spring Data对Hibernate Envers的支持

    Copyright (C) 2014Permission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the "Software"), to deal in theSoftware without ...

    netWindows_0.3.0_pre2

    * __data__ rewritten to support &quot;parser profiles&quot; which allow shallow data requests to be highly optimized using new parsers. * __sig__ now allow arbitrary number of arguments to any method...

    ZendFramework中文文档

    7.12. MVC 异常 7.12.1. 介绍 7.12.2. 如何处理异常? 7.12.3. 可能遭遇的MVC异常 7.13. 从以前的版本移植 7.13.1. 从 1.0.x 到 1.5.0 或更新的版本的移植 7.13.2. 从 0.9.3 到 1.0.0RC1 或更新的版本的移植 ...

    Python Geospatial Development - Second Edition.pdf

    No prior knowledge of geospatial concepts, tools or techniques is required - The book guides you through the process of installing and using various toolkits, obtaining geospatial data for use in ...

Global site tag (gtag.js) - Google Analytics