这个Spring Boot项目+ jsp的Gradle

时间:2019-03-06 05:46:22

标签: spring spring-boot jsp gradle

我在这个Spring启动项目中使用Gradle,我的任务是创建另一个jsp文件,例如:index.jsp,并做一些Spring Boot可以生成index.jsp的事情

我的问题是当我在webapp-> WEB_INF-> index.jsp中创建index.jsp时 它只会返回消息“索引”,而不是文件索引中的内容。

Application.java

package edu.msudenver.tsp.website;

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

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

PledgeController.java

package edu.msudenver.tsp.website.controllers;

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

@RestController
public class PledgeController {

    @GetMapping("/hello")
    public String getHelloMessage() {
        return "index";
    }
}

Application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {

    compile('org.springframework.boot:spring-boot-starter-web')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')

}

index.jsp

> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <html> <head>
>     <title>Hello Spring mvc</title> </head> <body>

2 个答案:

答案 0 :(得分:0)

为JSP添加依赖项

compile('javax.servlet:jstl')
compile("org.apache.tomcat.embed:tomcat-embed-jasper")

和index.jsp路径为 webapp / WEB_INF / jsp / index.jsp

如果您想举个例子,https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

答案 1 :(得分:0)

您必须在班级@Controller中使用@RestController注释而不是PledgeController

 @Controller
 public class PledgeController {


        @GetMapping("/hello")
        public String getHelloMessage() {
            return "index";
        }
 }