2017-02-22 4 views
3

나는, here에서 스프링 부팅 샘플 웹 정전기 프로젝트를 실행 한 치어스프링 부트에서 정적 html을 제공하려면 어떻게해야합니까?

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

이 변경을했다 그리고 같은 static 폴더에서 중복 된 페이지 index2.html를 제공하기 위해이 클래스를 추가 위치 :

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

당신은 정말로 바람둥이 의존성을 필요로하지 않습니다, 봄 초보 웹 & thymeleaf 그것을해야합니다. –

답변

8
+0

아, 그래, 내가 그 localhost : 8080/index2.html 실제로 브라우저에서 볼 수있는 봤고, 거기에 컨트롤러에서 제공하는 방법은 무엇입니까? – osmingo

+0

내 두 번째 ref 링크를 확인하거나 매개 변수를 처리하지 않으려는 경우 : http://stackoverflow.com/a/27279742/3862022 – csenga

+0

spring-boot-starter-thymeleaf를 추가해야하고 템플릿의 파일 만 참조해야합니다 디렉토리, 내가 그것을 가지고 있다고 생각, 감사합니다 – osmingo

0

당신의 spring boot official 예를 볼 수 있습니다

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

심판 :

봄 부팅이 자동으로 다음 디렉토리 중 어떤 내에있는 정적 웹 리소스를 추가합니다 이라는 프로젝트
먼저, extends SpringBootServletInitializer이 필요합니다.
둘째, 봄 부팅에서 시작 Application 클래스

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

override 구성 방법이 필요 /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/ 폴더를 사용할 수 있습니다.

관련 문제