Spring boot MVC:Whitelabel Error Page

时间:2016-04-13 10:47:49

标签: spring spring-mvc spring-boot

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

Whitelabel错误页面

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

2016年4月13日星期三15:45:59 IST 2016 出现意外错误(type = Internal Server Error,status = 500)。 圆形视图路径[home]:将再次调度回当前处理程序URL [/ rewards / web / home]。检查您的ViewResolver设置! (提示:由于默认的视图名称生成,这可能是未指定视图的结果。)

application.properties

server.contextPath=/rewards/web

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>

的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>

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;
    }
}

控制器:

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";
    }
}

JSP

home.jsp is in this path : /src/main/webapp/WEB-INF/views/jsp/home.jsp

当我点击时: 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";
    }
}
你能帮我解决一下吗?
感谢。

4 个答案:

答案 0 :(得分:5)

Spring Boot中没有 web.xml

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

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

@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;
    }
}

这是Spring Boot JSP Demo App,它将帮助您修改项目以实际使其成为Spring Boot项目。

另外,我建议您通过这个Spring Boot Guide来开始使用Spring Boot WebApp和Spring Boot Reference Guide

答案 1 :(得分:0)

看起来你需要一个Dispatcher Servlet。也许是这样的:

Dispatcher Config:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

应用程序配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/application-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>

希望这有帮助。

答案 2 :(得分:0)

我遇到了同样的问题,但事实证明这不是Spring Boot的问题。 如果您在Angular 2+中具有前端,请尝试使用

import cv2

image = cv2.imread('1.png')
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel, iterations=3)

cv2.imshow('opening', opening)
cv2.waitKey()

在您的app.module.ts中,它将有所帮助。

答案 3 :(得分:0)

在Spring Boot应用程序中呈现JSP页面之前,您必须遵循以下步骤。

1.application.properties

spring.mvc.view.prefix = / * spring.mvc.view.suffix = .jsp

2.POM.XML

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

3。将JSP放置在正确的路径中。我更喜欢下面的文件夹结构-> src \ main \ webapp \ view \

如果您遵循以上几点,JSP将很高兴与springboot集成

相关问题