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

时间:2015-07-14 15:11:23

标签: java spring jsp spring-mvc

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

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

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

文件夹结构:


TEST

- 弹簧元素(由STS创建)

- SRC /主/ JAVA
++ - TEST
++++ - TestApplication.java(由STS创建)
++ - TEST.Controller
++++ - JSPController.java

- 秒/主/资源
++ - 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);          
     }
}

针对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'
}

当我去http://localhost:8080/jsp时, html页面有正文:/webapp/WEB-INF/home.jsp

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

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

2 个答案:

答案 0 :(得分:27)

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

更改班级注释
@RestController
public class JSPController {
  ...
}

为:

@Controller
public class JSPController {
  ...
}

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

答案 1 :(得分:0)

将@Controller 改为@RestController 后,你必须添加这个依赖:

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

到您的 pom.xml 文件,如此链接中所述:https://www.yawintutor.com/warn-3200-path-with-web-inf-or-meta-inf/

相关问题