最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC五種類型參數(shù)傳遞及json傳遞參數(shù)

 更新時(shí)間:2022年07月18日 10:09:14   作者:不會(huì)壓彎的小飛俠  
本文主要介紹了SpringMVC五種類型參數(shù)傳遞及json傳遞參數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

學(xué)習(xí)內(nèi)容:

1.普通參數(shù)
2.pojo參數(shù)
3.嵌套pojo
4.數(shù)組參數(shù)
5.集合參數(shù)
6.解決中文亂碼
7.json數(shù)據(jù)傳遞參數(shù)

案例分析:

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};
? ? }
}
/*
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
? ? //加載SpringMVC容器配置
? ? @Override
? ? protected WebApplicationContext createServletApplicationContext() {
? ? ? ? AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
? ? ? ? ctx.register(SpringMvcConfig.class);
? ? ? ? return ctx;
? ? }
? ? //設(shè)置哪些請(qǐng)求可以歸屬SpringMVC處理
? ? @Override
? ? protected String[] getServletMappings() {
? ? ? ? return new String[]{"/"};
? ? }
? ? //加載Spring容器配置
? ? @Override
? ? protected WebApplicationContext createRootApplicationContext() {
? ? ? ? AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
? ? ? ? ctx.register(SpringConfig.class);
? ? ? ? return ctx;
? ? }
}*/

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.普通參數(shù)傳遞
? ? @RequestMapping("/commonParam")
? ? @ResponseBody
? ? public String commonParam(String name,int age){
? ? ? ? System.out.println("普通參數(shù)傳遞name:"+name);
? ? ? ? System.out.println("普通參數(shù)傳遞age:"+age);
? ? ? ? return "'module':'common param'";
? ? ? ? //http://localhost:8080/SpringMVC_Demo3/commonParam?name=tom&age=13 ?普通參數(shù)傳遞name:jack ?普通參數(shù)傳遞age:13
? ? }
? ? //2.pojo參數(shù)傳遞
? ? @RequestMapping("/pojo")
? ? @ResponseBody
? ? public String pojoParam(User user){
? ? ? ? System.out.println("pojo參數(shù)傳遞user:"+user);
? ? ? ? return "'module':'pojo.....'";
? ? ? ? //http://localhost:8080/SpringMVC_Demo3/pojo?name=%E5%B0%8F%E9%A3%9E%E4%BE%A0&age=13
? ? ?//pojo參數(shù)傳遞user:User{name='小飛俠', age=13}
? ? }
? ? // 3.嵌套pojo參數(shù)
? ? @RequestMapping("/pojos")
? ? @ResponseBody
? ? public String pojosParam(User user){
? ? ? ? System.out.println("pojos參數(shù)傳遞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參數(shù)傳遞user:User{name='小馬哥', age=13, address=Address{province='河南', city='商丘'}}
? ? }
? ? //4.數(shù)組參數(shù)傳遞
? ? @RequestMapping("/arr")
? ? @ResponseBody
? ? public String array(String [] arr){
? ? ? ? System.out.println("數(shù)組參數(shù)傳遞arr:"+ Arrays.toString(arr));
? ? ? ? return "'module':'array.....'";
? ? ? ? //http://localhost:8080/SpringMVC_Demo3/arr?arr=1&arr=2&arr=3
? ? ? ? //數(shù)組參數(shù)傳遞arr:[1, 2, 3]
? ? }
? ? //5.集合參數(shù)
? ? @RequestMapping("/coll")
? ? @ResponseBody
? ? public String collection(@RequestParam List<String> colls){
? ? ? ? System.out.println("集合參數(shù)傳遞colls:"+colls);
? ? ? ? return "'module':'collection.....'";
? ? ? ? //http://localhost:8080/SpringMVC_Demo3/coll?colls=1&colls=2&colls=3
? ? ? ? //集合參數(shù)傳遞colls:[1, 2, 3]
? ? }
? ? ?//6.pojo參數(shù):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 +
? ? ? ? ? ? ? ? '}';
? ? }
}

