如何使用Spring呈现页面?
问题描述:
我一直在试图遵循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
。
我有很多文件的错误可能存在,所以我不会立即包括它们所有立即简洁,所以如果你认为一个可能是相关的,请评论,我会添加它,但总的来说,他们类似于早前联回购..我的项目结构如下所示:
我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.html
或home.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;
}
答
你混合弹簧引导和历久弥新的经典xml配置的方式,无法正常工作。
要获取Boot,其配置和运行方式,请删除web.xml。
但最深的是什么兄弟,你的构造延伸SpringBootServletInitializer
(SpringBootServletInitializer
是traditional deployment(顺便说一句的类deprecated in Boot 1.4))
作为一个说明,如果你是新来的春天,我建议从春天启动,这使得所有这些配置都不必要(从而消除了这种错误的可能性)。 – chrylis
你可以删除侦听器标签并尝试吗?由于您使用的是DispatcherServlet,因此不是必需的。 –
请注意,如果您是Spring的新手,并想**了解它是如何工作的**,请远离春季启动的魔法。 – Ralph