springMVC获取参数方法——idea

121 阅读1分钟

1、搭建springMVC框架,不会到小伙伴可以点我主页的文章

2、创建一个用来请转发到默认页面跳转到控制器TestControler

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {


//默认转发到test_param.html页面
    @RequestMapping("/")
    public String Param(){
        return "test_param";
    }



}

3、创建对应html页面

<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试请求参数</title>
</head>
<body>

<h1>请求参数</h1><br/>

<a th:href="@{/testServletAPI(username='admin',password='123456')}">测试servletAPI获取请求参数</a><br/>
<a th:href="@{/testParam(username='admin',password='123456')}">测试控制器形参获取请求参数</a><br/>

<form th:action="@{/testParam}" method="post">
    用户名:<input type="text" name="user_name"><br/>
    密码:<input type="text" 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
    <input type="submit" value="控制器获取参数">
</form><br/>

<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" value="使用pojo接收参数">
</form>

</body>
</html>

4、创建成功跳转页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    success
</body>
</htm

5、创建ParamController控制器用来获取参数,获取参数三种方式

package controller;

import bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

@Controller
public class ParamController {
    //用servletAPI获取参数
    @RequestMapping("/testServletAPI")
    //形参requset表示当前请求
    public String testServletAPI(HttpServletRequest request){
        HttpSession session=request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+":"+password);
        return "success";
    }

    //用控制器获取参数
    @RequestMapping("/testParam")
    //请求参数多个重名,可以设置字符串数组接收请求参数
    public String testParam(
            //设置required为false表示参数可以为null
            @RequestParam(value = "user_name",required = false) String username,
            String password,
            String[] hobby){
        System.out.println(username+":"+password+":"+ Arrays.toString(hobby));
        return "success";
    }

    //通过实体类获取参数
    @RequestMapping("/testpojo")
    public String testpojo(User user){
        System.out.println(user);
        return "success";
    }


}