新建项目:

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开发工具的更多相关文章

  1. SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...

  2. Springboot下载Excel的3种方式

    Springboot下载Excel的3种方式 汇总一下浏览器下载和代码本地下载实现的3种方式. (其实一般都是在代码生成excel,然后上传到oss,然后传链接给前台,但是我好像没有实现过直接点击就能 ...

  3. springboot成神之——mybatis在spring-boot中使用的几种方式

    本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...

  4. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  5. 【SpringBoot】05.SpringBoot整合Listener的两种方式

    SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 创建一个类实现ServletContextListener (具体实现哪个Listener根据情况 ...

  6. 【SpringBoot】03.SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...

  7. MATLAB 显示输出数据的三种方式

    MATLAB 显示输出数据的三种方式 ,转载 https://blog.csdn.net/qq_35318838/article/details/78780412 1.改变数据格式 当数据重复再命令行 ...

  8. Python 输出百分比的两种方式

    Python 输出百分比的两种方式 注: 在python3环境下测试. 方式1:直接使用参数格式化:{:.2%} {:.2%}: 显示小数点后2位 显示小数点后2位: >>> pri ...

  9. Json传递数据两种方式(json大全)

    1.Json传递数据两种方式(json大全)----------------------------字符串 var list1 = ["number","name&quo ...

  10. Asp.net MVC在Razor中输出Html的两种方式

    http://qubernet.blog.163.com/blog/static/177947284201485104616368/ Razor中所有的Html都会自动编码,这样就不需要我们手动去编码 ...

随机推荐

  1. Python中的try-finally

    >>> try: ... raise KeyboardInterrupt ... finally: ... print('Goodbye, world!') ... Goodbye, ...

  2. Delphi UTF编码 UTF8Encode、UTF8Decode、URLEncode、URLDecode

    一.URL简介    URL是网页的地址,比如 http://www.cnblogs.com.Web 浏览器通过 URL 从 web 服务器请求页面.    由于URL字符串常常会包含非ASCII字符 ...

  3. 部署.net Core 到 Windows server 2008 r2 IIs

    1. 将项目发布 2.iis 新建网站,设置 3 安装windows  server  hosting 4 关于报错 1.下载sdk https://www.microsoft.com/net/lea ...

  4. SCP-bzoj-1084

    项目编号:bzoj-1084 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 分类讨论+DP.#滑稽 预处理前缀和s[i][s]=Σa[j][s](∀j∈[1,i])(m=1时略去第二维) 对 ...

  5. AcWing 209. 装备购买 (高斯消元线性空间)打卡

    脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量z[i]=(ai,1,ai,2,..,ai,m)z[i]=(ai,1,ai,2,..,ai,m) 表示,每个装备需要 ...

  6. 搭建Linux C语言开发环境

    1.操作系统 Windows操作系统:windows 7 and windows 10 2.开发工具和编译工具 开发工具:notpad++ 和 vim 编译工具:Cygwin64 Terminal 3 ...

  7. Area的使用

    本文转自-->王亮的博客文章[ASP.NET MVC 小牛之路]08 - Area 使用 文章引导 MVC路由解析---IgnoreRoute MVC路由解析---MapRoute MVC路由解 ...

  8. MFC DLL 导出函数的定义方式

    一直在鼓捣DLL,每天的工作都是调试一个一个的DLL,往DLL里面添加自己的代码,但是对于DLL一直不太了解啊!今天一查资料,才发现自己对于DLL编写的一些基本知识也不了解.要学习,这篇文章先总结DL ...

  9. Scrapy框架: pipelines.py设置

    保存数据到json文件 # -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your p ...

  10. expect的模式

    expect的模式其实就是对话模式: expect    # 期望什么 send      # 我给你什么 比如: expect "password: "    # 碰到什么了 s ...