spring boot 学习(五)------接收页面参数值和向页面发送数据的各3种方法+重定向的两种方式

接收页面参数值的三种方法:

1.使用request接收参数

@RequestMapping("/test1.do")

public ModelAndView test1(HttpServletRequest request){

           String userName =request.getParame("userName");//需要自己处理数据类型转换

           String password = request.getParame("password");

            return new ModelAndView("jsp/hello");

}

2.使用方法参数接收参数

@RequestMapping("/test2.do")

public ModelAndView test2(String userName,@RequestParam("password") String pwd){

           //参数类型自动转换,有可能出现类型转换异常
            return new ModelAndView("jsp/hello");

}

3.使用对象接收参数(使用自动机制封装成Bean对象)

/**需要定义User实体,属性名与<form>表单组件的name相同
页面:name,pwd
实体类:name,pwd
一一对应
*/

@RequestMapping("/test3.do")

public ModelAndView test3(User user){


            return new ModelAndView("jsp/hello");

}

像页面发送数据的方法:

1.使用ModeAndView对象传送数据

@RequestMapping("/test4.do")

public ModelAndView test4(){

            map<String,Object> data =new HashMap<String,Object>();

            data.put("success",true);

            data.put("message","操作成功");            
            return new ModelAndView("jsp/hello",data);

}

2.使用ModeMap传出数据

@RequestMapping("/test5.do")

public ModelAndView test5(ModelMap model){

            model.addAttibute("success",false);

            model.addAttibute("message","操作失败");  
            return new ModelAndView("jsp/hello");

}

3.使用@ModelAtrribute传出bean属性

@ModelAtrribute("age")//在Bean属性方法上使用

public int getAge(){

    return age;

}

4.使用@ModelAtrribute传出参数值

@RequestMapping("/test6.do")//在Controller方法的参数部分

public ModelAndView test6(@ModelAtrribute("userName")  String userName,String password){
            return new ModelAndView("jsp/hello");

}

session、重定向(略)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值