第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

哈士奇WWW 2023-07-13 15:37:20
我正在嘗試使用 spring-boot 讀取我的 jsp 文件,因此我啟動了項目,當我檢查本地主機時,它實際上返回實際的字符串“index”,但沒有將其讀取為 index.jsp (作為 jsp 文件)我創(chuàng)建了控制器類“HomeController”并實現(xiàn)了一個名為 homePage 的方法,該方法將具有 @RequestMapping 然后它將返回“index”家庭控制器@RestControllerpublic 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 錯誤頁面。
查看完整描述

3 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

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


@Controller

public class TestController {


? ? @RequestMapping(value = "/showHome", method = RequestMethod.GET)

? ? public String homePage() {

? ? ? return "index";

? ? }


}

查看完整回答
反對 回復 2023-07-13
?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

必須更改我的 pom.xml 文件依賴項,將 tomcat-embed 和 javax.servelet 導入到


  <dependency>

            <groupId>org.apache.tomcat.embed</groupId>

            <artifactId>tomcat-embed-jasper</artifactId>

            <version>8.5.20</version>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>jstl</artifactId>

        </dependency>


查看完整回答
反對 回復 2023-07-13
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

如果你想返回jsp文件,你應該使用@Controller。@RestController 返回一個字符串


@Controller

public class HomeController {


? ? @RequestMapping("/showHome")

? ? public String homePage(){

? ? ?return "index";

? ? }

}

對于配置,創(chuàng)建 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;

? ? }

}

查看完整回答
反對 回復 2023-07-13
  • 3 回答
  • 0 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號