Intellij IDEA 学习Spring Boot (1)
来源:互联网 发布:淘宝网购物夏装 编辑:程序博客网 时间:2024/06/07 05:01
工作之余,学习一下,使用Intellij IDEA 新建一个 Spring Boot项目,算是对 Spring Boot 入一个门
新建项目
完成之后,效果如下:
直接运行:
(这里我没有关注Maven,是因为我就默认使用了Intellj IDEA 自带的Maven工具了,不想配了,大不了下载的东西 会放在C盘用户目录下一个叫.m2的文件夹。我们用 spring boot 就是为了 简化各种配置的。你看上面,我们只需要 写写 项目应用的名字,然后就OK了。不要有强迫症,你看,这个spring boot,我即没有关注maven,也没有关注tomcat,为什么? 这些根本就不是我们操心的,人家都集成好了。)
浏览器运行:
这个404很明白的啦,没有页面。我们细看一下
接下来,我们写点东西。
先不考虑页面。先写个Controller。
@Controllerpublic class HelloController { @RequestMapping("/") @ResponseBody public String index(){ return "Hello World!"; }}这里先不返回view,返回数据。重新运行。
接下来考虑 返回view的情况。
大多数 thymeleaf 模板引擎来替换掉 jsp。我们就当学习一下潮流吧。这东西其实也不难。就是不熟练而已。
接下来,我们重新做一些有意义的事情。
在HelloController中添加一个 requestMapping
@RequestMapping("/index.html")public String toIndex(){ return "index";}
那么接下来return "index" 就是 index.html放在哪呢。注意不是index.jsp。就是index.html。
重新运行。
那么这里有些要注意了。不能出现单标签的了,因为thymeleaf引擎比较严格。以后<input>这种肯定不行了,必须类似这样<input></input>或者<input /> 所以我们修改一下
但是你发现刷新并没有效果。必须重新运行。
运行成功。但这就不爽勒,为什么不能像以前tomcat那样,改了刷新就可以看到效果呢。那么这种类似 热部署操作是需要配置的。参考 http://www.cnblogs.com/bingshu/p/6876030.html。
1. 修改pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope></dependency>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins></build>2. 修改idea。
1. 快捷键: Ctrl+Alt+S
OK完之后,
2. 快捷键:Ctrl+Shift+Alt+ /
OK之后,再重启项目试试。 你可以发现,你修改java代码,项目会重新部署,修改html文件,刷新浏览器,效果也会修改的。完美实现 “热部署”
这不算什么。我们还没有用上 那个新模板引擎的语法呢。 先搞点简单的,复杂的 各位还是请百度。
public class Student { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}新建了一个类Student。然后controller中加入这样的代码。
@RequestMapping("/students.html")public String students(Map<String,Object> map){ List<Student> list = new ArrayList<>(); for(int i=0;i<10;i++){ Student student = new Student(); student.setName("张三"+i); student.setAge(23+i); list.add(student); } map.put("sList",list);// 返回给页面的数据 return "students";}下面编写 students.html页面
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8" /> <title>Title</title></head><body> 所有学生 <ul th:each="stu,stuSta:${sList}"> <li> 序号:<span th:text="${stuSta.index}"/><br/> 姓名:<th:block th:text="${stu.name}"></th:block><br/> 年龄:<div th:text="${stu.age}"></div><br/> </li> </ul></body></html>
不要纠结着写红线了,不是错误。这是 这个集成工具的问题,不要纠结,没有错。修复也行,alt+enter,但那句代码都是注释状态,毫无意义,还多上了java类,
真的,这样我就觉得 毫无意义了,明明一个纯html文件里,没必要出现这些类名的吧。
所以,不要纠结了,看着看着就习惯了。
浏览器访问
接下来,我们讨论一个常用功能那就是 日志功能。
spring boot 日志已经集成了,什么log4j,slf4j,logback什么的,所以不需要导什么包,直接用。
like this.
Logger logger = LoggerFactory.getLogger(getClass());@RequestMapping("/students.html")public String students(Map<String,Object> map){ List<Student> list = new ArrayList<>(); for(int i=0;i<11;i++){ Student student = new Student(); student.setName("张三"+i); student.setAge(23+i); list.add(student); } map.put("sList",list);// 返回给页面的数据 logger.info("studentList: {}",list); return "students";}
那日志级别什么的怎么调整呢。 这里 我们 做一个配置,当然不需要新建 log4j.properties。没有用。
我们写点东西,这个 是带提示的哦。
我们简单的写点东西
#loglogging.file=mylog.loglogging.path=logslogging.level.root=info#thymeleafspring.thymeleaf.cache=false
再写一个简单的例子
@RequestMapping("/student/{name}.html")public String student(@PathVariable(name="name") String name, Map<String,Object> map){ Student student = new Student(); student.setName(name); student.setAge(23); map.put("student",student); return "student";}
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8" /> <title><th:block th:text="${student.name}"/>的信息</title></head><body> 该学生信息 姓名:<span th:text="${student.name}"></span> 年龄:<span th:text="${student.age}"></span></body></html>
好了,这个入门就到这了。下次继续聊 spring boot。
- Intellij IDEA 学习Spring Boot (1)
- IntelliJ IDEA Spring Boot学习(4) 集成mybatis
- Intellij Idea spring boot 报错记录
- Intellij idea + Spring boot + Thymeleaf + MySQL
- Spring Boot + Intellij IDEA 热部署
- IntelliJ IDEA + Spring Boot + Mybatis的整合
- IntelliJ Idea 实现热部署 Spring Loaded spring-boot-devtools
- 【Spring】HelloSpring:使用IntelliJ IDEA学习Spring
- intellij idea + bitbucket + maven + spring-boot配置记录
- intellij idea创建运行spring boot项目时报错
- Intellij IDEA 使用Spring-boot-devTools无效解决办法
- Intellij IDEA 使用Spring-boot-devTools无效解决办法
- 利用Intellij Idea构建一个Spring Boot构建
- 使用 intellij idea IDE 搭建 Spring boot 项目
- spring boot 入门(一):IntelliJ IDEA 实现hello world
- IntelliJ IDEA搭建Spring Boot的小Demo
- Intellij IDEA 使用Spring-boot-devTools无效解决办法
- intellij idea + spring boot + mybatis + druid + maven + mysql + thymeleaf
- js字符串类型
- 文章标题
- Sqlite3 语句
- Web Scraping with Python: 使用 Python 爬 Baidu 关键词
- 关于list.add添加的数据都一样的问题
- Intellij IDEA 学习Spring Boot (1)
- enum在C语言中的用法
- TensorFlow添加层
- 400位京东技术专家心血之作 《决战618:探秘京东技术取胜之道》重磅发售!
- Adobe Flash Player 26 on Fedora 26/25, CentOS/RHEL 7.4/6.9
- 使用HttpServletResponse响应数据--FastJson
- js实现字符串重复的repeat函数
- Qt-状态栏的应用
- java API开发文档的制作