Jar文件不在目标文件夹中工作,但在项目文件夹中工作

时间:2020-08-21 07:32:17

标签: java spring-boot maven

当我在STS中运行项目时,它工作正常。 webapp文件夹中的所有页面都正在浏览器中加载。但是,当我构建项目并运行JAR文件(文件夹: D:\ SpringBoot Projects \ DemoExample \ target)并转到页面URL时,出现白色标签错误。 在另一个项目中,我使用了spring security-在该项目中,登录页面可以很好地加载,但是webapp文件夹中的所有JSP页面都不能像上面的示例那样加载。 在尝试解决此问题时,我将JAR文件从目标文件夹移动到项目文件夹(D:\ SpringBoot Projects \ DemoExample),然后再次运行JAR文件,现在JSP页面可以正常加载了。 JSP页面仅在该文件夹中加载,如果我将文件移至另一个文件夹,则这些页面不会加载。 我想确保可以从任何文件夹运行JAR文件。

POM文件:

    <?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.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.project</groupId>
    <artifactId>DemoExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>DemoExample</name>
    <description>Project</description>

    <properties>
        <java.version>11</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
            <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.37</version>
</dependency>
    </dependencies>
    
    

    <build>
    <outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
    <testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
    
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

主要类别:

    package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoExampleApplication.class, args);      
    }

}

控制器:

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;

@org.springframework.stereotype.Controller
public class Controller {
    
    @RequestMapping("/")
    public String home() {
        return "home.jsp";
    }
}

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>
    Hello
</body>
</html>

Project Structure

Running Jar file in target folder

Result after running Jar file in target folder

Running Jar in project folder

Result after running Jar file in project folder

0 个答案:

没有答案