SpringMVC获取页面提交的参数以及返回提交信息测试

无参数请求测试

index.jsp

<a href="/test">无参测试</a>

text.java
添加注解@Controller

@Controller
public class TextController {
    private HttpSession session;
    private ModelMap map;
    private HttpServletRequest request;

    //无参数请求测试
    @RequestMapping("test") //指定处理请求的地址
    public String abc(){
        System.out.println("abc执行");

        //转发路径
        //http://localhost:8080/项目/路径/show.jsp
        return "show";
    }
 }

有参数请求测试

基本数据类型和String测试
index.jsp

<a href="/test1?uname=abc&age=18">有参测试</a>

text.java
参数名=控件名
类的上面添加注解@Controller

 @RequestMapping("test1")
    public String test1(String uname,int age){
        System.out.println("uname:"+uname);
        System.out.println("age:"+age);

        //转发路径
        //http://localhost:8080/项目/路径/show.jsp
        return "show";
    }

对象参数测试
index.jsp

<form method="post" action="/test2">
    用户名<input type="text" name="username" /><br>
    age<input type="text" name="age" /><br>
    sex<input type="radio" name="sex" value="男">男<br>
        <input type="radio" name="sex" value="女">女<<br>
    like
    <input type="checkbox" name="love" value="打篮球" >打篮球
    <input type="checkbox" name="love" value="足球" >足球
    <input type="checkbox" name="love" value="音乐" >音乐
    <input type="checkbox" name="love" value="打游戏" >打游戏

    地址
    <select name="address">
        <option>北京</option>
        <option>上海</option>
    </select>
  
<input type="submit" value="注册">


</form>

test.java

 //对象参数测试
    @RequestMapping("test2")
    public String test1(Users users){
        //传递参数会发生编码问题,解决方式一种为配置web.xml文件
        System.out.println(users.getUsername());
        System.out.println(Arrays.toString(users.getLove()));
        System.out.println(users.getSex());
        System.out.println(users.getAddress());
        System.out.println(users.getBirthday());

        return "show";

    }

对象参数返回到页面测试

  1. request方式:request.setAttribute(“u2”,users);
  2. ModelMap方式:map.put(“u2”,users);
  3. ModelAndView
    index.jsp
    1和2方式
<form method="post" action="/test3">
    用户名<input type="text" name="username" /><br>
    age<input type="text" name="age" /><br>
    sex<input type="radio" name="sex" value="男">男<br>
        <input type="radio" name="sex" value="女">女<<br>
    like
    <input type="checkbox" name="love" value="打篮球" >打篮球
    <input type="checkbox" name="love" value="足球" >足球
    <input type="checkbox" name="love" value="音乐" >音乐
    <input type="checkbox" name="love" value="打游戏" >打游戏

    地址
    <select name="address">
        <option>北京</option>
        <option>上海</option>
    </select>
  
<input type="submit" value="注册">


</form>

test.java

@RequestMapping("test3")
    public String test2(Users users, HttpServletRequest request, ModelMap map){
        //传递参数会发生编码问题,解决方式为配置web.xml文件
        System.out.println(users.getUsername());
        System.out.println(Arrays.toString(users.getLove()));
        System.out.println(users.getSex());
        System.out.println(users.getAddress());
        System.out.println(users.getBirthday());
        //request.setAttribute("u2",users);
        map.put("u2",users);
        return "show";
    }

show.jsp

<body>
    <h1>show.jsp</h1>
    <h1>姓名:${u2.username}</h1>
    <h1>age:${u2.age}</h1>
    <h1>address:${u2.address}</h1>
    <h1>生日:${u2.birthday}</h1>
</body>

3方式
test.java

@RequestMapping( value="/test3")
    public ModelAndView test3(Users users){
        ModelAndView m=new ModelAndView();
        m.setViewName("show");
        m.addObject("u2",users);
        return m;
    }

页面无参数名,传递参数测试

test.java
@PathVariable注解

//页面无参数名,传递参数测试
    @RequestMapping("test5/{a}/{b}")
    public String test5(@PathVariable("a") String name,@PathVariable("b") int age){
        System.out.println(name+"..........."+age);
        return "show";
    }

此时的传递参数的格式为 test5/“哈哈”/4

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值