spring boot MVC
1 spring boot的用途
第一,spring boot可以用来开发mvc web应用。
第二,spring boot可以用来开发rest api。
第三,spring boot也可以用来开发web app。
2 @Controller和@RestController注解
要返回jsp用@Controller注解,要返回json用@RestController注解。
3 spring boot返回jsp页面
3.1 配置
在src/main/resources下面新建application.properties文件,设置
spring.mvc.view.prefix:/index/ //告诉springboot去哪里取jsp,即前缀。
spring.mvc.view.suffix: .jsp //告诉springboot返回的文件的类型,这里是.jsp
3.2 控制器用@Controller
@Controller
public class UserController {
@RequestMapping("/index")
return "user";
}
这里返回的"user"其实是告诉spring boot去/index下面找user.jsp返回。
4 springboot controller注解
4.1 @Controller
返回jsp页面。
4.2 @RestController
返回json。
4.3 @RequestMapping
配置url映射。
4.4@RequestParam
获取请求参数。
5 在controller中给jsp数据的方式
5.1 交数据的两种方式
第一,由controller函数的形参传入的对象提交数据,这个时候参数的类型可以是Map<String, Object>、Model和ModelMap。
@RequestMapping("/index")
public String index(Map<String, Object> map) {
map.put("name", "chao");
return "index";
}
@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("name", "chao");
return "index";
}
@RequestMapping("/index")
public String index(ModelMap map) {
map.addAttribute("name", "chao");
return "index";
}
第二,由controller函数的返回值对象体检数据,这个时候只能是ModelAndView。
@RequestMapping("/index")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView("/index"); //这里“ /index”指定了返回的view为index.jsp
modelAndView.addObject("name", "chao");
return modelAndView;
}
spring boot MVC的更多相关文章
- 干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结
目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方法引用) 四.Lambda表达式 五.泛型 六.自动释放 七.重写(override) ASP.NET CORE ...
- ASP.NET CORE(C#)与Spring Boot MVC(JAVA)
干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结 目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方 ...
- 玩转spring boot——MVC应用
如何快速搭建一个MCV程序? 参照spring官方例子:https://spring.io/guides/gs/serving-web-content/ 一.spring mvc结合thymeleaf ...
- Spring Boot MVC 使用 JSP 作为模板
Spring Boot 默认使用 Thymeleaf 作为模板引擎,直接在 template 目录中存放 JSP 文件并不能正常访问,需要在 main 目录下新建一个文件夹来存放 JSP 文件,而且需 ...
- spring boot mvc系列-静态资源配置与MappingHandler拦截器
静态资源配置 Spring Boot 默认将 /** 所有访问映射到以下目录: classpath:/static classpath:/public classpath:/resources cla ...
- Spring Boot MVC api返回的String无法关联到视图页面
1:问题 使用 @Restcontroller 返回值定义为String 时 无法返回具体的页面 @RestController public class HelloController { @Get ...
- Spring Boot Mvc 单元测试
https://blog.csdn.net/hfmbook/article/details/70209162
- Spring Boot MVC 单张图片和多张图片上传 和通用文件下载
@Autowired private ServerConfig serverConfig; /** * 通用下载请求 * * @param fileName 文件名称 * @param delete ...
- 玩转spring boot——开篇
很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...
随机推荐
- c#生成html静态文件时出现空白行,怎么去掉utf-8中的bom
public static void UTF8RemoveBOM(string filepath) { UTF8RemoveBOM(filepath, filepath); } public st ...
- Python基础代码1
Python基础代码 import keyword#Python中关键字 print(keyword.kwlist) ['False', 'None', 'True', 'and', 'as', 'a ...
- Vue中src属性绑定的问题
地址:https://blog.csdn.net/qq_25479327/article/details/80082520 地址:https://blog.csdn.net/sinat_3655513 ...
- Ptyhon 合并列表
2019-08-25 list1 = [91, 95, 97, 99] list2 = [92, 93, 96, 98] 合并后得到:[91, 95, 97, 99 , 92, 93, 96, 9 ...
- springCloud的使用02-----服务消费者(rest+ribbon)
1 将服务提供者做成集群模式 配置service-hi的端口为8762进行启动,配置service-hi的端口为8763进行启动, service-hi会在ecureka server上注册两个ser ...
- java并发编程之美-阅读记录6
java并发包中锁 6.1LockSupport工具类 该类的主要作用就是挂起和唤醒线程,该工具类是创建锁和其他工具类的基础.LockSupport类与每个使用他的线程都关联一个许可证,在默认情况下调 ...
- MyEclipse安装jrebel7.0.2插件
1 安装: windows --> install from site 填入网址 http://update.zeroturnaround.com/update-site-archive/upd ...
- 【记录】MongoDB
什么情况建议使用MongoDB? 1:满足对数据库的高并发读写 2:对海量数据的高效存储和访问 3:对数据库高扩展性和高可用性 4:灵活的数据结构,满足数据结构不固定的场景 5:应用需要2000-30 ...
- PHP 接口签名验证
目录 概览 常用验证 单向散列加密 对称加密 非对称加密 密钥安全管理 接口调试工具 在线接口文档 扩展 小结 概览 工作中,我们时刻都会和接口打交道,有的是调取他人的接口,有的是为他人提供接口,在这 ...
- LNMP环境安装
一.LNMP 1.使用LNMP的优点: 资源占用少 更多并发 代理服务器 热启动 稳定高效 负载均衡 邮件服务器 2.安装前准备: 2.1查看防火墙(默认开启): [root@localhost ~] ...