Spring boot MVC:白标错误页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36596069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot MVC : Whitelabel Error Page
提问by Ravichandra
Getting the following Whitelabel Error Page while I am running Spring Boot MVC app.
在我运行 Spring Boot MVC 应用程序时获取以下白标错误页面。
Whitelabel Error Page
白标错误页面
This application has no explicit mapping for /error, so you are seeing this as a fallback.
此应用程序没有明确的 /error 映射,因此您将其视为后备。
Wed Apr 13 15:45:59 IST 2016
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [home]: would dispatch back to the current handler URL [/rewards/web/home] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
Wed Apr 13 15:45:59 IST 2016 出现意外错误(类型=内部服务器错误,状态=500)。圆形视图路径 [home]:将再次分派回当前处理程序 URL [/rewards/web/home]。检查您的 ViewResolver 设置!(提示:由于默认视图名称生成,这可能是未指定视图的结果。)
application.properties
应用程序属性
server.contextPath=/rewards/web
rewardsweb-servlet.xml:
奖励web-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
网页.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 files
弹簧引导文件
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;
}
}
Controller:
控制器:
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
JSP
home.jsp is in this path : /src/main/webapp/WEB-INF/views/jsp/home.jsp
when i hit : http://localhost:9090/rewards/web/homei am getting Whitelabel Error
当我点击:http://localhost:9090/rewards /web/home我收到白标错误
And also i have tried following solution, added the following code in controller class. but no help.
而且我也尝试了以下解决方案,在控制器类中添加了以下代码。但没有帮助。
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";
}
}
Can you please help me out.
Thanks.
你能帮我一下吗。
谢谢。
回答by Sanjay Rawat
There is NO web.xmlin Spring Boot.
Spring Boot 中没有web.xml。
All of your above xml configuration is ignored by Spring Boot. Spring Boot uses Java Config (although you can use configuration from xml, but you shouldn't use xml for new projects).
Spring Boot 会忽略上述所有 xml 配置。Spring Boot 使用 Java Config(虽然您可以使用来自 xml 的配置,但您不应该将 xml 用于新项目)。
This is how you define InternalResourceViewResolver
in Spring Boot:
这是您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;
}
}
Here is Spring Boot JSP Demo Appwhich will help you modify your Project to actually make it Spring Boot project.
这是Spring Boot JSP 演示应用程序,它将帮助您修改您的项目以实际使其成为 Spring Boot 项目。
Also I suggest you to go through this Spring Boot Guidefor getting started with Spring Boot WebApp and the Spring Boot Reference Guide.
另外,我建议您阅读Spring Boot 指南以开始使用 Spring Boot WebApp 和Spring Boot 参考指南。
回答by Mike3355
It looks like you need a Dispatcher Servlet. Maybe something like this:
看起来您需要一个 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>
Application Config
应用配置
<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>
Hope this helps.
希望这可以帮助。
回答by u11668738
I've had the same problem, but it turned out to be not Spring Boot's problem. If you have frontend in Angular 2+, try using
我遇到了同样的问题,但结果证明不是 Spring Boot 的问题。如果您在 Angular 2+ 中有前端,请尝试使用
{
provide: LocationStrategy,
useClass: HashLocationStrategy
}
in your app.module.ts and it will help.
在您的 app.module.ts 中,它会有所帮助。
回答by Arun
You have to follow below thing steps before rendering a JSP page in spring boot application.
在 Spring Boot 应用程序中渲染 JSP 页面之前,您必须遵循以下步骤。
1.application.properties
1.application.properties
spring.mvc.view.prefix = /* spring.mvc.view.suffix = .jsp
spring.mvc.view.prefix = /* spring.mvc.view.suffix = .jsp
2.POM.XML
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.Place JSPs in proper path. I prefer below folder structure -> src\main\webapp\view\
3.将JSP放置在适当的路径中。我更喜欢下面的文件夹结构 -> src\main\webapp\view\
If you follow above points,JSP will happily integrated with springboot
如果您遵循以上几点,JSP 将很乐意与 springboot 集成