Spring MVC 的controller方法返回值

controller方法返回值

返回ModelAndView

  • 说明:controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view

返回字符串

逻辑视图名

  • 说明:controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。

  • 返回字符串

    @Controller
    @RequestMapping("/account")
    public class AccountController {
        @RequestMapping(value = "/findAccount2")
        public String findAccount2(Model model) {
            //添加数据
            model.addAttribute("msg", "欢迎你 springmvc");
            return "success";
        }
    }
  • 在index.jsp里面定义超链接

    <a href="/account/findAccount2">返回字符串</a>

Redirect重定向

  • 说明:

    • Contrller方法返回结果重定向到一个url地址,如下商品修改提交后重定向到商品查询方法,参数无法带到商品查询方法中。

    • redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。

    • 由于新发起一个request原来的参数在转发时就不能传递到下一个url,如果要传参数可以/item/queryItem后边加参数,如下:/item/queryItem?...&…..

  • 重定向

    @Controller
    @RequestMapping("/account")
    public class AccountController {
        
        @RequestMapping(value = "/findAccount3")
        public String findAccount3() {
            return "redirect:/account/findAccount4";
        }
    ​
        @RequestMapping(value = "/findAccount4")
        public String findAccount4(Model model) {
            //添加数据
            model.addAttribute("msg", "这是springmvc的重定向");
            return "success";
        }
    }
  • 在index.jsp里面定义超链接

    <a href="/account/findAccount3">重定向</a>

forward转发

  • 说明:

    • controller方法执行后继续执行另一个controller方法,如下商品修改提交后转向到商品查询页面,修改商品的id参数可以带到商品查询方法中。

    • forward方式相当于request.getRequestDispatcher().forward(request,response),转发后浏览器地址栏还是原来的地址。转发并没有执行新的request和response,而是和转发前的请求共用一个request和response。所以转发前请求的参数在转发后仍然可以读取到。

  • 重定向

    @Controller
    @RequestMapping("/account")
    public class AccountController {
        
        @RequestMapping(value = "/findAccount3")
        public String findAccount3() {
            return "forward:/account/findAccount4";
        }
    ​
        @RequestMapping(value = "/findAccount4")
        public String findAccount4(Model model) {
            //添加数据
            model.addAttribute("msg", "这是springmvc的重定向");
            return "success";
        }
    }
  • 在index.jsp里面定义超链接

    <a href="/account/findAccount3">重定向和请求转发</a>
  • 24
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring MVC中,Controller的序列化和反序列化是通过使用注解来实现的。对于请求体的反序列化,可以使用 @RequestBody 注解来接收请求体。而对于响应体的序列化,可以使用 @ResponseBody 注解将返回值作为响应体。 具体来说,在Spring MVC中,使用 @RequestBody 注解标注的Controller方法参数可以用来接收请求体。而使用 @ResponseBody 注解标注的Controller方法则会将返回值作为响应体。请求体和响应体还可以具有不同的内容类型,比如json、xml等等。 在Spring MVC中,Controller方法参数的解析统一使用HandlerMethodArgumentResolver接口。该接口定义了两个方法:supportsParameter用于判断是否支持给定的方法参数,resolveArgument用于解析方法参数。 对于使用 @RequestBody 注解标注的参数,同样也是通过HandlerMethodArgumentResolver进行解析。 在具体实现中,RequestResponseBodyMethodProcessor是一个处理器,它负责将Controller方法返回值转换为响应体。其中,核心代码如下: ```java public class RequestResponseBodyMethodProcessor { // ... public void writeWithMessageConverters(Object returnValue, MethodParameter returnType, ServletServerHttpResponse response) throws IOException, HttpMediaTypeNotAcceptableException { // ... } // ... } ``` 总结起来,Spring MVC使用注解来实现Controller的序列化和反序列化。对于请求体的反序列化,可以使用 @RequestBody 注解标注参数;而对于响应体的序列化,可以使用 @ResponseBody 注解标注方法。具体的参数解析和返回值转换则是通过HandlerMethodArgumentResolver和RequestResponseBodyMethodProcessor来完成的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring MVC @RequestBody @ResponseBody 序列化反序列化实现](https://blog.csdn.net/zzuhkp/article/details/122935899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值