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文件

方法1:
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;
}
}


方法2:
// 这里需要注意是Controller
@Controller
public class TestController {

@RequestMapping("/hello3")
public String hello(HttpServletRequest request){
// 输出的参数
request.setAttribute("name", "test");
return "hello";
}
}

idea右上角,启动项目,http://localhost:8080/hello3 访问

原文地址:https://www.cnblogs.com/songfei90/p/10647722.html