2017-02-17 96 views
-1

我有非常簡單的春天引導創建的Hello World(只是入門網站,沒有thymeleaf等)。 我想要處理/你好URL,它應該從/static/view.html春季啓動網址.html結尾

我有我的控制器/static/view.html和簡單的方法產生的看法:

@RequestMapping("/hello") 
public String hello2() { 
    return "hello.html"; 
} 

的問題是,它會導致錯誤:

Circular view path [hello.html]: would dispatch back to the current handler URL [/hello.html] again

我想通了,如果我訪問/個招呼,或者/hello.html沒關係,春天對待他們一樣。

如何返回簡單,靜態的html與url路徑相同的名稱以及spring中的哪個對象mvc/boot導致像/example.html這樣的映射url只是/ example?

+0

您的文件被命名爲view.html,但是您返回的是hello.html。爲什麼?而且,view.html不是一個視圖。這是一個公開可用的資源。你應該轉發它。 –

+0

讓我們說,在顯示hello.html之前,我想在我的控制器中做一些邏輯(我的例子中的hello.html可以是簡單的確認頁面)。所以我想繼續做這個簡單的事情,爲什麼春季啓動地圖* .html只是*仍然有效 – swch

+0

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle /#mvc-multiple-representations –

回答

0

刪除控制器中的方法。對於靜態內容,不需要@RequestMapping

+0

我想你的意思是*不需要*。 –

+0

絕對......我會解決這個問題的 – ndrone

1

你必須執行下面的步驟:

  1. 介紹MVC配置:
@Configuration 
@EnableWebMvc 
public class MvcConfiguration extends WebMvcConfigurerAdapter{ 
    @Bean 
    public ViewResolver getViewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/WEB-INF/"); 
     return resolver; 
    } 

    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 
} 
  • 把你hello.html的/src/main/webapp/WEB-INF文件夾

  • 請確保您有編譯依賴關係是這樣的:

    compile("org.springframework.boot:spring-boot-starter-web") 
    compile("org.apache.tomcat.embed:tomcat-embed-jasper") 
    

    我的gradle發佈代碼,如果你有行家使用類似XML。

  • 注意第3步是因爲如果你使用Spring啓動它取決於你在classpath中有什麼應用自動配置最重要的一步。例如,如果你添加thymeleaf依賴

    compile("org.springframework.boot:spring-boot-starter-thymeleaf") 
    

    ,將最likelly打破代碼,因爲thymeleaf將推出其自己的自動配置視圖解析器。