SpringMVC-接收参数,跳转和传值

目录

 

一.接收请求参数

1.1基本类型参数

1.2实体收参*

1.3数组收参

1.4路径参数(@PathVariable())

二.跳转

2.1转发

2.2重定向

2.3跳转细节

三.传值

3.1Request和Session

3.2JSP中取值(EL JSTL )

3.3Model

3.4ModelAndView

3.5@SessionAttributes


一.接收请求参数

1.1基本类型参数

  1. 请求参数和方法的形参 同名即可

  2. springMVC默认可以识别的日期字符串格式为: YYYY/MM/dd HH:mm:ss 通过@DateTimeFormat可以修改默认日志格式

  /**
     * @description: 获取参数数据,只需要在处理请求的方法中,加入对应参数的数据类型和参数名即可,
     *              注意方法参数列表的变量名要和传入的参数的k值相同才能获取到,否则为null
     *              ,如:xxxx/aaaa?name=李四&age=14===↓
     *              @requestMapping("/aaaa")
     *              public String demo2( String name){
     *              =====>在方法内name就有值为李四
     *              }
     *
     *              */
    @RequestMapping("/d2")
    public String demo2(String name, int age, @DateTimeFormat(pattern = "yyyy-MM-dd") Date bornDate){
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        System.out.println("bornDate:"+bornDate);
        return "demo";
    }

1.2实体收参*

请求参数和实体的属性 同名即可

  /**
     * @description: 获取参数数据,k=v k如果和对象的属性名一致如 ename=张三,user对象中有ename属性,
     *              spring就会自动把ename属性的值封装到对象User里面
     */
//http://localhost:8989/.../test2?id=1&name=zzz&gender=false&birth=2018-12-12 12:20:30
    @RequestMapping("/d3")
    public String demo3(User u){
        System.out.println(u);
        return "demo";
    }

1.3数组收参

<form>
    ......
    <input type="checkbox" name="hobby" value="fb"/>足球 
    <input type="checkbox" name="hobby" value="bb"/>篮球 
    <input type="checkbox" name="hobby" value="vb"/>排球
    
</form>
//http://localhost:8989/.../test3?hobby=football&hobby=basketball
@RequestMapping("/test3")
public String testParam3(String[] hobby){
    for(String h:hobby){
        System.out.print(h+" ");
    }
    return "index";
}

1.4路径参数(@PathVariable())

// {id} 定义名为id的路径;【/hello/{id}】的匹配能力和【/hello/*】等价
// http://localhost:8080/.../hello/10   {id}匹配到10
@RequestMapping("/hello/{id}")
// @PathVariable将{id}路径匹配到值赋给id参数
// 路径名和参数名相同则@PathVariable("id")可简写为 @PathVariable
public String testParam5(@PathVariable("id") Integer id){
    System.out.println("id:"+id);            
    return "index";
}

// http://localhost:8080/.../hello/tom   {username}匹配到tom
@RequestMapping("/hello/{username}")
public String testParam6(@PathVariable("username") String name){//将{username}路径匹配到的值赋给name参数
    System.out.println("username:"+name);
    return "index";
}

二.跳转

2.1转发

@Controller
@RequestMapping("/forw")
class ForwardController{
    @RequestMapping("/test1")
    public String testForward(){
        System.out.println("test forward1");
        // 转发跳转 /views/users.jsp
        // return "views/users";//和下一行等价
        return "forward:/views/users.jsp";
    }
    @RequestMapping("/test2")
    public String testForward2(){
        System.out.println("test forward2");
        //return "forward:test1";//相对路径(转发到本类中的test1)
        //转发到  /forw/test1
        return "forward:/forw/test1"; //绝对路径
    }
}

2.2重定向

@Controller
@RequestMapping("/redir")
class RedirectController{
    @RequestMapping("/test1")
    public String testRedirect1(){
        System.out.println("test redirect1");
        //重定向到 /redir/test1
        //return "redirect:test1"; //相对路径(转发到本类中的test1)
        return "redirect:/redir/test1";//绝对路径
    }
    @RequestMapping("/test2")
    public String testRedirect2(){
        System.out.println("test redirect2");
        //重定向到 /views/users.jsp
        return "redirect:/view/user.jsp";
    }
}

2.3跳转细节

  1. 在增删改之后,为了防止请求重复提交,重定向跳转
  2. 在查询之后,可以做转发跳转

三.传值

Controller得到数据后,跳转到View,并向View传递数据。进而View中可以渲染数据,让用户看到含有数据的页面

  • 转发跳转:Request作用域
  • 重定向跳转:Session作用域

3.1Request和Session

//形参中 即可获得 request 和 session对象
@RequestMapping("/test1")
public String testData(HttpSession session,HttpServletRequest req,Integer id){
    session.setAttribute("user",new User());
    req.setAttribute("age", 18);
    req.setAttribute("users",Arrays.asList(new User(),new User()));
    return "test2";
}

3.2JSP中取值(EL JSTL )

//jsp中用EL表达式 取值即可
<fmt:formatDate value="${sessionScope.user.birth}" pattern="yyyy-MM-dd"/> <br/>
${sessionScope.user.birth} <br>
${requestScope.age}

3.3Model

//model中的数据,会在V渲染之前,将数据复制一份给request
@RequestMapping("/test")
public String testData(Model model){
    model.addAttribute("name", "张三");
    return "index";
}

//jsp中用EL表达式 取值即可
${requestScope.name}

3.4ModelAndView

//modelandview 可以集中管理 跳转和数据
@RequestMapping("/test")
public ModelAndView testData(){//返回值类型为ModelAndView
    //新建ModelAndView对象
    ModelAndView mv = new ModelAndView();
    // 设置视图名,即如何跳转
    mv.setViewName("forward:/index.jsp");
    // 增加数据
    mv.addObject("age",18);
    return mv;
}

//jsp中用EL表达式 取值即可
${requestScope.age}

3.5@SessionAttributes

  • @SessionAttributes({"gender","name"}) :model中的 name和gender 会存入session中
  • .setComplete()移除session
@Controller
@SessionAttributes({"gender","name"}) // model中的 name和gender 会存入session中
public class UserController {

    @RequestMapping("/hello")
    public String hello(Model m){
        m.addAttribute("gender",true); // 会存入session
        mv.addObject("name","zhj"); // 会存入session
        return "index";
    }
    
    @RequestMapping("/hello2")
    public String hello(SessionStatus status){
        // 移除通过SessionAttributes存入的session
        status.setComplete();
        return "index";
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值