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

SpringMVC獲取請(qǐng)求參數(shù)筆記整理

 更新時(shí)間:2022年04月11日 08:50:11   作者:鐵甲小寶同學(xué)  
本文記錄和分享在學(xué)習(xí)Spring MVC過程中的筆記,通過案例示例代碼分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

本篇文章目的是為了學(xué)習(xí)、記錄和分享博主在學(xué)習(xí) Spring MVC過程中的筆記。同時(shí)也希望本篇文章能夠幫助屏幕前的你!

一、使用ServletAPI獲取參數(shù)

通過 HttpServletRequest 當(dāng)作形參,此時(shí) HttpServletRequest 類型的參數(shù)表示封裝了當(dāng)前請(qǐng)求的請(qǐng)求報(bào)文的對(duì)象。

測(cè)試案例:

 @RequestMapping("/testParam")
    public String testParam(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }
<form method="get" action="/SpringMVC/testParam">
    username: <input  type="text"   name="username"/>
    password: <input  type="password"   name="password"/>
    <input type="submit" value="Submit" />
</form>

運(yùn)行結(jié)果:

在這里插入圖片描述

二、通過控制器方法的形參獲取請(qǐng)求參數(shù)

在控制器方法的形參位置,設(shè)置和請(qǐng)求參數(shù)同名的形參,當(dāng)瀏覽器發(fā)送請(qǐng)求,匹配到請(qǐng)求映射時(shí),在 DispatcherServlet 中就會(huì)將請(qǐng)求參數(shù)賦值給相應(yīng)的形參。

測(cè)試用例:

    @RequestMapping("/testParam")
    public String testParam(String username,String password){
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }
<form method="get" action="/SpringMVC/testParam">
    username: <input  type="text"   name="username"/>
    password: <input  type="password"   name="password"/>
    <input type="submit" value="Submit" />
</form>

運(yùn)行結(jié)果:

在這里插入圖片描述

三、@RequestParam

@RequestParam是將請(qǐng)求參數(shù)控制器方法的形參創(chuàng)建映射關(guān)系!

OK,我們可以來看一下 @RequestParam 的源碼部分:

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    //別名name,一般默認(rèn)為空
    @AliasFor("name")
    String value() default "";
	//指定為形參賦值的請(qǐng)求參數(shù)的參數(shù)名
    @AliasFor("value")
    String name() default "";
	//設(shè)置是否必須傳輸此請(qǐng)求參數(shù),默認(rèn)值為true
    boolean required() default true;
	//用來定義默認(rèn)值
    String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}

注:

required設(shè)置為true時(shí),則當(dāng)前請(qǐng)求必須傳輸value所指定的請(qǐng)求參數(shù),若沒有傳輸該請(qǐng)求參數(shù),且沒有設(shè)置defaultValue屬性,則頁面報(bào)錯(cuò) 400:Required String parameter ‘xxx’ is not present; 若設(shè)置為false,則當(dāng)前請(qǐng)求不是必須傳輸value所指定的請(qǐng)求參數(shù),若沒有傳輸,則注解所標(biāo)識(shí)的形參的值為null。

    @RequestMapping(value = "/testParam", method = RequestMethod.POST)
    public String testParam(@RequestParam(value = "user_name",required = false,defaultValue = "gg") String name,
                            @RequestParam(value = "password",required = true,defaultValue = "hh") String pwd,
                            String[] hobby) {

        System.out.println(name + pwd + Arrays.toString(hobby));
        return "success";
    }
<form th:action="@{/testParam}" method="post">
    用戶名:<input type="name" name = "user_name"><br>
    密碼:<input type="password" name = "password"><br>
    愛好:<input type="checkbox" name = "hobby" value="a">a
    <input type="checkbox" name = "hobby" value="b">b
    <input type="checkbox" name = "hobby" value="c">c<br>
    <input type="submit" value="測(cè)試">
</form>

運(yùn)行結(jié)果:

在這里插入圖片描述

四、@RequestHeader

@RequestHeader是將請(qǐng)求頭信息和控制器方法的形參創(chuàng)建映射關(guān)系。

@RequestHeader注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam。

@RequestMapping(value = "/testParam2",method = RequestMethod.POST)
//形參位置的request表示當(dāng)前請(qǐng)求
public String testParam2(@RequestParam("username") String name,
                         @RequestParam("password") String pwd,
                         String[] hobby,
                         @RequestHeader("Host") String host){
    System.out.println(name+pwd+ Arrays.toString(hobby));
    System.out.println("Host:"+host);//輸出結(jié)果Host:localhost:8080
    return "success";
}

五、@CookieValue

@CookieValue是將cookie數(shù)據(jù)和控制器方法的形參創(chuàng)建映射關(guān)系

@CookieValue注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam

@CookieValue("JSESSIONID") String JSESSIONID
@Controller
@RequestMapping("hello")
public class HelloController2 {
? ? @RequestMapping("show25")
? ? public String test25(Model model, @CookieValue("JSESSIONID")String jsessionid){
? ? ? ? model.addAttribute("msg", "獲取cookie,jsessionid:" + jsessionid);
? ? ? ? return "hello2";
? ? }
}

