为了账号安全,请及时绑定邮箱和手机立即绑定

刚刚开始学习Springboot中添加 @restcontroller报错 ?

刚刚开始学习Springboot中添加 @restcontroller报错 ?

白猪掌柜的 2019-08-20 18:14:09
刚刚开始学习Springboot中添加 @restcontroller报错 
查看完整描述

4 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

其实简单粗暴的理解,
就是如果@RestController注解Controller,则返回的内容就是Return 里的内容。
例如:
@RestController
@RequestMapping
public class TestController {
@RequestMapping("/index")
public String index() {
return "user/hello";
}
}
页面显示的是user/hello字符串
如果@Controller注解Controller,则返回到指定页面。
例如:
@Controller
@RequestMapping
public class TestController {
@RequestMapping("/index")
public String index() {
return "user/hello";
}
}
显示user底下的hello页面上的内容

查看完整回答
反对 回复 2019-08-24
?
PIPIONE

TA贡献1829条经验 获得超9个赞

有两种方法:
1、return "redirect:/class/list.action?name=value"; 返回的时候直接通过?传递值
2、@RequestMapping(value="/delClass.action")
public String delClass(ModelMap model) {
model.addAttribute("name", "value");
return "redirect:/class/list.action";
}
方法2记忆中成功过,这样传值没有太多限制,像map一样,随便放就可以

查看完整回答
反对 回复 2019-08-24
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解。4.0之前的版本,Spring MVC的组件都使用@Controller来标识当前类是一个控制器servlet。 使用这个特性,我们可以开发REST服务的时候不需要使用@Controller而专门的@RestController。 当你实现一个RESTful web services的时候,response将一直通过response body发送。为了简化开发,Spring 4.0提供了一个专门版本的controller。下面我们来看看@RestController实现的定义: @Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController 官方解释: A convenience annotation that is itself annotated with @Controller and @ResponseBody. Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default. ...4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解。4.0之前的版本,Spring MVC的组件都使用@Controller来标识当前类是一个控制器servlet。 使用这个特性,我们可以开发REST服务的时候不需要使用@Controller而专门的@RestController。 当你实现一个RESTful web services的时候,response将一直通过response body发送。为了简化开发,Spring 4.0提供了一个专门版本的controller。下面我们来看看@RestController实现的定义: @Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController 官方解释: A convenience annotation that is itself annotated with @Controller and @ResponseBody. Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default. 注解本身使用@Controller和@ResponseBody注解。使用了这个注解的类会被看作一个controller-使用@RequestMapping的方法有一个默认的@ResponseBody注解。 @ResponseBody – As of version 4.0 this annotation can also be added on the type level in which case is inherited and does not need to be added on the method level. @ResponseBody也可以加到类一级,通过继承方法一级不需要添加。

查看完整回答
反对 回复 2019-08-24
  • 4 回答
  • 0 关注
  • 3030 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信