Java 在spring控制器spring boot中提供jsp页面时出错?
我对Spring比较陌生,所以我只是尝试从Spring控制器呈现一个jsp页面。然而,我一直收到一个白色标签错误页面。我已经尝试了几乎所有可能的方法来解决错误,但我似乎无法找出导致404的原因。有什么建议吗Java 在spring控制器spring boot中提供jsp页面时出错?,java,spring,jsp,spring-boot,Java,Spring,Jsp,Spring Boot,我对Spring比较陌生,所以我只是尝试从Spring控制器呈现一个jsp页面。然而,我一直收到一个白色标签错误页面。我已经尝试了几乎所有可能的方法来解决错误,但我似乎无法找出导致404的原因。有什么建议吗 This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Dec 30 11:45:49 EAT 2017 There was an unex
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Dec 30 11:45:49 EAT 2017
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/index.jsp
package com.example.TestProject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class HomeController {
@RequestMapping("/")
public String hello(ModelMap model){
model.put("message","Hello World");
return "index";
}
}
package com.example.TestProject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>TestProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestProject</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
TestApplication.java
package com.example.TestProject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.example.TestProject")
@SpringBootApplication
public class TestProjectApplication {
public static void main(String[] args) {
SpringApplication.run(TestProjectApplication.class, args);
}
}
此应用程序没有/error的显式映射,因此您将其视为回退。
2017年12月30日星期六11:45:49
出现意外错误(类型=未找到,状态=404)。
/WEB-INF/jsp/index.jsp
包com.example.TestProject;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
@控制器
公共类家庭控制器{
@请求映射(“/”)
公共字符串hello(ModelMap模型){
model.put(“message”、“Hello World”);
返回“索引”;
}
}
包com.example.TestProject;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.web.servlet.viewsolver;
导入org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
导入org.springframework.web.servlet.config.annotation.EnableWebMvc;
导入org.springframework.web.servlet.config.annotation.WebMVCConfigureAdapter;
导入org.springframework.web.servlet.view.InternalResourceViewResolver;
@配置
@EnableWebMvc
公共类MVCConfigureAdapter扩展了WebMVCConfigureAdapter{
@豆子
公共ViewResolver getViewResolver(){
InternalResourceViewResolver解析器=新的InternalResourceViewResolver();
resolver.setPrefix(“/WEB-INF/jsp/”);
resolver.setSuffix(“.jsp”);
返回解析器;
}
@凌驾
公共无效配置DefaultServletHandling(
DefaultServletHandlerConfigurer(配置器){
configurer.enable();
}
}
应用程序属性
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
pom.xml
4.0.0
com.example
请检查是否在生成文件中设置了正确的依赖项
确保依赖项列表中包含jasper和jstl:
<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>
org.apache.tomcat.embed