15

I am trying to understand the whole Spring Framework. To my knowledge, the relatively new techniques of using gradle makes a lot of the tutorials and posts online outdated?

The main issue that I am running in to is when I try to display a jsp or html page, the webpage's body shows text: "filename.jsp".

The project was created by New-> Other-> Spring Starter Project using Gradle and STS 3.7. The goal is to create a web application using MVC pattern.

folder structure:

TEST

--Spring Elements (created by STS)

--src/main/java
++--TEST
++++--TestApplication.java (created by STS)
++--TEST.Controller
++++--JSPController.java

--sec/main/resources
++--application.properties (created by STS , EMPTY file)

--src/main/webapp ( ** I created this directory, but is this required?)
++--WEB-INF
++++--home.jsp
++++--home.html
++++--web.xml (** Additional question below)

TestApplication.java

package TEST;

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

@SpringBootApplication 
public class TestApplication {

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

home.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test</title>
</head>
<body>

    <h1>Test 1</h1>

    <form>
        First name:<br> <input type="text" name="firstname"> <br>
        Last name:<br> <input type="text" name="lastname">
    </form>

</body>
</html>

JSPController.java

package TEST.controller;

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

@RestController
public class JSPController {

    @RequestMapping(value = "/jsp", method = RequestMethod.GET)
    public String home(){
        return "/webapp/WEB-INF/home.jsp";
    }   

}

web.xml

//empty file right now

build.gradle

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

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot' 
apply plugin: 'io.spring.dependency-management' 

jar {
    baseName = 'TEST'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test") 
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

When I go to http://localhost:8080/jsp, the html page has the body: /webapp/WEB-INF/home.jsp

So the jsp is not displaying at all. I have tried html and with or without method=RequestMethod.GET/POST. Nothing works.

**Additional question: A lot of online posts/tutorial goes in to .xml files, for example, web.xml. To my understanding, these are no longer required or needed because spring + gradle generates a .xml automatically from the @notations?

2 Answers 2

46

That's because you are using the annotation @RestController instead of @Controller.

Change your class annotation from

@RestController
public class JSPController {
  ...
}

to:

@Controller
public class JSPController {
  ...
}

When you annotate a class with RestController, all methods annotated with @RequestMapping assume @ResponseBody semantics by default. In other words, your method #home is serializing the String /webapp/WEB-INF/home.jsp as JSON, instead of mapping its value to a view.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the fast respond, but when I change it to @Controller, I am receiving an error on the browser: Whitelabel Error Page. There was an unexpected error (type=Not Found, status=404).
Now you have another problem! :) Since you are using Spring Boot, your views should be placed under the directory src/main/resources/templates , so you don't need the directory src/main/webapp. I also suggest that you use dont use JSP as your view technology (specially when paired with spring-boot). Please read the reference guide here: docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/…
Thank. Im reading the ref guide now. They mentioned creating a pom.xml. Which brings me back to the additional question above. Do I need to make pom.xml or any .xml eventhough I am building with gradle?
I see that a lot of the reference guide has maven code in it, mvn, which makes it really hard to distinguish what is required and not required for my project with gradle. Do you have any suggestions/recommendations? Still confused about .xml with gradle.
@MarlonBernardes the link to the reference guide returns a 404, do you have an updated one? I am using JSP in Spring Boot and would like to understand why it should not be used.
|
0

After changing @Controller to @RestController, you have to add this dependancy :

<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

to your pom.xml file, as explained in this link : https://www.yawintutor.com/warn-3200-path-with-web-inf-or-meta-inf/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.