1、表单提交
使用到的form表单和实体类文件
<form action="${pageContext.request.contextPath}/test" method="get">
<input type="text" name="name">
<input type="submit" value="提交">
</form>
<form action="${pageContext.request.contextPath}/test" method="post">
<input type="text" name="name">
<input type="submit" value="提交">
</form>
package com.xjj.entity;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1、Get方式提交
1、使用基本数据类型直接接收(可直接接收中文)
@RequestMapping("/test")
public void test(String name){
}
2、使用request接收(可直接接收中文)
@RequestMapping("/test")
public void test(HttpServletRequest request) {
System.out.println(request.getParameter("name"));
}
3、创建java对象(保证页面的参数name和对象属性名字一样)(可直接接收中文)
@RequestMapping("/test")
public void test(User user) {
System.out.println(user.getName());
}
4、使用Map来接收参数(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> map) {
System.out.println(map.get("name"));
}
2、Post方式提交
1、使用基本数据类型直接接收(中文需转码)
@RequestMapping("/test")
public void test(String name) throws UnsupportedEncodingException {
System.out.println(name);
System.out.println(new String(name.getBytes("ISO-8859-1"),"utf-8"));
}
2、使用request进行接收(中文需转码)
@RequestMapping("/test")
public void test(HttpServletRequest request) throws UnsupportedEncodingException {
System.out.println(request.getParameter("name"));
System.out.println(new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8"));
}
3、创建java对象来接收(中文需转码中文需转码)
@RequestMapping("/test")
public void test(User user) throws UnsupportedEncodingException {
System.out.println(user.getName());
System.out.println(new String(user.getName().getBytes("ISO-8859-1"),"utf-8"));
}
4、使用Map来接收(中文需转码)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> map) throws UnsupportedEncodingException {
System.out.println(map.get("name"));
System.out.println(new String(map.get("name").toString().getBytes("ISO-8859-1"),"utf-8"));
}
2、ajax
使用到的js代码
<!--普通方式post-->
<script>
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/test",
data:{
name:"中文",
},
text:"json",
success:function(data){
}
});
</script>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
<!--json额外处理的依赖,不加可能会报错误-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!--处理json为javabean格式时的依赖-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<script>
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/test",
data:JSON.stringify({
name:"中文"
}),
dataType : 'json',
contentType : 'application/json;charset=UTF-8',
success:function(data){
alert("返回成功");
}
});
</script>
<!--文件上传依赖-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="54000000"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<script>
var
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/test",
data:formdata,
contentType:false,
processData:false,
success:function(data){
alert(data);
return loadPage('${pageContext.request.contextPath}/goods/findGoodsByGoodsType/1');
}
});
</script>
1、普通的ajax方式(POST和GET的情况一样,所以合并为一项)
1、直接使用变量名字来借收(可直接接收中文)
@RequestMapping("/test")
public void test(String name) {
System.out.println(name);
}
2、使用request进行接收(可直接接收中文)
@RequestMapping("/test")
public void test(HttpServletRequest request) {
System.out.println(request.getParameter("name"));
}
3、使用java对象来接收(可直接接收中文)
@RequestMapping("/test")
public void test(user user) {
System.out.println(user.getName();
}
4、使用Map来接收(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> map) {
System.out.println(map.get("name"));
}
2、改用JSON.stringify的方式传递(只成功了POST,GET全部失败,因为get请求中不能带有{}这种特殊字符)
1、使用@ResponseBody来接收并注入到对象中(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestBody user user) {
System.out.println(user.getName());
}
2、使用@ResponseBody来接收成为一个字符串(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestBody String name) {
System.out.println(name);
}
3、使用formdata提交数据
1、使用参数名直接获取(可直接获取中文)
@RequestMapping("/test")
public void test(String name) {
System.out.println(name);
}
2、使用request获取(可直接获取中文)
@RequestMapping("/test")
public void test(HttpServletRequest request) {
System.out.println(request.getParameter("name"));
}
3、使用java对象来接收(可直接获取中文)
@RequestMapping("/test")
public void test(User user) {
System.out.println(user.getName());
}
4、使用Map来接收(可直接获取中文)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> json) {
System.out.println(json.get("name"));
}
3、直接使用地址来传参
1、使用@PathVariable(可接收中文,但是不可以附带’.’)
@RequestMapping("/test/{name}")
public void test(@PathVariable(name = "name")String name) {
System.out.println(name);
}
"/test")
public void test(@RequestParam Map<String,Object> json) {
System.out.println(json.get("name"));
}