gpt4 book ai didi

java - 如何使用spring boot读取jsp文件?

转载 作者:行者123 更新时间:2023-12-02 00:55:36 25 4
gpt4 key购买 nike

我正在尝试使用 spring-boot 读取我的 jsp 文件,因此我启动了该项目,当我检查本地主机时,它实际上返回实际的字符串“index”,但没有将其读取为 index.jsp (作为 jsp 文件)

我创建了 Controller 类“HomeController”并实现了一个名为 homePage 的方法,该方法将具有 @RequestMapping 然后它将返回“index”

家庭 Controller

@RestController
public class HomeController {

@RequestMapping("/showHome")
public String homePage(){
return "index";
}
}

index.jsp 文件

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
<body>
<div>
<div>
<h1>Spring Boot JSP Example</h1>
<h2>Hello</h2>
</div>
</div>
</body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

应用程序属性

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

项目结构 enter image description here

我的浏览器中有“index”字符串,希望加载 jsp 文件注意:当我用 @Controller 替换 @ResController 时,我收到 Whitelabel 错误页面。

最佳答案

从 application.properties 文件中删除这两行

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

并从资源包中删除模板文件夹。

然后将此类添加到您的 com.example.demo 包中。

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages= {"com.example"})
public class WebConfig implements WebMvcConfigurer {

public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp().prefix("/WEB-INF/views/").suffix(".jsp");
}

public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}


}

在 Controller 中使用:

 @Controller not @RestController 

喜欢:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestController {

@RequestMapping(value = "/showHome", method = RequestMethod.GET)
public String homePage() {
return "index";
}

}

示例博客站点应用程序(git 源代码),带有 Spring Boot and Hibernate您可以从这里获得帮助。

关于java - 如何使用spring boot读取jsp文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57821277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com