到此這篇關(guān)于SpringMVC五種類型參數(shù)傳遞及json傳遞參數(shù)的文章就介紹到這了,更多相關(guān)SpringMVC類型參數(shù)傳遞及json傳遞參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis驗(yàn)證多級(jí)緩存及 Cache Aside 模式的應(yīng)用小結(jié)

    MyBatis驗(yàn)證多級(jí)緩存及 Cache Aside 模式的應(yīng)用小結(jié)

    本文介紹了MyBatis的多級(jí)緩存機(jī)制,包括本地緩存和全局緩存,并通過Spock測(cè)試框架驗(yàn)證了多級(jí)緩存的實(shí)現(xiàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • SpringBoot SSMP 整合案例分享

    SpringBoot SSMP 整合案例分享

    這篇文章主要介紹了SpringBoot SSMP 整合案例分享,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Java使用itextpdf實(shí)現(xiàn)Excel轉(zhuǎn)PDF

    Java使用itextpdf實(shí)現(xiàn)Excel轉(zhuǎn)PDF

    這篇文章主要為大家詳細(xì)介紹了Java如何使用itextpdf實(shí)現(xiàn)Excel轉(zhuǎn)PDF,并且支持xlsx和xls兩種格,文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助
    2024-01-01
  • SpringBoot項(xiàng)目運(yùn)行一段時(shí)間后自動(dòng)關(guān)閉的坑及解決

    SpringBoot項(xiàng)目運(yùn)行一段時(shí)間后自動(dòng)關(guān)閉的坑及解決

    這篇文章主要介紹了SpringBoot項(xiàng)目運(yùn)行一段時(shí)間后自動(dòng)關(guān)閉的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 快速使用IDEA圖形化界面連接Phoenix的方法

    快速使用IDEA圖形化界面連接Phoenix的方法

    最近很多朋友跟小編留言如何使用IDEA圖形化界面連接Phoenix,在這小編就不一一回復(fù)大家了,今天抽空給大家整理一篇教程關(guān)于idea 圖形化界面連接Phoenix的相關(guān)知識(shí),需要的朋友快來學(xué)習(xí)下吧
    2021-05-05
  • springcloud如何獲取網(wǎng)關(guān)封裝的頭部信息

    springcloud如何獲取網(wǎng)關(guān)封裝的頭部信息

    這篇文章主要介紹了springcloud獲取網(wǎng)關(guān)封裝的頭部信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • JAVA如何獲取工程下的文件

    JAVA如何獲取工程下的文件

    這篇文章主要介紹了JAVA如何獲取工程下的文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 新手初學(xué)Java網(wǎng)絡(luò)編程

    新手初學(xué)Java網(wǎng)絡(luò)編程

    網(wǎng)絡(luò)編程是指編寫運(yùn)行在多個(gè)設(shè)備(計(jì)算機(jī))的程序,這些設(shè)備都通過網(wǎng)絡(luò)連接起來。本文介紹了一些網(wǎng)絡(luò)編程基礎(chǔ)的概念,并用Java來實(shí)現(xiàn)TCP和UDP的Socket的編程,來讓讀者更好的了解其原理
    2021-07-07
  • Spring Boot詳細(xì)打印啟動(dòng)時(shí)異常堆棧信息詳析

    Spring Boot詳細(xì)打印啟動(dòng)時(shí)異常堆棧信息詳析

    這篇文章主要給大家介紹了關(guān)于Spring Boot詳細(xì)打印啟動(dòng)時(shí)異常堆棧信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Elasticsearch(ES)多種查詢方式案例

    Elasticsearch(ES)多種查詢方式案例

    Elasticsearch是一個(gè)分布式的RESTful搜索和分析引擎,可讓您輕松地大規(guī)模存儲(chǔ),搜索和分析,這篇文章主要給大家介紹了關(guān)于Elasticsearch(ES)多種查詢方式的相關(guān)資料,需要的朋友可以參考下
    2023-09-09

最新評(píng)論

慈利县| 台江县| 罗山县| 乐东| 潮安县| 枝江市| 方城县| 天台县| 皋兰县| 寻乌县| 漯河市| 盐亭县| 会泽县| 易门县| 大荔县| 松阳县| 信丰县| 临西县| 中阳县| 万全县| 乐都县| 遵义市| 湘乡市| 太和县| 吴桥县| 晋中市| 姚安县| 灌云县| 普陀区| 萨嘎县| 驻马店市| 新泰市| 武乡县| 芦溪县| 综艺| 南雄市| 杂多县| 阳原县| 怀宁县| 许昌市| 涪陵区|