Springboot获取页面参数的方式

1、第一种指定前端URL请求参数名称与方法名称一致,这种方式简单来说就是URL请求格式中参数需要与方法的参数名称对应上,举个例子,这么一个URL请求:http://localhost:8080/0919/test1?name = xxx & pass = yyy 在指定的控制器类上加上Controller 注解,同时指定RequestMapping注解即可,当请求路径参数与方法参数匹配上时会自动注入。

@RequestMapping("/test1")
@ResponseBody
public String test1( String name, String pass ){
     String s1 = name;
     String s2 =pass;
     String s1 + s2; 
}

2、第二种方式是通过HttpServletRequest来获取前端页面参数,代码如下。简单来说就是通过调动Request的getParameter方法来获取参数,比如访问路径类似这样:http://localhost:8080/0919/test2?name = zhang &pass = 123 

@RequstMapping("/test2")
@ResponseBody
public String test2( HttpServletRequest request ){ 
String name =request.getParamenter("name");
String pass = request.getParameter("pass");
     return name = pass; 
}

3、第三种方式是通过创建一个JavaBean对象来封装表单参数或者是请求URL路径中参数,简单来说就是将表单参数作为一个JavaBean类的属性,通过设置方法参数为一个JavaBean对象,之后在方法中通过调用对象的get方法来获取表单传过来的参数。

4、第四种方式是通过RequestParam注解来获取

 

@RequestMapping( value = "/test")
@ResponseBody
public String test( @RequestParam("aaa") String qqq,@RequstParam("www") String www)
{
   return qqq+www;
}

5、第五种方式是通过ModelAttribute方式来注入参数的,这种方式一般是想要在页面上展示从另一个页面上传过来的值!如下:

获取值;

@RequestMapping( value = "/aaa")
public String aaa()
{
    return "test2";
}

 显示值;

@RequestMapping( value = "/test6")
public String test6( @ModelAttributer("aaa") Student s )
{
    return "test3";
}

猜你喜欢

转载自blog.csdn.net/qq_44776691/article/details/88724000