Controller的使用
来源:互联网 发布:linux查看ftp用户权限 编辑:程序博客网 时间:2024/05/22 12:53
对于只使用@Controller注解控制器,必须配套有模板(如JSP),否则报404错误
而使用@RestController注解则不须配有模板
只使用@Controller注解
须添加模板,此处为如Spring官方的Thyemleaf模板(替代JSP的好东西),添加jar包
目录结构:
模板文件:index.heml
<h1>hello Spring Boot!</h1>
控制器类:
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;/** * Created by Shusheng Shi on 2017/5/3. */@Controllerpublic class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String say() { return "index"; }}
运行结果
现在流行前后端分离,后端只需要提供rest接口,返回一些json格式给前端,不需要模板,会影响性能
若将控制器类里的方法写成如下所示(即无method属性)
@RequestMapping(value = "/hello") public String say() { return girlProperties.getCupSize(); }
那么使用GET/POST方式访问皆可.不过并不推荐如此写法,毕竟两种方法适合于不同需求
控制器方法
@RequestMapping(value = "/{id}/say", method = RequestMethod.GET) public String say(@PathVariable("id") int id) { return "id: " + id; }
运行结果
控制器方法
@RequestMapping(value = "/say", method = RequestMethod.GET) public String say(@RequestParam("id") int myId) { return "id: " + myId; }
运行结果
设置id默认值
@RequestMapping(value = "/say", method = RequestMethod.GET)public String say(@RequestParam(value = "id", required = false, defaultValue = "0") int myId) { return "id: " + myId; }
运行结果
另替代写法
// @RequestMapping(value = "/say", method = RequestMethod.GET) @GetMapping(value = "/say")
0 0
- angular controller的使用
- Controller的使用
- SpringMVC-@Controller的使用
- Controller的使用
- springBoot Controller的使用
- springboot-controller的使用
- Controller的使用
- Controller的使用
- Collection View Controller的使用
- 使用【SpringMVC】的【Controller注解】
- 使用controller建立面向目标的场景
- codeigniter中控制器(controller)的使用
- Tab Bar Controller(选项卡)的使用
- Spring的注解Controller中使用AOP
- spring的controller中使用aop
- iOS中Container View Controller的使用
- 使用Spring AOP预处理Controller的参数
- Spring Boot-Controller控制器的使用
- Adaboost 算法的原理与推导
- SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
- 463. Island Perimeter
- C++知识点:数组末端下一位置
- join查询要点整合
- Controller的使用
- 大多数的愤怒源于自己的无知——《Excel受保护视图》
- 463. Island Perimeter
- 图的遍历
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- Shiro框架学习-身份验证
- Android 动画简单实现
- CF
- 【算法】将一维数组arr中的元素循环左移p个位置