anhN
anhN

Reputation: 29

Gradle for this Spring Boot project + jsp

Im using Gradle for this Spring boot project and my task is to create another jsp file , for example : index.jsp and do something that Spring boot can generate that index.jsp

My problem is when i create index.jsp in webapp -> WEB_INF -> index.jsp it only return the message 'index' instead of what is in file index.

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>

Upvotes: 2

Views: 5015

Answers (3)

Victor Georgescu
Victor Georgescu

Reputation: 1

I had the same issue. It's not about the java code. The only solution I found was switching to maven. The only change was build.gradle -> pom.xml, and nothing else in the controller or jsp file, what you have there is correct.

Upvotes: 0

Selvam M
Selvam M

Reputation: 530

You have to use @Controller annotation instead of @RestController in your class PledgeController.

 @Controller
 public class PledgeController {


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

Upvotes: 1

sh.seo
sh.seo

Reputation: 1610

add dependencies for JSP

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

and index.jsp PATH is webapp/WEB_INF/jsp/index.jsp.

if you want example, https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp.

Upvotes: 3

Related Questions