Spring Boot MVC:Whitelabel错误页面

Rav*_*dra 1 spring spring-mvc spring-boot

当我运行Spring Boot MVC应用程序时,获取以下Whitelabel错误页面。

白标错误页面

此应用程序没有针对/ error的显式映射,因此您将其视为后备。

IST 2016年4月13日星期三15:45:59发生意外错误(类型=内部服务器错误,状态= 500)。循环视图路径[home]:将再次分派回当前处理程序URL [/ rewards / web / home]。检查您的ViewResolver设置!(提示:由于默认视图名称的生成,这可能是未指定视图的结果。)

application.properties

server.contextPath=/rewards/web
Run Code Online (Sandbox Code Playgroud)

rewardsweb-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.rewards.web" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />

</beans>
Run Code Online (Sandbox Code Playgroud)

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_2_5.xsd"
    version="2.5">

    <display-name>rewards-web</display-name>
    <welcome-file-list>  
   <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
    <servlet>
        <servlet-name>rewardsweb</servlet-name>
        <servlet-class>
                    org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>
Run Code Online (Sandbox Code Playgroud)

Spring Boot文件

package com.rewards.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;


@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

}

package com.rewards.web;

import io.undertow.Undertow.Builder;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
public class ApplicationConfiguration {
    @Bean
    public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
        UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
        factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {

            public void customize(Builder builder) {
                builder.addHttpListener(9090, "0.0.0.0");
            }

        });
        return factory;
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

package com.rewards.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Controller
public class HomeController {

    @RequestMapping("/home")
    public String getHome(){
        System.out.println("-------this is home----------");
        return "home";
    }
}
Run Code Online (Sandbox Code Playgroud)

JSP

home.jsp is in this path : /src/main/webapp/WEB-INF/views/jsp/home.jsp
Run Code Online (Sandbox Code Playgroud)

当我点击时:http:// localhost:9090 / rewards / web / home我收到Whitelabel错误

我也尝试了以下解决方案,在控制器类中添加了以下代码。但没有帮助。

package com.rewards.web.controller;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController implements ErrorController{

    private static final String PATH = "/error";

    @RequestMapping(value = PATH)
    public String error() {
        return "Error handling";
    }

    public String getErrorPath() {
        return PATH;
    }

    @RequestMapping("/home")
    public String getHome(){
        System.out.println("-------this is home----------");
        return "home";
    }
}
Run Code Online (Sandbox Code Playgroud)

你能帮我一下吗。
谢谢。

San*_*wat 5

Spring Boot中没有web.xml

您的所有上述xml配置都会被Spring Boot忽略。Spring Boot使用Java Config(尽管您可以使用xml中的配置,但不应将xml用于新项目)。

这是您InternalResourceViewResolver在Spring Boot中定义的方式:

@Configuration
@EnableWebMvc
public class ApplicationWebMvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是Spring Boot JSP演示应用程序,它将帮助您修改项目以使其实际成为Spring Boot项目。

另外,我建议您阅读本《Spring Boot指南》以开始使用Spring Boot WebApp和《Spring Boot参考指南》