Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring keeps returning string instead of jsp/html content

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?

like image 889
JustAName Avatar asked Jul 14 '15 15:07

JustAName


1 Answers

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.

like image 151
Marlon Bernardes Avatar answered Oct 31 '22 13:10

Marlon Bernardes