欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springmvc請求參數(shù)的綁定的項目實踐

 更新時間:2025年11月05日 11:13:15   作者:sin2201  
本文主要介紹了springmvc請求參數(shù)的綁定的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、創(chuàng)建項目,pom文件

?<?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>com.qcby</groupId>
    <artifactId>springMVC12</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>5.0.2.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
?

二、web.xml

?<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--配置啟動加載-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>
?

三、spring-mvc.xml

?<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置spring創(chuàng)建容器時要掃描的包 -->
    <context:component-scan base-package="com.qcby.controller"></context:component-scan>
    <!-- 配置視圖解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 配置spring開啟注解mvc的支持-->
<!--    <mvc:annotation-driven></mvc:annotation-driven>-->
</beans>
?

四、index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>請求參數(shù)綁定</title>
</head>
<body>
<form action="user/save1.do" method="post">
    姓名:<input type="text" name="username" />
    年齡:<input type="text" name="age" />
    <input type="submit" value="提交" />
</form>
<h3>請求參數(shù)綁定(封裝到實體類)</h3>
<form action="user/save2.do" method="post">
    姓名:<input type="text" name="username" />
    年齡:<input type="text" name="age" />
    <input type="submit" value="提交" />
</form>
<h3>請求參數(shù)綁定(封裝到實體類)</h3>
<form action="user/save3.do" method="post">
    姓名:<input type="text" name="username" />
    年齡:<input type="text" name="age" />
    金額:<input type="text" name="address.money" />
    <input type="submit" value="提交" />
</form>
<h3>請求參數(shù)綁定(封裝到實體類,存在list集合)</h3>
<form action="user/save4.do" method="post">
    姓名:<input type="text" name="username" />
    年齡:<input type="text" name="age" />
    金額:<input type="text" name="address.money" />
    集合:<input type="text" name="list[0].money" />
    集合:<input type="text" name="list[1].money" />
    <input type="submit" value="提交" />
</form>
</body>
</html>

五、實體類

Address類

import java.io.Serializable;
public class Address implements Serializable {
    private Double money;
    public Double getMoney() {
        return money;
    }
    public void setMoney(Double money) {
        this.money = money;
    }
    @Override
    public String toString() {
        return "Address{" +
                "money=" + money +
                '}';
    }
}

User類

import java.io.Serializable;
import java.util.List;
public class User implements Serializable {
    private String username;
    private Integer age;
    // 引用對象
    private Address address;
    // list集合
    private List<Address> list;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public List<Address> getList() {
        return list;
    }
    public void setList(List<Address> list) {
        this.list = list;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", address=" + address +
                ", list=" + list +
                '}';
    }
}

六、UserController類

import com.qcby.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/save1.do")
    public String save(String username,Integer age){
        System.out.println("姓名:"+username);
        System.out.println("年齡:"+age);
        return "success";
    }
    @RequestMapping("/save2.do")
    public String save2(User user){
        System.out.println("user對象:"+user);
        return "success";
    }
    @RequestMapping("/save3.do")
    public String save3(User user){
        System.out.println("user對象:"+user);
        return "success";
    }
    @RequestMapping("/save4.do")
    public String save4(User user){
        System.out.println("user對象:"+user);
        return "success";
    }
}

七、請求參數(shù)解決中文亂碼

在web.xml中配置Spring提供的過濾器

?
<!-- 配置過濾器,解決中文亂碼的問題 -->
<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <!-- 指定字符集 -->
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
現(xiàn)在的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 配置過濾器,解決中文亂碼的問題 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 指定字符集 -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--配置啟動加載-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>
?

八、配置tomcat,然后啟動tomcat

1.

2.

3.

4.

九、接收Map類型

1.直接接收Map類型

如果想直接接收前端傳過來的map參數(shù),應該使用兩個注解(RequestBody或RequestParam;RequestParam--get和post請求都可以,RequestBody只能post請求,底層封裝都是LinkedHashMap)

(1)Get請求

第一種情況,什么注解也沒有

