Spring Boot输出hello world,3种方式(String,JSON,jsp),IDEA开发工具

新建项目:

 

 

 

 

 

 

 

 

 

 

 下图是写代码时候,class自动导入包的设置(写方法,会自动import包)

 

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>

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 访问

posted @ 2019-04-03 11:25  大飞90  阅读(678)  评论(0编辑  收藏  举报