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都会自动编码,这样就不需要我们手动去编码 ...
随机推荐
- enumerate()(Python)
>>> E=enumerate('spam') >>> E <enumerate object at 0x1021ceca8> >>> ...
- 【leetcode】1003. Check If Word Is Valid After Substitutions
题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...
- org.zkoss.zul.Filedownload.java 源码
/* Filedownload.java Purpose: Description: History: Mon Apr 16 09:29:44 2007, Created by tomyeh Copy ...
- 【LeetCode 33】搜索旋转排序数组
题目链接 [题解] 会发现旋转之后,假设旋转点是i 则0..i-1是递增有序的.然后i..len-1也是递增有序的. 且nums[i..len-1]<nums[0]. 而nums[1..i-1] ...
- Sqli labs系列-less-5&6 报错注入法(下)
我先输入 ' 让其出错. 然后知道语句是单引号闭合. 然后直接 and 1=1 测试. 返回正常,再 and 1=2 . 返回错误,开始猜表段数. 恩,3位.让其报错,然后注入... 擦,不错出,再加 ...
- Who Saw My Blog
I found that my blog has visitors!!! I wonder who has watched my blog and what did they feel at that ...
- docker-compose快速搭建 Prometheus+Grafana监控系统
一.说明Prometheus负责收集数据,Grafana负责展示数据.其中采用Prometheus 中的 Exporter含:1)Node Exporter,负责收集 host 硬件和操作系统数据.它 ...
- SAS创建和使用索引(SAS INDEX)
一.概述 在合并数据集的时候,可以使用DATA步,但使用DATA 步时需要对KEY VALUE 排序,且KEY VALUE 的名字也必须一致:也可以用PROC SQL ,不需要进行上述排序.重命名的步 ...
- Callable创建线程
(1)Callable接口更像是Runnable接口的增强版,相比较Runable接口,Call()方法新增捕获和抛出异常的功能;Call()方法可以返回值<br> (2)Future接口 ...
- 18-python基础-字符串(1)
1.字符串的定义 字符串就是一串字符,是编程语言中表示文本的数据类型. 在python中可以使用一对双引号“”或者一对单引号‘’定义一个字符串. 可以使用索引获取一个字符串中指定位置的字符,索引计数从 ...