美文网首页
spring boot 配置视图jsp

spring boot 配置视图jsp

作者: lazyTai | 来源:发表于2018-10-23 14:38 被阅读13次
image.png

目录结构


image.png

prom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--    <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
    -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!--添加对jsp的支持-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--此处的<scope></scope>一定不要加上作用于为provided,可以为compile或缺省-->
        </dependency>

        <!--添加对tomcat的支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置 application.yml
D:\www\demo\src\main\resources\application.yml

spring:
  mvc:
    view:
      prefix: /views/
      suffix: .jsp
server:
  port: 8081

controller
D:\www\demo\src\main\java\com\example\demo\web\index.java


@controller
@RequestMapping("/index")

public class index {
    @RequestMapping("/hello")
    public String hello() {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("hello", "fuck");
        return "index";
    }
}

controller reutrn string就是到jsp
如果放回map呢?就是json?
验证ing

image.png

你是对的

传json出来

controller 写法

package com.example.demo.web.json;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class JsonIndex {
    @RequestMapping("/index")
    public Map index() {
        Map a = new HashMap();
        a.put("name", "liumingtai");
        return a;
    }
}

给jsp参数

controller写法

package com.example.demo.web.view;


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

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class index {
    @RequestMapping("/hello")
    public String hello() {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("hello", "fuck");
        return "index";
    }

    @RequestMapping("/jspgetvalue")
    public String ineedjson(Map<String, Object> map) {
        map.put("content", "将属性值传递给jsp页面");
        return "index1";
    }

    @RequestMapping("/json")
    public Map<String, Object> putjson(Map<String, Object> map) {
        map.put("content", "将属性值传递给jsp页面");
        return map;
    }
}


jsp写法

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
content:${content}
</body>
</html>

相关文章

网友评论

      本文标题:spring boot 配置视图jsp

      本文链接:https://www.haomeiwen.com/subject/uxfazftx.html