SpringMVC五种类型参数传递及json传递参数
学习内容:
1.普通参数
2.pojo参数
3.嵌套pojo
4.数组参数
5.集合参数
6.解决中文乱码
7.JSON数据传递参数
案例分析:
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Http://Maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springmvc-Demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-WEBmvc</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</project>
2.ServletContainersInitConfig
package com.study.config;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfiGClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
3.SpringMvcConfig
package com.study.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.study.controller")
public class SpringMvcConfig {
}
4.UserController
package com.study.controller;
import com.study.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.List;
@Controller
public class UserController {
//1.普通参数传递
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(String name,int age){
System.out.println("普通参数传递name:"+name);
System.out.println("普通参数传递age:"+age);
return "'module':'common param'";
//http://localhost:8080/SpringMVC_Demo3/commonParam?name=tom&age=13 普通参数传递name:jack 普通参数传递age:13
}
//2.pojo参数传递
@RequestMapping("/pojo")
@ResponseBody
public String pojoParam(User user){
System.out.println("pojo参数传递user:"+user);
return "'module':'pojo.....'";
//http://localhost:8080/SpringMVC_Demo3/pojo?name=%E5%B0%8F%E9%A3%9E%E4%BE%A0&age=13
//pojo参数传递user:User{name='小飞侠', age=13}
}
// 3.嵌套pojo参数
@RequestMapping("/pojos")
@ResponseBody
public String pojosParam(User user){
System.out.println("pojos参数传递user:"+user);
return "'module':'pojos.....'";
//http://localhost:8080/SpringMVC_Demo3/pojos?name=%E5%B0%8F%E9%A9%AC%E5%93%A5&age=13&address.province=%E6%B2%B3%E5%8D%97&address.city=%E5%95%86%E4%B8%98
//pojo参数传递user:User{name='小马哥', age=13, address=Address{province='河南', city='商丘'}}
}
//4.数组参数传递
@RequestMapping("/arr")
@ResponseBody
public String array(String [] arr){
System.out.println("数组参数传递arr:"+ Arrays.toString(arr));
return "'module':'array.....'";
//http://localhost:8080/SpringMVC_Demo3/arr?arr=1&arr=2&arr=3
//数组参数传递arr:[1, 2, 3]
}
//5.集合参数
@RequestMapping("/coll")
@ResponseBody
public String collection(@RequestParam List<String> colls){
System.out.println("集合参数传递colls:"+colls);
return "'module':'collection.....'";
//http://localhost:8080/SpringMVC_Demo3/coll?colls=1&colls=2&colls=3
//集合参数传递colls:[1, 2, 3]
}
//6.pojo参数:json格式
@RequestMapping("/json")
@ResponseBody
public String json(User user) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
return "'module':'json.....'";
//http://localhost:8080/SpringMVC_Demo3/json?name=小马哥&age=23&address.province=河南&address.city=商丘
//{"name":"小马哥","age":23,"address":{"province":"河南","city":"商丘"}}
}
}
5.Address
package com.study.domain;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
6.User
package com.study.domain;
public class User {
private String name;
private int age;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
到此这篇关于SpringMVC五种类型参数传递及json传递参数的文章就介绍到这了,更多相关SpringMVC类型参数传递及json传递参数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章