如何将外部页面添加到文件jsp?

时间:2019-02-02 14:10:54

标签: spring-boot spring-mvc

我从start.spring.io下载了我的项目,文件夹结构如图片所示,无法将special-title.html页面添加到home.jsp页面。

我使用以下命令进行集成。

<jsp:include page="/WEB-INF/jsp/special-title.html"></jsp:include>

<jsp:include page="../WEB-INF/jsp/special-title.html"></jsp:include>

<jsp:include page="${pageContext.request.contextPath}/WEB-INF/jsp/special-title.html"></jsp:include>

但是,我无法运行这些命令。我在哪里犯错了?

Picture of project folder structure

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 http://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.2.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>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

server.port=7070
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

DemoController.java

package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class DemoController {

    @GetMapping("/home")
    public String showHomePage() {
        return "home";
    }

}

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<jsp:include page="/WEB-INF/jsp/special-title.html"></jsp:include>

<h1>HELLO WORLD</h1>

</body>
</html>

special-title.html

<h2>SPECIAL TITLE</h2>

测试网址

http://localhost:7070/home

1 个答案:

答案 0 :(得分:0)

首先将extension NSAttributedString { func sizeFittingWidth(_ w: CGFloat) -> CGSize { let textStorage = NSTextStorage(attributedString: self) let size = CGSize(width: w, height: CGFloat.greatestFiniteMagnitude) let boundingRect = CGRect(origin: .zero, size: size) let textContainer = NSTextContainer(size: size) textContainer.lineFragmentPadding = 0 let layoutManager = NSLayoutManager() layoutManager.addTextContainer(textContainer) textStorage.addLayoutManager(layoutManager) layoutManager.glyphRange(forBoundingRect: boundingRect, in: textContainer) let rect = layoutManager.usedRect(for: textContainer) return rect.integral.size } } special-title.html移到WEB-INF下的templates文件夹中。

然后在jsp文件中尝试添加行resources而不是<%@ include file="resources/templates/special-title.html"%>

要将html作为jsp运行,您可以做一件事。像这样在<jsp:include page="/WEB-INF/jsp/special-title.html"></jsp:include>内写一个web.xml

WEB-INF