Springmvc学习-Springmvc04-recieveParameter(2)

Springmvc04-recieveParameter

3. arrayOrCollection
  • index.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="springmvc/hello" method="post">
		<input type="checkbox" name="intrest" value="a1">a1</br>
		<input type="checkbox" name="intrest" value="a2">a2</br>
		<input type="checkbox" name="intrest" value="a3">a3</br>
				<input type="submit" value="提交"></br>
	</form>
</body>
</html>
  • controller
package com.caorui.handlers;



import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.caorui.pojo.Star;

//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc")//该注解起了限定范围的作用
public class MyController {

//	@RequestMapping("/hello")   //用数组接收
//	public void hello(String[] intrest) { 
//		for (String string : intrest) {
//			System.out.println(string);
//		}
//	}


	@RequestMapping("/hello")   //用集合接收
	public void hello1(@RequestParam List<String> intrest) {
		for (String string : intrest) {
			System.out.println(string);
		}
	}
}


4. restful
  • controller
package com.caorui.handlers;



import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.caorui.pojo.Star;

//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc")//该注解起了限定范围的作用
public class MyController {
	
	@RequestMapping("/{name}/{age}/hello")  //@PathVariable接收请求路径中占位符的值
	public void hello(@PathVariable String name, @PathVariable int age) { 
		System.out.println(name+age);
	}
}


5. json
  • index.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">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js"></script>
<script type="text/javascript">
	$(function(){
		$("#myButton").click(function(){
			var data1={username:"zhangsan",age:23};
		$.ajax({
			url:"${pageContext.request.contextPath}/springmvc/hello",
			type:"POST",
			contentType:"application/json",
			data:JSON.stringify(data1)  //将data1转换成字符串给controller
		})
			
		})
		
	})
</script>
<title>Insert title here</title>
</head>
<body>

	<button id="myButton">点击我呀!</button>
</body>
</html>
  • controller
package com.caorui.handlers;



import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.caorui.pojo.Star;

//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc")//该注解起了限定范围的作用
public class MyController {
	//接受json字符串并封装成对象
	@RequestMapping("/hello")  
	public void hello(@RequestBody Star star) { 
		System.out.println(star);
	}
}

data:JSON.stringify(data1) //将data1转换成字符串给controller
@RequestBody Star star //接受json字符串并封装成对象


6. 获得请求体里面的信息
  • controller
package com.caorui.handlers;



import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.caorui.pojo.Star;

//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc")//该注解起了限定范围的作用
public class MyController {

	@RequestMapping("/hello")  
	public void hello(@RequestHeader String host, @RequestHeader String cookie) { 
		System.out.println(host + " " + cookie);
	}
}

@RequestHeader String host, @RequestHeader String cookie通过@RequestHeader可以获得请求体里面的数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值