UserController類里加一個方法

@RequestMapping("/mapSave1.do")
public String mapSave1(Map<String, Objects> map){
    System.out.println("map:"+map);
    return "success";
}

沒有JSP頁面,啟動tomcat

控制臺:什么輸出也沒有,沒有值

第二種情況:傳個值

控制臺:還是什么都沒有

第三種情況:聲明是get請求

UserController類的mapSave1()方法:

@RequestMapping(value = "/mapSave1.do",method = RequestMethod.GET)

public String mapSave1(Map<String, Objects> map){

    System.out.println("map:"+map);

    return "success";

}

再啟動:

控制臺:還是沒有值

所以跟請求是什么沒關系,要想接收就要加注解

第四種情況:加@RequestParam

@RequestMapping(value = "/mapSave1.do")

public String mapSave1(@RequestParam Map<String, Objects> map){

    System.out.println("map:"+map);

    return "success";

}

再運行:

所以,我傳遞一個map在后端接收,用get請求必須加@RequestParam注解

(2)post請求:

第一種情況:什么注解也沒有

@RequestMapping(value = "/mapSave2.do")
public String mapSave1(Map<String, Objects> map){
    System.out.println("map:"+map);
    return "success";
}
前端頁面:加一個表單
<h3>請求參數(shù)的綁定--map集合</h3>
<form action="user/mapSave2.do" method="post">
    map集合key:<input type="text" name="map.key" />
    map集合value:<input type="text" name="map.value" />
    <input type="submit" value="提交" />
</form>

運行

點提交

控制臺:什么也沒有

第二種情況:聲明是post請求

@RequestMapping(value = "/mapSave2.do",method = RequestMethod.POST)
public String mapSave2(Map<String, Objects> map){
    System.out.println("map:"+map);
    return "success";
}

再運行:

點提交

控制臺:

說明跟get的一樣,不加注解是沒有辦法接收到的

第三種情況:加上@RequestParam注解

@RequestMapping(value = "/mapSave2.do",method = RequestMethod.POST)
public String mapSave2(@RequestParam Map<String, Objects> map){
    System.out.println("map:"+map);
    return "success";
}

運行:

點提交

控制臺:

可以看出,get請求和post請求都可以用@RequestParam注解

表單和controller類中的方法改改(加個username)

表單:

<h3>請求參數(shù)的綁定--map集合</h3>
<form action="user/mapSave2.do" method="post">
    username:<input type="text" name="username">
    map集合:<input type="text" name="test1">
<%-- test1就是map的key,輸入框中的就是map的value --%>
    <input type="submit" value="提交" />
</form>

方法:

@RequestMapping(value = "/mapSave2.do")
public String mapSave2(@RequestParam Map<String, Objects> map,String username){
    System.out.println("map:"+map);
    System.out.println("username:"+username);
    return "success";
}

運行:

點提交:

控制臺:

可以看到:表單中的數(shù)據(jù)都被封裝到了map集合中

第四種情況:加@RequestBody注解

但是這樣的話,它只能接收json數(shù)據(jù)

現(xiàn)在用表單接收就會報錯:

@RequestMapping(value = "/mapSave2.do")
public String mapSave2(@RequestBody Map<String, Objects> map, String username){
    System.out.println("map:"+map);
    System.out.println("username:"+username);
    return "success";
}

運行:

點提交:(報錯)

總結:無注解時,什么都接收不了;@RequestParam注解時,將參數(shù)包裝成LinkedHashMap對象,參數(shù)的key是Map的key,參數(shù)的值是Map的value,get和

post請求都支持;@RequestBody注解接收json類型的數(shù)據(jù)(跟表單不一樣,表單傳不了),也會包裝成LinkedHashMap對象,該注解不支持get請求,get請求沒有請求體,不能傳json

2.用對象接收map

(1)User類里加一個map

private Map<String,Address> userMap;

(2)前端:

