Spring Boot JSP 404

我正在尝试在Spring Boot服务中添加一个jsp页面。 我的问题是,每当我尝试去那个页面时,我都会这样:

白标错误页面

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

Tue Apr 21 23:16:00 EEST 2015出现意外错误(type = Not Found,status = 404)。 没有消息可用

我在我的application.properties中添加了前缀和sufix:

spring.view.prefix: /WEB-INF/jsp/ spring.view.suffix: .jsp 

这是我的控制器类:

 @Controller public class MarkerController { @RequestMapping(value="/map") public String trafficSpy() { return "index"; } } 

我的应用类:

 @SpringBootApplication public class Application extends SpringBootServletInitializer { private static Logger logger = Logger.getLogger(Application.class.getName()); public static void main(String[] args) { logger.info("SPRING VERSION: " + SpringVersion.getVersion()); SpringApplication.run(Application.class, args); } } 

而index.jsp:

     

Hello, World!!!

JSTL URL: ${url}

这是src文件结构:

 ├── src │  ├── main │  │  ├── java │  │  │  └── com │  │  │  └── example │  │  │  └── internetprogramming │  │  │  └── myserver │  │  │  └── server │  │  │  ├── Application.java │  │  │  ├── config │  │  │  │  └── DatabaseConfig.java │  │  │  ├── controller │  │  │  │  └── MarkerController.java │  │  │  ├── dao │  │  │  │  ├── MarkerDaoImplementation.java │  │  │  │  └── MarkerDaoInterface.java │  │  │  ├── Marker.java │  │  │  └── service │  │  │  ├── MarkerServiceImplementation.java │  │  │  └── MarkerServiceInterface.java │  │  ├── resources │  │  │  └── application.properties │  │  └── webapp │  │  └── WEB-INF │  │  └── jsp │  │  └── index.jsp 

确保在依赖项列表中有jasper和jstl:

   org.apache.tomcat.embed tomcat-embed-jasper provided   javax.servlet jstl  

这是一个有效的入门项目 – https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

在较新版本的Spring中,需要将以下内容放在application.properties文件中:

spring.mvc.view.prefix = / WEB-INF / JSP /
spring.mvc.view.suffix = .JSP

此外,JSP文件需要放在src / main / resources / META-INF / resources / WEB-INF / jsp下

我的问题是我使用@RestController而不是@Controller作为我的控制器类中的注释。 希望这可以帮助别人。

我的问题是Spring vesrion :我发现自从1.4.3及更高版本停止支持嵌入式JSP。 所以我将版本更改为1.4.1 ,它对我有用

另一件事起飞:

  org.springframework.boot spring-boot-starter-thymeleaf  

它无法使用它。

除了上面的答案,应用程序需要部署为战争而不是jar

 com.igt customer 0.0.1-SNAPSHOT war 

跑步

 java -jar customer-0.0.1-SNAPSHOT.war 

此外,如果您打算将应用程序作为战争或可执行应用程序启动,则需要在SpringBootServletInitializer回调和main方法可用的方法中共享构建器的自定义,类似于

 package com.igt.customer; import java.util.Arrays; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @SpringBootApplication public class CustomerApplication extends org.springframework.boot.web.support.SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(CustomerApplication.class); } public static void main(String[] args) { SpringApplication.run(CustomerApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }; } } 

请参见

这个

Spring MVC没有提供开箱即用的默认(后备)错误页面。 设置默认错误页面的最常见方法一直是SimpleMappingExceptionResolver (实际上是Spring V1)。 但是,Spring Boot还提供了回退error handling页面。

在启动时,Spring Boot会尝试查找/error的映射。 按照惯例,以/error结尾的URL映射到同名的逻辑视图: error 。 通常,此视图依次映射到error.html Thymeleaf模板。 (如果使用JSP,它将根据您的InternalResourceViewResolver的设置映射到error.jsp )。


Spring Boot会自动使用和配置Thymeleaf作为视图渲染引擎,只要它在类路径上即可。

Thymeleaf与Maven:

使用以下命令确保安装了Maven 3:mvn –version。 导航到要在其中创建项目的目录并执行Maven archtetype:

 mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=pl.codeleak.demos.sbt -DartifactId=spring-boot-thymeleaf -interactiveMode=false 

上面的命令将创建一个新目录spring-boot-thymeleaf。 现在您可以将其导入IDE。 下一步是配置应用程序。 打开pom.xml并添加父项目:

  org.springframework.boot spring-boot-starter-parent 1.1.8.RELEASE  

如果未指定,则父项目中的值将是此项目的默认值。 下一步是添加Web依赖项。 为了做到这一点,我首先删除了所有以前的依赖项(实际上是junit 3.8.1)并添加了以下依赖项:

   org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-thymeleaf   

现在,等待一秒钟直到Maven下载依赖项并运行mvn dependency:tree以查看包含哪些依赖项。 接下来是包assembly置。 让我们添加Spring Boot Maven插件:

    org.springframework.boot spring-boot-maven-plugin    

Thymeleaf with Gradle:

将Thymeleaf放在classpath上使用

 compile("org.springframework.boot:spring-boot-starter-thymeleaf") 

在gradle构建文件中(使用相关的maven依赖是很简单的)。

在您的情况下,为了显示index.jsp视图(根据您使用的控制器),您需要将它放在src/main/resources/templates/


如果找不到/ error到View的映射,Spring Boot会定义自己的后备错误页面 – 所谓的Whitelabel Error Page (只有HTTP状态信息的最小页面和任何错误详细信息,例如消息来自未被捕获的例外)。

你可以使用jme的百里香叶,但你必须写:

 spring.thymeleaf.excluded-view-names=#jsp file without extension 

在application.properties文件中

在pom.xml中有这个

   org.apache.tomcat.embed tomcat-embed-jasper provided    javax.servlet jstl  

这可能还不够。

你一定不要错过这个。

 war 

否则,当您构建包时,您将获得一个jar文件,并且没有JSP或嵌入式tomcat。

请参阅可运行的示例及其说明https://www.surasint.com/spring-boot-jsp/

尝试在错误文件夹下添加错误jsp文件。

 application.properties spring.mvc.view.prefix=/views/jsp/ spring.mvc.view.suffix=.jsp jsp files : /views/jsp/error/401.jsp /views/jsp/error/404.jsp - to display 404 instead of default whitelabel page