猿问

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

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


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


家庭控制器


@RestController

public class HomeController {


    @RequestMapping("/showHome")

    public String homePage(){

     return "index";

    }

}

索引.jsp 文件


<!DOCTYPE html>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<html>

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

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





哈士奇WWW
浏览 144回答 3
3回答

holdtom

从 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 not @RestController 喜欢:import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class TestController {    @RequestMapping(value = "/showHome", method = RequestMethod.GET)    public String homePage() {      return "index";    }}

一只甜甜圈

必须更改我的 pom.xml 文件依赖项,将 tomcat-embed 和 javax.servelet 导入到&nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.tomcat.embed</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>tomcat-embed-jasper</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>8.5.20</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.servlet</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jstl</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>

阿晨1998

如果你想返回jsp文件,你应该使用@Controller。@RestController 返回一个字符串@Controllerpublic class HomeController {    @RequestMapping("/showHome")    public String homePage(){     return "index";    }}对于配置,创建 WebConfig.java 文件并写入以下内容:@Configuration@EnableWebMvc@ComponentScan(basePackages = {        "com.example.demo"})public class WebConfig implements WebMvcConfigurer {    @Bean    public InternalResourceViewResolver getInternalResourceViewResolver(){        InternalResourceViewResolver resolver = new InternalResourceViewResolver();        resolver.setPrefix("/WEB-INF/views/");        resolver.setSuffix(".jsp");        return resolver;    }}
随时随地看视频慕课网APP

相关分类

Java
我要回答