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的更多相关文章

  1. 干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结

    目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方法引用) 四.Lambda表达式 五.泛型 六.自动释放 七.重写(override) ASP.NET CORE ...

  2. ASP.NET CORE(C#)与Spring Boot MVC(JAVA)

    干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结   目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方 ...

  3. 玩转spring boot——MVC应用

    如何快速搭建一个MCV程序? 参照spring官方例子:https://spring.io/guides/gs/serving-web-content/ 一.spring mvc结合thymeleaf ...

  4. Spring Boot MVC 使用 JSP 作为模板

    Spring Boot 默认使用 Thymeleaf 作为模板引擎,直接在 template 目录中存放 JSP 文件并不能正常访问,需要在 main 目录下新建一个文件夹来存放 JSP 文件,而且需 ...

  5. spring boot mvc系列-静态资源配置与MappingHandler拦截器

    静态资源配置 Spring Boot 默认将 /** 所有访问映射到以下目录: classpath:/static classpath:/public classpath:/resources cla ...

  6. Spring Boot MVC api返回的String无法关联到视图页面

    1:问题 使用 @Restcontroller 返回值定义为String 时 无法返回具体的页面 @RestController public class HelloController { @Get ...

  7. Spring Boot Mvc 单元测试

    https://blog.csdn.net/hfmbook/article/details/70209162

  8. Spring Boot MVC 单张图片和多张图片上传 和通用文件下载

    @Autowired private ServerConfig serverConfig; /** * 通用下载请求 * * @param fileName 文件名称 * @param delete ...

  9. 玩转spring boot——开篇

    很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...

随机推荐

  1. Raft Paper 简译

      本文是对 Raft Paper 核心部分的意译,不包括原文中的如下章节:<3 Paxos 的优缺点论述>.<4 Raft 的易理解性介绍>.<9 Raft 算法的易理 ...

  2. Java业务代理模式~

    业务代理模式用于解耦表示层和业务层. 它基本上用于减少表示层代码中的业务层代码的通信或远程查找功能.在业务层有以下实体. 客户端(Client) - 表示层代码可以是JSP,servlet或UI ja ...

  3. Ubuntu18.10 编译libevent出现错误: creating symbolic link XXXXXX : Operation not supported

    今天在VirtualBox虚拟机下的Ubuntu18.10编译libevent源代码时,按照github中使用cmake方式: $ mkdir build && cd build $ ...

  4. Excel文件加密后忘记密码破解方法

    最好使用VBA 工程密码破解方法 新建一个excel文档,然后打开,同时按Alt和F11,进入VBA界面 点击菜单上的插入,模块 在新的窗口粘贴以下代码: Sub crack() Dim i As L ...

  5. C++中的临时对象

    1,临时对象神秘在于不知不觉就请入程序当中,并且给程序带来了一定的问题: 2,下面的程序输出什么?为什么? #include <stdio.h> class Test { int mi; ...

  6. How To Release and/or Renew IP Addresses on Windows XP | 2000 | NT

    Type 'ipconfig' (without the quotes) to view the status of the computer's IP address(es). If the com ...

  7. 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化

    Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...

  8. Linux学习笔记之认识与学习Bash

    什么是shell:shell是一个翻译器,将所敲的命令翻译成CPU能理解的语言,之后CPU再去执行,CPU执行后返回给shell,shell再翻译成我们所能理解的语言并显示:终端并不是shell,而是 ...

  9. Java缓冲流的优点和原理

    不带缓冲的流的工作原理: 它读取到一个字节/字符,就向用户指定的路径写出去,读一个写一个,所以就慢了. 带缓冲的流的工作原理: 读取到一个字节/字符,先不输出,等凑足了缓冲的最大容量后一次性写出去,从 ...

  10. Codeigniter项目使用phpDocumentor生成api文档

    前言 运行环境: vagrant 2.2.4 virtualbox 6.0 box bento/ubuntu-16.04 (Apache 2.4.18 + Mysql 5.7.26 + PHP 5.6 ...