我在这个Spring boot项目中使用Gradle,我的任务是创建另一个jsp文件,例如: index.jsp,并执行Spring boot可以生成该index.jsp的操作
我的问题是,当我在webapp -> WEB_INF -> index.jsp中创建索引时,它只返回消息‘index.jsp’,而不是文件索引中的内容。
Application.java
package edu.msudenver.tsp.website;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}PledgeController.java
package edu.msudenver.tsp.website.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PledgeController {
@GetMapping("/hello")
public String getHelloMessage() {
return "index";
}
}Application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jspbuild.gradle
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}index.jsp
> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <html> <head>
> <title>Hello Spring mvc</title> </head> <body>发布于 2019-03-06 14:30:31
为JSP添加依赖项
compile('javax.servlet:jstl')
compile("org.apache.tomcat.embed:tomcat-embed-jasper")index.jsp路径为webapp/WEB_INF/jsp/index.jsp.
如果您需要示例,可以使用https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp。
发布于 2019-03-06 14:33:03
您必须在类PledgeController中使用@Controller注释而不是@RestController。
@Controller
public class PledgeController {
@GetMapping("/hello")
public String getHelloMessage() {
return "index";
}
}发布于 2022-02-13 09:09:40
我也有同样的问题。这与java代码无关。我找到的唯一解决方案就是切换到maven。唯一的更改是build.gradle -> pom.xml,控制器或jsp文件中没有其他内容,您在那里拥有的内容是正确的。
https://stackoverflow.com/questions/55016359
复制相似问题