如何使用Spring呈现页面?

时间:2016-09-24 13:59:25

标签: java spring spring-mvc

我一直在尝试遵循this repo的结构,从我所看到的我已经匹配并修改了所需的属性。

当我运行项目时,我收到错误

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

我有很多错误可能存在的文件所以我不会为了简洁而立即将它们全部包含在内,所以如果你认为一个可能是相关的请注释我会添加它但总的来说它们类似于repo链接较早..我的项目结构如下:

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;



}

1 个答案:

答案 0 :(得分:0)

您正在以不起作用的方式混合使用Spring-Boot和旧的经典xml配置。

要获取Boot,其配置和运行方式,请删除web.xml。

但最让我兄弟的是,你的配置延伸SpringBootServletInitializerSpringBootServletInitializer适用于traditional deployment(顺便说一句,这是deprecated in Boot 1.4})