首页 > 其他分享 >spring boot——请求与参数校验——spring-mvc——通过形参获取请求参数——使用 @RequestParam 注解获取

spring boot——请求与参数校验——spring-mvc——通过形参获取请求参数——使用 @RequestParam 注解获取

时间:2023-01-15 14:44:06浏览次数:40  
标签:return 请求 spring request String 参数 http out name

 

之前有一个遗留问题,如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package org.example.controller.requestparam;

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 org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
public class ForParamController
{

    // http://localhost:8080/getRequestParam?name=C语言中文网&url=c.biancheng.net

    /**
     * 通过 HttpServletRequest 获取请求参数
     * @param request
     * @return
     */
    @RequestMapping("/getRequestParam")
    public String requestParam(HttpServletRequest request) {

        String name = request.getParameter("name");
        String url = request.getParameter("url");

        System.out.println("name:" + name);
        System.out.println("url:" + url);


        return "cheng_gong";
    }


    // http://localhost:8080/getRequestParam?firstName=C语言中文网&lastName=c.biancheng.net
    /**
     * 通过 HttpServletRequest 获取请求参数
     * @param request
     * @return
     */
    @RequestMapping(value = "/getRequestParam2")
    public String test2(HttpServletRequest request) {
        String a = request.getParameter("firstName");
        String b = request.getParameter("lastName");
        return a + b;
    }




    //http://localhost:8080/getRequestParam3?name=tom&language=java

    @RequestMapping("/getRequestParam3")
    public String test3(String name, String language) {
        System.out.println("a:" + name);
        System.out.println("b:" + language);
        return "success";
    }



    //  http://localhost:8080/getRequestParam4?name=tom&language=2

    @RequestMapping("/getRequestParam4")
    public String test4(String name, Integer language) {
        System.out.println("a:" + name);
        System.out.println("b:" + language);
        return "success";
    }






    //  http://localhost:8080/testRequestParam5?name=Java&pass=yyds

    @RequestMapping("/testRequestParam5")
    public String testRequestParam(@RequestParam("name") String username, @RequestParam("pass") String password) {
        System.out.println(username + "," + password);
        return "success";
    }












}

 

 

 

 

 

 

 

 

 

 

 

 

 

package org.example.controller.requestparam;

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 org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
public class ForParamController
{

    // http://localhost:8080/getRequestParam?name=C语言中文网&url=c.biancheng.net

    /**
     * 通过 HttpServletRequest 获取请求参数
     * @param request
     * @return
     */
    @RequestMapping("/getRequestParam")
    public String requestParam(HttpServletRequest request) {

        String name = request.getParameter("name");
        String url = request.getParameter("url");

        System.out.println("name:" + name);
        System.out.println("url:" + url);


        return "cheng_gong";
    }


    // http://localhost:8080/getRequestParam?firstName=C语言中文网&lastName=c.biancheng.net
    /**
     * 通过 HttpServletRequest 获取请求参数
     * @param request
     * @return
     */
    @RequestMapping(value = "/getRequestParam2")
    public String test2(HttpServletRequest request) {
        String a = request.getParameter("firstName");
        String b = request.getParameter("lastName");
        return a + b;
    }




    //http://localhost:8080/getRequestParam3?name=tom&language=java

    @RequestMapping("/getRequestParam3")
    public String test3(String name, String language) {
        System.out.println("a:" + name);
        System.out.println("b:" + language);
        return "success";
    }



    //  http://localhost:8080/getRequestParam4?name=tom&language=2

    @RequestMapping("/getRequestParam4")
    public String test4(String name, Integer language) {
        System.out.println("a:" + name);
        System.out.println("b:" + language);
        return "success";
    }






    //  http://localhost:8080/testRequestParam5?name=Java&pass=yyds

    @RequestMapping("/testRequestParam5")
    public String testRequestParam(@RequestParam("name") String username, @RequestParam("pass") String password) {
        System.out.println(username + "," + password);
        return "success";
    }







    //    http://localhost:8080/requestParam6?username=Java

    //    http://localhost:8080/requestParam6?username=

    @RequestMapping("/requestParam6")
    public String requestParam22(@RequestParam(value = "username",defaultValue = "abc21") String name)
    {

        return name;
    }





}

 

 

 

 

 

 

标签:return,请求,spring,request,String,参数,http,out,name
From: https://www.cnblogs.com/xiaobaibailongma/p/17053454.html

相关文章