2015-05-15 44 views
0

我已經運行了一個簡單的程序,它允許我將HTTP請求映射到函數 - 這很好,所以我決定,而不是簡單地返回一個隨機字符串,我可以這很容易以字符串格式返回HTML頁面,並且能夠動態地提供靜態HTML頁面。如何從已編譯的Maven/Spring項目加載文件資源

問題是我無法加載網頁資源。

我的項目有這樣的結構:

projectFolder 
    pom.xml 
    -> src 
     -> main 
       -> webapp 
       home.html 
       -> java 
        -> redway 
         MLoader.java 
         Application.java 

正如我說我的程序工作正常,如果我只是想返回一個字符串,它試圖加載,我似乎無法Home.html文件中管理,特別是我的問題是如何獲得正確的路徑。

這是我想的最後一件事:

package redway; 

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

import java.net.URL; 
import java.io.*; 

@RestController 
public class MLoader { 

    public static final String HOME_HTML = "src/main/webapp/home.html"; 

    @RequestMapping("/home") 
    public String home() { 

     String page = ""; 
     String line; 

     URL url = this.getClass().getClassLoader().getResource(HOME_HTML); 

     try (BufferedReader reader = new BufferedReader(new InputStreamReader (new FileInputStream(url.toString()),"UTF-8"))){ 
      while((line = reader.readLine()) != null) { 
       page += line; 
      } 
     } catch (IOException e) { 
      return "unable to load file..."; 
     } 

     return page; 
    } 

} 

這裏是我的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> 

    <groupId>org.springframework</groupId> 
    <artifactId>HTMLLoader</artifactId> 
    <version>1</version> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.2.3.RELEASE</version> 
    </parent> 

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

    <properties> 
     <java.version>1.7</java.version> 
    </properties> 


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

    <repositories> 
     <repository> 
      <id>spring-releases</id> 
      <url>https://repo.spring.io/libs-release</url> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>spring-releases</id> 
      <url>https://repo.spring.io/libs-release</url> 
     </pluginRepository> 
    </pluginRepositories> 
</project> 

和公正的良好措施下面是一個包含我的主要方法我Apllication.java文件:

package redway; 

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

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

我一直在擺弄它幾個小時,這讓我很生氣,因爲我確定t這裏一定是一個非常簡單的解釋。我現在只是在玩它,所以我確信這不是構建動態Web應用程序的最佳方法,但我仍然想知道我做錯了什麼。

回答

0

行家方法是將HTML文件(或任何其他資源文件)在src /主/資源/

後試圖

this.getClass().getResource("/home.html") 
+0

它編譯但將在運行時出現此錯誤:週五5 15 21:35:41 BST 2015 發生意外錯誤(type = Internal Server Error,status = 500)。 沒有留言可用 –

+0

我是這麼想的,所以我查看了實際的jar文件來檢查它的位置,我在jar找不到資源。這聽起來很愚蠢,因爲我是Maven的新手,但是我需要將它包含在pom.xml文件中嗎?我想也許它從來沒有編譯進Jar? –

+0

檢查你的jar目標文件夾,如jar tf target/your-name.jar –