六、通過實(shí)體類的形參獲取參數(shù)

首先需要?jiǎng)?chuàng)建一個(gè)實(shí)體類 User

package xiaobao.mvc.bean;
public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private String sex;
    private String email;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    public String getUsername() {
        return username;
    public void setUsername(String username) {
        this.username = username;
    public String getPassword() {
        return password;
    public void setPassword(String password) {
        this.password = password;
    public Integer getAge() {
        return age;
    public void setAge(Integer age) {
        this.age = age;
    public String getSex() {
        return sex;
    public void setSex(String sex) {
        this.sex = sex;
    public String getEmail() {
        return email;
    public void setEmail(String email) {
        this.email = email;
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", email='" + email + '\'' +
                '}';
}

然后前端:

<form th:action="@{/testpojo}" method="post">
    用戶名:<input type="text" name="username"><br>
    密碼:<input type="password" name="password"><br>
    性別:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
    年齡:<input type="text" name="age"><br>
    郵箱:<input type="text" name="email"><br>
    <input type="submit">
</form>

后端:

@RequestMapping("/testpojo")
public String testPOJO(User user){
    System.out.println(user);
    return "success";
}

運(yùn)行結(jié)果:

在這里插入圖片描述

因?yàn)槲疫€沒沒有解決亂碼,所以這個(gè)就一直亂碼問題,等到后面我專門出一期這個(gè)文章來解決亂碼的問題!

到此這篇關(guān)于SpringMVC獲取請(qǐng)求參數(shù)筆記整理的文章就介紹到這了,更多相關(guān)SpringMVC獲取請(qǐng)求參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java concurrency集合之ConcurrentSkipListMap_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java concurrency集合之ConcurrentSkipListMap_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了Java concurrency集合之ConcurrentSkipListMap的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • RabbitMQ 的消息持久化與 Spring AMQP 的實(shí)現(xiàn)詳解

    RabbitMQ 的消息持久化與 Spring AMQP 的實(shí)現(xiàn)詳解

    這篇文章主要介紹了RabbitMQ 的消息持久化與 Spring AMQP 的實(shí)現(xiàn)剖析詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • MyBatis自定義SQL攔截器示例詳解

    MyBatis自定義SQL攔截器示例詳解

    Mybatis支持對(duì)Executor、StatementHandler、PameterHandler和ResultSetHandler 接口進(jìn)行攔截,也就是說會(huì)對(duì)這4種對(duì)象進(jìn)行代理,下面這篇文章主要給大家介紹了關(guān)于MyBatis自定義SQL攔截器的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 解決@ServerEndpoint不能注入@Autowired的問題

    解決@ServerEndpoint不能注入@Autowired的問題

    這篇文章主要介紹了解決@ServerEndpoint不能注入@Autowired的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java spring webmvc如何實(shí)現(xiàn)控制反轉(zhuǎn)

    Java spring webmvc如何實(shí)現(xiàn)控制反轉(zhuǎn)

    這篇文章主要介紹了Java spring webmvc如何實(shí)現(xiàn)控制反轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • java thread start()和run()方法簡(jiǎn)析

    java thread start()和run()方法簡(jiǎn)析

    本文以java中thread的start()和run()的區(qū)別做詳細(xì)介紹, 需要了解跟多的朋友可以參考下
    2012-11-11
  • java大話之創(chuàng)建型設(shè)計(jì)模式教程示例

    java大話之創(chuàng)建型設(shè)計(jì)模式教程示例

    這篇文章主要為大家介紹了java大話之創(chuàng)建型設(shè)計(jì)模式教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • ShardingSphere數(shù)據(jù)分片算法及測(cè)試實(shí)戰(zhàn)

    ShardingSphere數(shù)據(jù)分片算法及測(cè)試實(shí)戰(zhàn)

    這篇文章主要為大家介紹了ShardingSphere數(shù)據(jù)分片算法及測(cè)試實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 在springboot中如何使用線程池

    在springboot中如何使用線程池

    在SpringBoot中,可以通過定義ThreadPoolTaskExecutor的Bean并使用@Autowired注入來使用線程池,具體步驟包括創(chuàng)建ThreadPoolTaskExecutor的Bean配置,本文給大家介紹springboot使用線程池的例子,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Java泛型的用法及T.class的獲取過程解析

    Java泛型的用法及T.class的獲取過程解析

    這篇文章主要介紹了Java泛型的用法及T.class的獲取過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評(píng)論

清河县| 齐河县| 芮城县| 太康县| 墨玉县| 宁阳县| 洪雅县| 洪洞县| 孟州市| 桂平市| 平昌县| 容城县| 伊吾县| 曲麻莱县| 科技| 武强县| 浦江县| 木兰县| 西青区| 壤塘县| 寻乌县| 长岭县| 专栏| 永昌县| 芒康县| 西畴县| 兴业县| 宁武县| 武宣县| 图木舒克市| 沽源县| 剑阁县| 广灵县| 栖霞市| 屏东县| 巩义市| 阿坝| 长兴县| 龙里县| 安新县| 巴彦县|