<h3>請求參數(shù)綁定(封裝到實體類,存在map集合)</h3>
<form action="user/save5.do" method="post">
    姓名:<input type="text" name="username" />
    年齡:<input type="text" name="age" />
    金額:<input type="text" name="address.money" />
    Map集合:<input type="text" name="userMap['one'].money" />
    Map集合:<input type="text" name="userMap['two'].money" />
    <input type="submit" value="提交" />
</form>

(3)運行:

點提交:

控制臺:

十、在控制器中使用原生的ServletAPI對象

只需要在控制器的方法參數(shù)定義HttpServletRequest和HttpServletResponse對象

UserController里加:

/*原生的API*/
@RequestMapping("/save6.do")
public String save6(HttpServletRequest request, HttpServletResponse response){
    System.out.println(request);
    // 獲取到HttpSession對象
    HttpSession session = request.getSession();
    System.out.println(session);
    System.out.println(response);
    return "success";
}

運行:

控制臺:

到此這篇關于springmvc請求參數(shù)的綁定的項目實踐的文章就介紹到這了,更多相關springmvc請求參數(shù)的綁定內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • mybatisPlus中批量刪除的示例代碼

    mybatisPlus中批量刪除的示例代碼

    本文主要介紹了mybatisPlus中批量刪除的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • 詳談Springfox與swagger的整合使用

    詳談Springfox與swagger的整合使用

    下面小編就為大家?guī)硪黄斦凷pringfox與swagger的整合使用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • java方法替換word文檔中需要替換的部分操作步驟

    java方法替換word文檔中需要替換的部分操作步驟

    這篇文章主要介紹了java方法替換word文檔中需要替換的部分操作的相關資料,需要的朋友可以參考下,包括引入依賴、創(chuàng)建業(yè)務方法、測試以及擴展功能,需要的朋友們可以參考下
    2024-12-12
  • 一文帶你理解@RefreshScope注解實現(xiàn)動態(tài)刷新原理

    一文帶你理解@RefreshScope注解實現(xiàn)動態(tài)刷新原理

    RefeshScope這個注解想必大家都用過,在微服務配置中心的場景下經常出現(xiàn),他可以用來刷新Bean中的屬性配置,那大家對他的實現(xiàn)原理了解嗎,它為什么可以做到動態(tài)刷新呢,所以本文小編將給大家詳細介紹@RefreshScope注解實現(xiàn)動態(tài)刷新原理
    2023-07-07
  • Java 多線程有序執(zhí)行的幾種方法總結

    Java 多線程有序執(zhí)行的幾種方法總結

    這篇文章主要介紹了Java 多線程有序執(zhí)行的幾種方法總結的相關資料,需要的朋友可以參考下
    2017-03-03
  • 解決SpringBoot中使用@Transactional注解遇到的問題

    解決SpringBoot中使用@Transactional注解遇到的問題

    這篇文章主要介紹了SpringBoot中使用@Transactional注解遇到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java實現(xiàn)小球碰撞功能

    java實現(xiàn)小球碰撞功能

    這篇文章主要為大家詳細介紹了java實現(xiàn)小球碰撞功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Java中的適配器模式詳解

    Java中的適配器模式詳解

    這篇文章主要介紹了Java中的適配器模式詳解,適配器模式(Adapter?Pattern)將某個類的接口轉換成客戶端期望的另一個接口表示,主的目的是兼容性,讓原本因接口不匹配不能一起工作的兩個類可以協(xié)同工作,需要的朋友可以參考下
    2023-09-09
  • 深入了解Java設計模式之職責鏈模式

    深入了解Java設計模式之職責鏈模式

    Java設計模式中有很多種類別,例如單例模式、裝飾模式、觀察者模式等。本文將為大家詳細介紹其中的職責鏈模式,感興趣的可以了解一下
    2022-09-09
  • spring security自定義登錄頁面

    spring security自定義登錄頁面

    在項目中我們肯定不能使用Spring自己生成的登錄頁面,而要用我們自己的登錄頁面,下面通過本文給大家分享spring security自定義登錄頁面的實現(xiàn)方法,一起看看吧
    2017-09-09

最新評論