Spring保持返回字符串而不是jsp/html内容

Jus*_*ame 9 java spring jsp spring-mvc

我试图了解整个Spring框架.据我所知,使用gradle的相对较新的技术使很多教程和帖子在线过时了?

我遇到的主要问题是当我尝试显示jsp或html页面时,网页的正文显示文本:"filename.jsp".

该项目由New-> Other-> Spring Starter Project使用Gradle和STS 3.7创建.目标是使用MVC模式创建Web应用程序.

文件夹结构:

测试

- 春天元素(由STS创建)

--src/main/java
++ - TEST
++++ - TestApplication.java(由STS创建)
++ - TEST.Controller
++++ - JSPController.java

--sec/main/resources
++ - application.properties(由STS创建,EMPTY文件)

--src/main/webapp(**我创建了这个目录,但是这是必需的吗?)
++ - WEB-INF
++++ - home.jsp
++++ - home.html
++++ --web.xml(**下面的附加问题)

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);          
     }
}
Run Code Online (Sandbox Code Playgroud)

针对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>
Run Code Online (Sandbox Code Playgroud)

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";
    }   

}
Run Code Online (Sandbox Code Playgroud)

web.xml中

//empty file right now
Run Code Online (Sandbox Code Playgroud)

的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'
}
Run Code Online (Sandbox Code Playgroud)

当我转到http:// localhost:8080/jsp时,html页面的主体是:/webapp/WEB-INF/home.jsp

所以jsp根本没有显示.我尝试过html,有或没有方法= RequestMethod.GET/POST.什么都行不通.

**附加问题:许多在线帖子/教程都会使用.xml文件,例如web.xml.根据我的理解,不再需要或不需要这些,因为spring + gradle会自动从@notations生成.xml?

Mar*_*des 33

那是因为你使用的是注释@RestController而不是@Controller.

更改您的班级注释

@RestController
public class JSPController {
  ...
}
Run Code Online (Sandbox Code Playgroud)

至:

@Controller
public class JSPController {
  ...
}
Run Code Online (Sandbox Code Playgroud)

当您使用注释类时RestController,默认情况下使用@RequestMapping假定@ResponseBody语义注释的所有方法.换句话说,您的方法#home是将String序列化为/webapp/WEB-INF/home.jspJSON,而不是将其值映射到视图.

  • 现在您还有另一个问题!:)由于您使用的是Spring Boot,因此您的视图应放在目录src / main / resources / templates下,因此您不需要目录src / main / webapp。我还建议您不要将JSP用作视图技术(特别是在与spring-boot配对时)。请在此处阅读参考指南:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-template-engines (2认同)