2017-02-22 60 views
3

私がポンポンスプリングブートから静的HTMLをどのように提供できますか?

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

にこの変更を行い、同じstaticフォルダから重複ページindex2.htmlを提供するために、このクラスを追加し、hereから春ブート・サンプル・ウェブ・静的プロジェクトを実行しました場所:

import org.springframework.http.MediaType; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class Rester { 

    @RequestMapping(value = "/rand", produces = MediaType.APPLICATION_JSON_VALUE) 
    @ResponseBody 
    private RandomObj jsonEndpoint() { 
     return new RandomObj(); 
    } 

    @RequestMapping(value = "/tw") 
    public String somePg() { 
     return "index2"; 
    } 
} 

JSON URLが正常に動作しますが、私はローカルホストにアクセスしようとすると:8080/TW私は空白のページを取得し、コンソールでこのエラー:

2017-02-22 15:37:22.076 ERROR 21494 --- [nio-8080-exec-9] o.s.boot.web.support.ErrorPageFilter  : Cannot forward to error page for request [/tw] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false 

私は何か間違っていますか?助けていただければ幸いです。

+0

あなたは本当に風変わりな依存関係を必要としません、春のスターターWeb&thymeleafはそれを行う必要があります。 –

答えて

8

スタティックファイルは、コントローラからではなくリソースから提供する必要があります。

春ブーツが自動的 次のいずれかのディレクトリ内に配置された静的なWebリソースを追加します。

/META-INF/resources/ 
/resources/ 
/static/ 
/public/ 

引用文献:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot https://spring.io/guides/gs/serving-web-content/

+0

ああ、okだから私はlocalhost:8080/index2.htmlがブラウザから実際に見ることができ、それをコントローラから提供する方法がないのを見た? – osmingo

+0

私の2回目のリファレンスリンクを確認するか、パラメータを処理したくない場合: http://stackoverflow.com/a/27279742/3862022 – csenga

+0

spring-boot-starter-thymeleafを追加してテンプレート内のファイルのみを参照する必要がありますディレクトリ、私はそれを持っていると思う、ありがとう – osmingo

0

をあなたがspring boot official例を見ることができますという名前のプロジェクトです。
まず、extends SpringBootServletInitializerが必要です。
第二に、あなたは春のブートで起動Applicationクラス

@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
    return builder.sources(Application.class); 
} 
0

override configureメソッドを必要とし、/META-INF/resources//resources/static/public/ディレクトリは、静的コンテンツを提供するために利用されています。

resources/ディレクトリの下にstatic/またはpublic/ディレクトリを作成し、静的コンテンツをそこに置くことができます。そして、彼らはhttp://localhost:8080/your-file.extによってアクセス可能になります。 (server.portが8080と仮定して)

application.propertiesspring.resources.static-locationsを使用して、これらのディレクトリをカスタマイズできます。例えば

spring.resources.static-locations=classpath:/custom/ 

今、あなたは、静的なファイルを提供するためにresources/custom/フォルダを使用することができます。

関連する問題