我是靠谱客的博主 慈祥钢笔,这篇文章主要介绍Requestbody,PathVariable,SessionAttribute,现在分享给大家,希望可以做个参考。

Requestbody一般用于异步,
在jsp页面设计一个表单:

<form action="anno/testRequestBody" method="post">
        用户姓名:<input type="text" name="username" /><br/>
        用户年龄:<input type="text" name="age" /><br/>
        <input type="submit" value="提交" />
    </form>

/**
     * 获取到请求体的内容
     * @return
     */
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body){
        System.out.println("执行了...");
        System.out.println(body);
        return "success";
    }

@RequestBody 的作用就是使String body变成一个请求体,使得body能够打印出来。username=hh&age=22
注意username=中文,会乱码。

PathVariable用于绑定url中的占位符。
这是一种新的testful编程风格。
jsp文件:

<a href="anno/testPathVariable/10">testPathVariable</a>

即在地址后添加id,
对应的控制器为:

/**
     * PathVariable注解
     * @return
     */
    @RequestMapping(value="/testPathVariable/{sid}")
    public String testPathVariable(@PathVariable(name="sid") String id){
        System.out.println("执行了...");
        System.out.println(id);
        return "success";
    }
value="/testPathVariable/{sid}"

@PathVariable(name="sid") String id是重点。

SessionAttribute用于方法之间的共享。

最后

以上就是慈祥钢笔最近收集整理的关于Requestbody,PathVariable,SessionAttribute的全部内容,更多相关Requestbody内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部