2016-09-24 82 views
0

我一直在試圖遵循this repo的結構,並從我所看到的我已經匹配它並修改了所需的屬性。如何使用Spring呈現頁面?

當我運行的項目,我得到的錯誤

java.lang.RuntimeException: java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!

即使我只有一個串在我的整個項目配套ContextLoader*,更別說我web.xml

我有很多文件的錯誤可能存在,所以我不會立即包括它們所有立即簡潔,所以如果你認爲一個可能是相關的,請評論,我會添加它,但總的來說,他們類似於早前聯回購..我的項目結構如下所示:

enter image description here

web.xml看起來是這樣的:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    <async-supported>true</async-supported> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 --> 
<welcome-file-list> 
    <welcome-file></welcome-file> 
</welcome-file-list> 

我怎樣才能得到它加載index.htmlhome.jsp頁面?

編輯

ApplicationConfig.java

package brass.ducks; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 


@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class ApplicationConfig extends SpringBootServletInitializer{ 

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

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(applicationClass); 
    } 

    private static Class<ApplicationConfig> applicationClass = ApplicationConfig.class; 



} 
+0

作爲一個說明,如果你是新來的春天,我建議從春天啓動,這使得所有這些配置都不必要(從而消除了這種錯誤的可能性)。 – chrylis

+0

你可以刪除偵聽器標籤並嘗試嗎?由於您使用的是DispatcherServlet,因此不是必需的。 –

+0

請注意,如果您是Spring的新手,並想**瞭解它是如何工作的**,請遠離春季啓動的魔法。 – Ralph

回答

0

你混合彈簧引導和歷久彌新的經典xml配置的方式,無法正常工作。

要獲取Boot,其配置和運行方式,請刪除web.xml。

但最深的是什麼兄弟,你的構造延伸SpringBootServletInitializerSpringBootServletInitializertraditional deployment(順便說一句的類deprecated in Boot 1.4))

+0

我已經刪除了'web.xml',並且當我訪問一個我想在其上呈現頁面的URL時,只是以字符串形式返回文件名。 – Nanor

+0

但這也意味着,您的應用程序啓動並能夠處理Web請求。 - 現在谷歌的「春季啓動jsp」和流例如說明https://hellokoding.com/spring-boot-hello-world-example-with-jsp/ – Ralph

相關問題