java Spring 不断返回字符串而不是 jsp/html 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31410528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:34:41  来源:igfitidea点击:

Spring keeps returning string instead of jsp/html content

javaspringjspspring-mvc

提问by JustAName

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?

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

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".

我遇到的主要问题是当我尝试显示 jsp 或 html 页面时,网页的正文显示文本:“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.

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

folder structure:

文件夹结构:

TEST

测试

--Spring Elements (created by STS)

--Spring 元素(由 STS 创建)

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

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

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

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

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

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

home.jsp

主页.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

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

网页.xml

//empty file right now

build.gradle

构建.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

当我访问http://localhost:8080/jsp 时,html 页面的正文为:/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.

所以jsp根本不显示。我已经尝试过 html 和有或没有 method=RequestMethod.GET/POST。没有任何作用。

**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?

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

回答by Marlon Bernardes

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

那是因为您使用的是注释@RestController而不是@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 @RequestMappingassume @ResponseBodysemantics by default. In other words, your method #homeis serializing the String /webapp/WEB-INF/home.jspas JSON, instead of mapping its value to a view.

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