springboot输出hello world,3种方式(String,JSON,jsp),IDEA开发工具
新建项目:
File -> new -> Project -> Spring Initializr -> Next -> Next -> Next
-> Project name: demo
Project location: D:/project/demo
->Finish
编写时候,class自动导入包的引用
File -> Settings -> Editor -> General -> Auto Import ->
(Add unambiguous imports on the fly和Optimize imports on the fly 打钩) -> OK
1.String方式
在com.example.demo目录下新建HelloController class文件
package com.example.demo; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
idea右上角,启动项目,http://localhost:8080/hello 访问
2.JSON方式
在com.example.demo目录下新建Hello class文件
package com.example.demo; public class Hello{
private String name; public Hello() {} public Hello(String name) {
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
在com.example.demo目录下新建HelloController2 class文件
package com.example.demo; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController2 { @RequestMapping("/hello2")
public Hello hello(){
Hello hello = new Hello("hello world");
/*
或
Hello hello = new Hello();
hello.setName("hello world");
*/
return hello;
}
}
idea右上角,启动项目,http://localhost:8080/hello2 访问
3.jsp方式
pom.xml中增加
<!-- jstl JSP标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 返回jsp页面还需要这个依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
pom.xml中有关artifactId找不到
1).file -> settings -> 搜索maven -> always update snapshots 打钩 -> OK
2).右下角选择 import...
resources application.properties中增加
server.port=8080
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
src.main目录下新建webapp,webapp下新建WEB-INF,
WEB-INF下新建hello.jsp
在项目中鼠标右键new的时候无JSP
File -> Project structure -> Modules -> Web -> Web Resource Directories -> + 选择项目 -> OK
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
hello world
</body>
</html>
在com.example.demo目录下新建HelloController3 class文件
package com.example.demo; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView; @RestController
public class HelloController3 { @RequestMapping("hello3")
public ModelAndView hello(){
ModelAndView model = new ModelAndView();
model.setViewName("hello");
return model;
}
}
idea右上角,启动项目,http://localhost:8080/hello3 访问
springboot输出hello world,3种方式(String,JSON,jsp),IDEA开发工具的更多相关文章
- SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...
- Springboot下载Excel的3种方式
Springboot下载Excel的3种方式 汇总一下浏览器下载和代码本地下载实现的3种方式. (其实一般都是在代码生成excel,然后上传到oss,然后传链接给前台,但是我好像没有实现过直接点击就能 ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- 【SpringBoot】05.SpringBoot整合Listener的两种方式
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 创建一个类实现ServletContextListener (具体实现哪个Listener根据情况 ...
- 【SpringBoot】03.SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...
- MATLAB 显示输出数据的三种方式
MATLAB 显示输出数据的三种方式 ,转载 https://blog.csdn.net/qq_35318838/article/details/78780412 1.改变数据格式 当数据重复再命令行 ...
- Python 输出百分比的两种方式
Python 输出百分比的两种方式 注: 在python3环境下测试. 方式1:直接使用参数格式化:{:.2%} {:.2%}: 显示小数点后2位 显示小数点后2位: >>> pri ...
- Json传递数据两种方式(json大全)
1.Json传递数据两种方式(json大全)----------------------------字符串 var list1 = ["number","name&quo ...
- Asp.net MVC在Razor中输出Html的两种方式
http://qubernet.blog.163.com/blog/static/177947284201485104616368/ Razor中所有的Html都会自动编码,这样就不需要我们手动去编码 ...
随机推荐
- 【leetcode】948. Bag of Tokens
题目如下: You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can ...
- SPI总线介绍和裸机编程分析
一.SPI总线结构 SPI(Serial Peripheral Interface)串行外设接口,是一种高速的,全双工,同步的通信总线.采用主从模式(Master Slave)架构,支持多个slave ...
- 提供 web前端、H5、html页面 技术服务
如有前端页面的需求请在评论区留言 第一时间进行回复
- 2018icpc南京/gym101981 I Magic Potion
题意: 若干个勇士,每个勇士只能杀特定的怪物.每个勇士只能杀1个怪,但是有一些药,喝了药之后能再杀一个,每个勇士只能喝一瓶药.问你最多杀多少怪. 题解: 按照如下建图套网络流板即可. 网上有题解说套D ...
- 大数据学习之BigData常用算法和数据结构
大数据学习之BigData常用算法和数据结构 1.Bloom Filter 由一个很长的二进制向量和一系列hash函数组成 优点:可以减少IO操作,省空间 缺点:不支持删除,有 ...
- Ettercap详细参数
关于界面:ettercap提供 4 种运行界面: Text #文本模式,参数 -T ,一般配合 -q(安静模式)使用 Curses/GTK #图形模式,参数 -C ...
- 力扣算法——142LinkedListCycleII
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 力扣算法题—148sort-list
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- camunda用户的一些简单操作
act_id_group:存放组信息act_id_membership:存放用户与组的相关信息act_id_user:存放用户act_id_info:存放用户个人信息act_id_tenant:存放租 ...
- 在webpack4 中利用Babel 7取消严格模式方法
报错信息: Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on stri ...