SpringBoot:找不到匹配的bean异常

Aru*_*run 6 java spring jsp spring-mvc spring-boot

我想在我的第一个SpringBoot应用程序中启动我的登录页面:

主类

@SpringBootApplication

public class MainGate extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainGate.class);
    }

    public static void main(String... args) {
        System.out.println("Booting .. ");
        SpringApplication.run(MainGate.class, args) ;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的Gradle文件

buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
    }
    repositories {
        maven {
            url "http://masked_domain/repository/external-proxy-group/"
        }
        maven {
            url "https://plugins.gradle.org/m2/"
        }
}
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.arun'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

task fatJar(type: Jar) {
    manifest {
        attributes(
            'Implementation-Title': 'Arun Spring Boot Application',
            'Implementation-Version': version,
            'Built-By': System.getProperty('user.name'),
            'Built-Date': new Date(),
            'Main-Class':  'com.arun.MainGate',
            'Built-JDK': System.getProperty('java.version')
        )
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

repositories {
        maven {
            url "http://masked_domain/repository/external-proxy-group/"
        }
        maven {
            url "https://plugins.gradle.org/m2/"
        }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testImplementation 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)

应用属性文件

spring.mvc.view.prefix: jsp/
spring.mvc.view.suffix: .jsp

logging.level.org.springframework=debug
Run Code Online (Sandbox Code Playgroud)

资源文件夹

在此输入图像描述

控制器类

@Controller
public class CommonController {

    @RequestMapping("/")
    public String home(Map<String, Object> model) {
        System.out.println("Reached the homeContoller");
        return "sso_arch" ;
    }

}
Run Code Online (Sandbox Code Playgroud)

我得到的例外

 017-10-20 17:01:28.568 TRACE 6704 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking 'com.arun.controller.CommonController.home' with arguments [{}]
Reached the homeContoller
2017-10-20 17:01:28.568 TRACE 6704 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [com.arun.controller.CommonController.home] returned [sso_arch]
2017-10-20 17:01:28.574 DEBUG 6704 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2017-10-20 17:01:28.574 DEBUG 6704 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'sso_arch'
2017-10-20 17:01:28.577 DEBUG 6704 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory     : Invoking afterPropertiesSet() on bean with name 'sso_arch'
Run Code Online (Sandbox Code Playgroud)

如何为JSP配置spring boot mvc app中提到的解决方案不管用.因此提出一个新的线程.

use*_*900 5

您找到了正确的问题,但您似乎没有在答案中添加解决方案,您需要添加一个 InternalResourceViewResolver

这个 ViewResolver 允许我们设置视图名称的前缀或后缀等属性来生成最终的视图页面 URL:

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
}
Run Code Online (Sandbox Code Playgroud)

并确保您有依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 不过,`ViewResolver` 应该不是必需的,Spring boot 应该处理这个问题。这也是为什么你需要配置 `spring.mvc.view.prefix` 和 `spring.mvc.view.suffix` 属性(它们被用作 `ViewResolver` 的前缀/后缀)。([`WebMvcAutoConfiguration`](https://github.com/spring-projects/spring-boot/blob/97c1365e248966e8e793091d59048573459d7600/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot /autoconfigure/web/servlet/WebMvcAutoConfiguration.java#L224-L230)) (4认同)