SpringBoot返回Json和Jsp页面数据

版权声明:原创,转载需告知 https://blog.csdn.net/qq_42136250/article/details/89641745

(一)SpringBoot返回json数据

  1. pom.xml引入依赖项
<parent>
        <artifactId>springboot_parent</artifactId>
        <groupId>cn.lzj.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_02_web</artifactId>

    <dependencies>

        <!--内置tomcat和SpringMVC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- servlet 依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- tomcat 的支持. -->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
            <!--<scope>provided</scope>-->
        <!--</dependency>-->
        <!--<dependency>-->
            <!--<groupId>org.apache.tomcat.embed</groupId>-->
            <!--<artifactId>tomcat-embed-jasper</artifactId>-->
            <!--<scope>provided</scope>-->
        <!--</dependency>-->
    </dependencies>
  1. controller
package cn.lzj.springboot.controller;

import cn.lzj.springboot.domain.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/*
 * 如果使用@RestController   方法上面就可以不用再添加@ResponseBody和@Controller
 *       @RestController  相当于是@Controller + @ResponseBody
 * */
@RestController //@RestController=@Controller+@ResponseBody 官方推荐使用
@RequestMapping("/json2")
public class JsonController2 {

    //字符串
    @RequestMapping("/str")
    public String json1(){
        return "this moment , the world has nothing to do with me";
    }
    //对象-日期

    @RequestMapping("/obj")
    public Person json2(){
            return new Person(1L,"木兰",new Date());
    }

    //数组
    @RequestMapping("/array")
    public List<Person> json3(){
        return Arrays.asList( new Person(1L,"工藤静香",new Date())
                ,new Person(2L,"木村拓哉",new Date())
                ,new Person(2L,"木村希美",new Date())
        );
    }
}

注意设置日期的返回格式:

@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
public Date getBirthDay() {
    return birthDay;
}

(二)SpringBoot集成jsp

  1. pom.xml
 <parent>
        <artifactId>springboot_parent</artifactId>
        <groupId>cn.lzj.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_03_webjsp</artifactId>
    <packaging>war</packaging>

    <name>springboot_03_webjsp Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- servlet 依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- tomcat 的支持. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>springboot_03_webjsp</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
  1. application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
  1. Controller
package cn.lzj.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/jsp")
public class JspController {

    @RequestMapping("/index")
    public String jsp(Model model){
        model.addAttribute("msg", "木村拓哉");
        return "user/index";
    }
}
  1. jsp页面准备
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
	<head>
	    <title>哈哈</title>
	</head>
	<body>
	  	${msg}
	</body>
</html>

在这里插入图片描述

  1. SpringBoot的应用器
package cn.lzj.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebJspApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebJspApplication.class);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42136250/article/details/89641745