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可以获得请求体里面的数据。